Underpants
January 23rd, 2007, 11:23 PM
I figured I'd share a shell script that I just got finished tweaking. Should help out those to wish to perform incremental backups of their system. You should know how to create a shell script and make it executable. For this to run you'll need to run it as root, so "$ sudo ./backup.sh"
It's laid out pretty basic. I stole the script from oreilly.com but have edited to simplify it so it looks pretty in a text editor. You can easily add more increments in step 2 if you need. You can also easily change the source and destination to fit your needs. (such as only needing to back up your personal /home folder)
My goal here isn't to explain how any of it works. I figured it out all by myself. I've changed it so anyone else should be able to look at it and easily use a man page or a --help to figure out what's going on pretty easily.
I believe there is an issue with timestamps using this method, so I think just to do things "right" someone needs to add a 'touch' section to the end that keeps things tidy. It doesn't bother me for this box, but if anyone would like to contribute....
# rotating snapshots of /home
# step 1: delete the oldest snapshot, if it exists:
if [ -d /media/usbdisk/hourly.3 ] ; then rm -rf /media/usbdisk/hourly.3 ; fi;
# step 2: shift the middle snapshots(s) back by one, if they exist
if [ -d /media/usbdisk/hourly.2 ] ; then mv /media/usbdisk/hourly.2 /media/usbdisk/hourly.3 ; fi;
if [ -d /media/usbdisk/hourly.1 ] ; then mv /media/usbdisk/hourly.1 /media/usbdisk/hourly.2 ; fi;
# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot, if that exists
if [ -d /media/usbdisk/hourly.0 ] ; then cp -al /media/usbdisk/hourly.0 /media/usbdisk/hourly.1 ; fi;
# step 4: rsync from the system into the latest snapshot (notice that
# rsync behaves like cp --remove-destination by default, so the destination
# is unlinked first. If it were not so, this would copy over the other
# snapshot(s) too!
rsync -vax --delete --delete-excluded --exclude-from="/home/aaron/.backup_exclude" / /media/usbdisk/hourly.0 ;
It's laid out pretty basic. I stole the script from oreilly.com but have edited to simplify it so it looks pretty in a text editor. You can easily add more increments in step 2 if you need. You can also easily change the source and destination to fit your needs. (such as only needing to back up your personal /home folder)
My goal here isn't to explain how any of it works. I figured it out all by myself. I've changed it so anyone else should be able to look at it and easily use a man page or a --help to figure out what's going on pretty easily.
I believe there is an issue with timestamps using this method, so I think just to do things "right" someone needs to add a 'touch' section to the end that keeps things tidy. It doesn't bother me for this box, but if anyone would like to contribute....
# rotating snapshots of /home
# step 1: delete the oldest snapshot, if it exists:
if [ -d /media/usbdisk/hourly.3 ] ; then rm -rf /media/usbdisk/hourly.3 ; fi;
# step 2: shift the middle snapshots(s) back by one, if they exist
if [ -d /media/usbdisk/hourly.2 ] ; then mv /media/usbdisk/hourly.2 /media/usbdisk/hourly.3 ; fi;
if [ -d /media/usbdisk/hourly.1 ] ; then mv /media/usbdisk/hourly.1 /media/usbdisk/hourly.2 ; fi;
# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot, if that exists
if [ -d /media/usbdisk/hourly.0 ] ; then cp -al /media/usbdisk/hourly.0 /media/usbdisk/hourly.1 ; fi;
# step 4: rsync from the system into the latest snapshot (notice that
# rsync behaves like cp --remove-destination by default, so the destination
# is unlinked first. If it were not so, this would copy over the other
# snapshot(s) too!
rsync -vax --delete --delete-excluded --exclude-from="/home/aaron/.backup_exclude" / /media/usbdisk/hourly.0 ;