System Restore with Rsync


Rsync is one of the  super-duper *nix utility for synchronizing or transferring files among the computers. It has so much functionality that I could not explain it to you in one post. In this post, I will use rsync utility to backup and restore my system. In this activity, I am using CentOS7(virtual guest) on KVM host.

Backing up Whole System Before destroy the running system, I will backup whole running system to my remote backup server with rsync. I am excluding some of the folders as they are populated at boot time. By using below rsync options, I will able to backup the system with extended attributes and SELinux.

backup # rsync -vaHAXSz --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} root@centos7:/ /opt/backup/centos7/
-H, --hard-links      preserve hard links
-A, --acls            preserve ACLs (implies -p)
-X, --xattrs          preserve extended attributes
-S, --sparse          handle sparse files efficiently
-v, --verbose         increase verbosity
-z, --compress        compress file data during the transfer
-a, --archive         archive mode; equals -rlptgoD (no -H,-A,-X)

The system has backed-up,  while it was running. We are safe now. Restoring the Corrupted System. To restore corrupted system from the existing backup we need couple of things to do before the actual operations. 1- Attach live CentOS7 ISO image and the boot the system from it. 2- After your system booted, configure your network and routing settings temporarily to connect to the Backup Server. ifconfig netmask up ip route add default via dev I configure my system as below.

live#ifconfig ens3 netmask up
live#ip route add default via dev ens3

3- Ping your backup server if it is accessible from the live system. 4- Partition the disk with fdisk utility. Or gdisk utility, if you want to partition the disk with GPT. I - For separate boot partition 500MiB is enough. (/dev/sda1) II- For the root (/) partition. 5- Open-Up the terminal. As I used lvm disk on my system, I recreate the VGs and LVs from the existing configuration. In order for that, first copy your lvm folder from your backup  server to your live system’s /root partition.

live# rsync -avz  [email protected]:/opt/backup/centos7/etc/lvm /root/lvm

I will initialize my physical disk to use lvm and I will use the same uuid as before. To find that uuid I will look into the configuration in the /root/lvm/backup/ file. Initializing the disk for LVM with the same uuid.

live# pvcreate --uuid "fpK70u-ZvuO-bgBM-hO67-3M2l-VFFo-0rCWsa" --restorefile /root/lvm/backup/vg\_node01 /dev/sda2
Couldn't find device with uuid fpK70u-ZvuO-bgBM-hO67-3M2l-VFFo-0rCWsa.
Physical volume "/dev/sda2" successfully created.

Restoring the VG

live# vgcfgrestore -f /root/lvm/backup/vg_node01 vg_node01
  Restored volume group vg_node01


live# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_root vg_node01 -wi-a----- 6.71g
lv_swap vg_node01 -wi-a----- 816.00m

Building file system ext4 and swap

live# mkfs.ext4 /dev/sda1
live# mkfs.ext4 /dev/mapper/vg_node01-lv_root 
live# mkswap /dev/mapper/vg_node01-lv_swap

Create a folder  for  chroot

live# mkdir /mnt/sysimage

Restoring files from the backup server. (excluding /boot contents)

live# rsync -vaHAXSz --exclude={"/boot/*"}  [email protected]:/opt/backup/centos7/ /mnt/sysimage/

Mounting pseudo files with –bind option.

live# mount --bind /dev /mnt/sysimage/dev 
live# mount --bind /sys /mnt/sysimage/sys
live# mount --bind /proc /mnt/sysimage/proc


live# chroot /mnt/sysimage /bin/bash

After chroot command applied, all operations that we do will  affect files in the /mnt/sysimage which is our actual system files. Mounting /boot partition in the chroot

centos7# mount /dev/sda1 /boot

After mounting the /boot partition copy the boot contents from the backup server.

centos7# rsync -vaHAXSz [email protected]:/opt/backup/centos7/boot/ /boot/

Install the MBR We need to embed stage1 in the first sector(512Byte) of the harddisk.

[root@centos7 boot]# grub2-install /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.
[root@centos7 boot]# 

Update GRUB2

[root@centos7 boot]# grub2-mkconfig -o /boot/grub2/grub.cfg

Do not forget to change fstab entries If you are using UUID option  in  the fstab, do not forget to change it with the new UUID. You can find the UUID of the each partition with blkid command. Umount the partitions before reboot. Exit from chroot and umount pseudo file system partitions and reboot the host.

centos7# exit
live# umount /mnt/sysimage/dev 
live# umount /mnt/sysimage/sys 
live# umount /mnt/sysimage/proc
live# umount /mnt/sysimage
live# reboot