Backup images of root and home filesystems

This is a nice way to make a backup of your root and home filesystems - at least this is how I get it done. Before you read on, I'll say that the home files are on a different mounted drive than the root files. These are also LVM2 drives. First I check the drive that will get the final partimage files. Then I mount the spare drive to be used by partimage. This allows me to never unmount the root and home filesystems. Then I unmount the spare drive and check it. Next I make the partimage copy or delete the tmp file if it fails. Last I delete images older than 25 days and unmount. This allows me to keep four backup versions - one for today and three previous images. I set it all up to run in cron on a weekly basis.

#!/bin/bash
#make partimage of root filesytem and homes

#today's date, used later for image filename
TODAY=`date +%m-%d-%Y`

#check image drive
e2fsck -y /dev/vg1/image || { echo "fsck image drive failed" ; exit ; }

#mount root copy drive
{ mount /dev/vg1/test /mnt && echo -e "\ncopy drive mounted\n" ; } || { echo "can't mount root copy" ; exit ; }

#rsync the root filesystem, but exclude the home filesystem from being deleted
{ rsync -vax --delete --exclude="/y/homes/" / /mnt && echo ; } || echo "can't rsync root files"

#rsync home filesystem
{ rsync -vax --delete /home /mnt/y/homes && echo ; } || echo "can't rsync home files"

#unmount and check copy drive
umount /mnt && fsck -y /dev/vg1/test || { echo "fsck root copy failed" ; exit ; }

#mount image drive
{ mount /dev/vg1/image /y/images && echo -e "\nimages mounted\n" ; } || { echo "can't mount image drive" ; exit ; }

#partimage with today's date of copy drive OR delete tmp file
{ partimage -doc -g0 -B=bl save /dev/vg1/test /y/images/ghani-${TODAY}.partimg.gz && echo -e "\npartimage made" ; } || { echo "can't partimage root copy" ; find /y/images/* -name "*.tmp" -delete ; }

#delete files older than 25 days (saves this backup plus last 3 backups = 4 total)
find /y/images/*partimg* -mtime +25 -delete

#unmount image drive
umount /y/images || echo "image drive unmount failed"
Category: