Backup Restore or Clone LIVE

Backup/Restore/Clone a partition LIVE using LVM Snapshots and rsync

Formulated this hack to fix the disk errors on my /var partition normally you could do this with any partition and could be preformed live too. However considering i have to do fsck on /var the machine must be shutdown -h now first then you have to restart udev using /etc/init.d/udev start to get snapshots working again.  Note some sort of remote console is need for this operation iDrac for instance.


Do a backup of /var to /usr/backups/rsync_var using a handy dandy snapshot

mkdir /usr/local/backups/snap_var
mkdir /usr/local/backups/rsync_var
lvcreate -L15G -s -n snap_var /dev/vg1/var
mount /dev/vg1/snap_var /usr/backups/snap_var

This command should be edited to taste note –inplace or –sparse options here however i do not think these matters as i have tried not to create any KVM images on /var instead making them have their own LVM partition, problem solved right? you tell me if you know!

time ionice -c2 -n7 rsync --archive --one-file-system --hard-links --acls --xattrs --human-readable --inplace --numeric-ids --delete --delete-excluded --progress --log-file=/root/rsync.log --exclude-from=/root/rsync-exclude-var.txt --include-from=/root/rsync-include-var.txt /usr/backups/snap_var/ /usr/backups/rsync_var
lvremove /dev/vg1/snap_var

revision 2 of thist command

time rsync ionice -c2 -n7 -a -A -x -X -v -h --progress --delete --delete-excluded --exclude='/var/lib/vz/dump' --exclude='/var/lib/vz/private/101' --log-file=/root/rsync.log /usr/local/backups/snap_var/ /var3

Note the above beautify crafted rsync command requires the existence of /root/rsync-exclude.txt and /root/rsync-include.txt and creates a log file called /root/rsync.log. I will put some examples here soon for excluding complicated exclude commands

Implement as a quick and dirty rescue backup

Hoping i do not have to use this, but for example if the fsck breaks my /var partition i can slip in the fresh backup quite easily using /usr/backups/rsync_var as a new /var partition.

umount /dev/vg1/var
mount /usr/backups/rsync_var /var

Clone and or make perment

Again hoping not to use this for /var but it would come in handy for a standard live partion clone operation
mkdir var2
lvcreate -l 500G -n /dev/vg1/var2
mkfs.ext4 /dev/vg1/var2
mount /dev/mapper/vg1-var2 /var2
time rsync -av --delete --progress /usr/backups/rsync_var/ /var2
umount /dev/vg1/var2
edit /etc/fstab comment out original var line and add this one
/dev/vg1/var2 /var ext4 defaults 0 2
mount -a
rm -r /var2

NOTE you can skip strait to this step by rsyncing directly from the snapshot to the LVM device this is done in the above revised command

migrate software raid 1 to 5

There was a thread about this on the Linux-raid list a little while ago. The process is actually much easier. All you have to do is start the 2-drive RAID1 array as a 2-drive RAID5 array:
mdadm –grow /dev/mdx –level=5
and then add a third hard drive. It is important that the raid set is started as a RAID5 array before the third drive is added.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.