Rsync backup

Jump to: navigation, search

See also rsync notes.

This is the small script I use for backups with rsync. This includes a one week rotating window. Prior days include only the files that changed, so each day does not take up the full amount of disk. The prior days' backups will appear complete because identical files are hard linked. There's about a hundred versions of this script on the Internet. This one isn't super fancy, but it's small and gets the job done.

When the script complete successfully it will create a file called BACKUP_TIMESTAMP in the root directory of the destination. If the script fails for any reason it will create a file called BACKUP_ERROR. The files will contain the finish date and exit code and message reported by `rsync`.

This script can change the permission of all directories to 755 and all files to 644. This is specified with the -n flag. This may be a bad thing in some cases. In my case, I am using this as a backup for a shared NAS through SaMBa. For restore purposes the difference between Windows permissions and UNIX permissions is worthless. Normalizing the permissions makes the the files easier to browse through the UNIX side and it removes the problem where someone might change permissions in the backup target directory which could cause rsync to fail.


This script doesn't require much setup. It will automatically create destination backup directories if they don't exist. After the first time you run it you will see that it creates numbered copies of the source base directory. Each numbered copy appears to be a separate copy, but due to the use of hardlinks each copy takes no more room than one copy plus whatever files have changed between each run of the backup. If you run the backup script from a daily cron job then each numbered directory will correspond to one day in the past.


To use this script add something similar to the following line to /etc/crontab:

   01 4 * * * backup /usr/bin/rsync_backup /media/shareddocs /home/backup

rsync_backup script

Click here to download: rsync_backup <include src="/home/noahspurrier/" highlight="sh" />

Ken's exclude.list file

My friend, Ken, uses this rsync exclude list.

### tmp ###
- **/tmp/**
### transient ###
- /proc/
- /mnt/
- /cdrom/
- /media/
- /sys/
- /var/run/
- /var/lock/
- /var/log/
- /var/tmp/
- /dev/
### config ###
- /etc/adjtime
- /etc/motd
- /etc/mtab
- /etc/resolv.conf
- /etc/lvm/.cache
- /var/lib/alsa/asound.state
- /var/lib/dhcp3/dhclient.*.leases
- /var/lib/urandom/
- /var/lib/urandom/random-seed
### apps ###
# firefox
- **/.mozilla/firefox/*.default/Cache/**
# bash
- .bash_history
# svn
- .svn/
- .svn/**
# vim
- .viminfo
- .*.swp
# X
+ .cache/sessions/
- .cache/sessions/**
- .xsession-errors
- .ICEauthority
- .serverauth.*
# less
- .lesshst
### backup ###
- *~