How To Move Full Folder On The Partition / To The New Hard Disk

Sometimes when I install Linux, I do not do the partition configuration on Linux so that my Linux only has one partition that is partition /. This will cause problems if one folder commonly used by the system such as /var or /usr folder size is getting larger so the server cannot run normally due to partition / lack of space. Therefore, this article will explain how to move the full folder that resides on the partition / to the new hard disk. This article uses CentOS 7 and it should be applicable to another Linux.

Example condition of my server like this:
folder
You can see that partition / has only 4% available file size and I try to know which folders have big size, I see folder /var is the biggest:

So, I want to try move folder /var to another folder. Then I must insert a new hard disk to the server and make sure CentOS can detect hard disk using command fdisk -l:

You can see that CentOS can detect the hard disk as /dev/sdb. Next, I must format the new hard disk with type the commands:


Then I convert new hard disk to ext4 filesystem and I create new folder newvar in folder /mnt and I mount the hard disk to the new folder:

After that, I must move data in folder /var to /mnt/newvar. To do that, I must change to the single-user mode with type init 1. After I type init 1, the server will display like this:

Type root password and I must copy data in folder /var to /mnt/newvar.


Then I rename folder /var to /var.old and create new folder /var. After that, I do umount and mount processes.

and I check partition size:

From the picture above, I can see that folder /var has separated from the partition / but partition / still 96% was used. So I delete folder /var.old and then check partition size:

Now we can see that partition / only 3% was used. In order to /var partition can be mounted automatically after server reboot then adds the following script in file /etc/fstab:

Reboot server and it should /var partition can be mounted automatically.

Reference:
blog.oshim.net

Leave a Reply

Your email address will not be published. Required fields are marked *