How to move var to new partition on CentOS 7 linux

Here we will show you how to move var to new partition on CentOS 7 linux. also it is applicable to another directory like /home, /tmp or other directories that has possibility to be separate from root.

Sometimes, there is a situation where you have installed your OS and then attach new drive to your computer. you decide to move some directory from root to this new drive.
for example you have a ftp server and have set it up to store files on /var, but as times go, /var gets bigger and bigger. this growing finally may fill your root parition that has a dramatic negative impact on performance. so you need to separate your /var from root directory.

Our environment

OS: CentOS 7
No. of Disks: One 200GB for OS and four 500 GB disks with LVM layout and XFS filesystem

Note: if you want to know how to create LVM layout you can refer to How to create LVM with four disks on CentOS 7 linux

1- Install rsync:

# yum install rsync

2- Create new directory:

we need temporary directoray to mount our LVM on it.

# mkdir /mnt/var_new

3-Mount lvm:

after creating temporary directory, we mount lvm on it:

# mount /dev/vg00/lvol0 /mnt/var_new

4- Copy and sync content

the following command will move files on current var to new partition:

# rsync -avHPSAX /var/ /mnt/var_new/

5- Same content in two directories:

# diff -r /var /mnt/var_new/

6- Rename /var:

# mv /var /var_old

7- Create new /var:

# mkdir /var

8- Add fstab entry

edit/etc/fstab file and add an entry to mount LVM on /var directory:

# /dev/vg00/lvol0 /var xfs defaults 0 0

9- Unmount var_new/:

# umount /mnt/var_new

10- Finally mount /var:

# mount /var