Well this Weeks mini project was to get a automated backup of all my Virtual machines on my KVM host, the first hurdle was to shut down the running VMs this is completed by executing virsh shutdown x where x is the domain number of the VM, I normally never have more then five machines running so I started the script with
#!/bin/bash
virsh shutdown 1
virsh shutdown 2
virsh shutdown 3
virsh shutdown 4
virsh shutdown 5
after this the VMs shutdown now I figured Compression would be key as most of these VMs have forty gigabyte hard drives if not bigger but normally only use about half.
Originally I started with tar.bz2 with the following command
sudo tar jcvf VM-Backup.tar.bz2 dir_1 dir_2
Sadly this took forever!! upon a little research it was due to the fact that bzip2 isn’t SMP compliant otherwise known as using multiple cores or CPUs
so next I tried Gzip with this command
sudo tar zcf VM-Backup.tar.gz dir_1 dir_2
To the same effect so I started to invest time in google until i found a SMP compatible BZIP2 here: http://compression.ca/pbzip2/ and was finally happy with the results with this command
sudo tar cfv VM-Backup.tar.bz2 –use-compress-prog=pbzip2 dir_1 dir_2
This used all cores available and maxed them during compression, for being a quad core system it cut the compression time roughly down to a third of what it was.
Now that things are starting to look up I decided I wanted to Log when the script ran and tag the output files with the date and time of Backup this was done by making a Variable for date
DATE=$(date +%m%d%Y-%H%M)
at this point what ever you put $DATE in it will output MMDDYYYY-HHmm so I wrote a few echo lines like this one
echo “Nightly Backup Started for $DATE” > /location/of/log
After all was said and done it was brought to my attention a question, “How do you check your backups for data integrity?” so I tagged on the end of all the compression scripts an md5 hash output
&& md5sum * > VM-Backup-$DATE.MD5
you may notice the $DATE again I wanted to make sure the same MD5 matched the Backup file, I then thought about the space this is going to take up and figured one backup being kept is good enough to each time this script is ran it changes directory into the back up directory and deletes all files with
cd /location/of/backup/
rm *
and finished off with a reboot as I plan to run this weekly, this is my finished script changed for system anonymity
#!/bin/bash
DATE=$(date +%m%d%Y-%H%M)
#echo $DATE
echo “Nightly Backup Started for $DATE” > /location/of/log
cd /location/of/backup/
rm *
virsh shutdown 1
virsh shutdown 2
virsh shutdown 3
virsh shutdown 4
virsh shutdown 5
#sudo tar jcvf VM-Backup-$DATE.tar.bz2 dir1 dir2 && md5sum * > VM-Backup-$DATE.MD5
#sudo tar zcf VM-Backup-$DATE.tar.gz dir1 dir2 && md5sum * > VM-Backup-$DATE.MD5
sudo tar cfv VM-Backup-$DATE.tar.bz2 –use-compress-prog=pbzip2 dir1 dir2 && md5sum * > VM-Backup-$DATE.MD5
DATETWO=$(date +%m%d%Y-%H%M)
echo “Nightly Backup Complete for $DATETWO” > /location/of/log
sudo reboot
Enjoy and leave me tips if you have suggestions or kudos
Like this:
Like Loading...