Rsync backup
From Noah.org
Jump to navigationJump to searchThis is the small script I use for online backups with rsync. This includes a one week rotating window. Prior days backups 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 copied files are hard linked. This isn't super fancy, but it gets the job done.
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
#!/bin/sh # # This maintains a one week rotating backup. This will normalize permissions on # all files and directories on backups. It has happened that someone removed # owner write permissions on some files, thus breaking the backup process. This # prevents that from happening. All this permission changing it tedious, but it # eliminates any doubts. I could have done this with "chmod -R +X", but I wanted # to be explicit. # # Pass two arguments: # rsync_backup SOURCE_DIRECTORY BACKUP_ROOT # # $Id$ SOURCE_DIRECTORY=$1 BACKUP_ROOT=$2 SOURCE_BASE=`basename $SOURCE_DIRECTORY` RSYNC_OPTS="-a --delete -q" #RSYNC_OPTS="-a --delete -v --progress" PERMS_DIR=755 PERMS_FILE=644 # Create the rotation directories if they don't exist. if [ ! -d $BACKUP_ROOT ] ; then mkdir $BACKUP_ROOT fi if [ ! -d $BACKUP_ROOT/$SOURCE_BASE.0 ] ; then mkdir $BACKUP_ROOT/$SOURCE_BASE.0 fi if [ ! -d $BACKUP_ROOT/$SOURCE_BASE.1 ] ; then mkdir $BACKUP_ROOT/$SOURCE_BASE.1 fi if [ ! -d $BACKUP_ROOT/$SOURCE_BASE.2 ] ; then mkdir $BACKUP_ROOT/$SOURCE_BASE.2 fi if [ ! -d $BACKUP_ROOT/$SOURCE_BASE.3 ] ; then mkdir $BACKUP_ROOT/$SOURCE_BASE.3 fi if [ ! -d $BACKUP_ROOT/$SOURCE_BASE.4 ] ; then mkdir $BACKUP_ROOT/$SOURCE_BASE.4 fi if [ ! -d $BACKUP_ROOT/$SOURCE_BASE.5 ] ; then mkdir $BACKUP_ROOT/$SOURCE_BASE.5 fi if [ ! -d $BACKUP_ROOT/$SOURCE_BASE.6 ] ; then mkdir $BACKUP_ROOT/$SOURCE_BASE.6 fi # Rotate backups. find $BACKUP_ROOT/$SOURCE_BASE.6 -type d -exec chmod $PERMS_DIR {} \; find $BACKUP_ROOT/$SOURCE_BASE.6 -type f -exec chmod $PERMS_FILE {} \; rm -rf $BACKUP_ROOT/$SOURCE_BASE.6 mv $BACKUP_ROOT/$SOURCE_BASE.5 $BACKUP_ROOT/$SOURCE_BASE.6 mv $BACKUP_ROOT/$SOURCE_BASE.4 $BACKUP_ROOT/$SOURCE_BASE.5 mv $BACKUP_ROOT/$SOURCE_BASE.3 $BACKUP_ROOT/$SOURCE_BASE.4 mv $BACKUP_ROOT/$SOURCE_BASE.2 $BACKUP_ROOT/$SOURCE_BASE.3 mv $BACKUP_ROOT/$SOURCE_BASE.1 $BACKUP_ROOT/$SOURCE_BASE.2 cp -al $BACKUP_ROOT/$SOURCE_BASE.0 $BACKUP_ROOT/$SOURCE_BASE.1 # Backup. find $BACKUP_ROOT/$SOURCE_BASE.0 -type d -exec chmod $PERMS_DIR {} \; find $BACKUP_ROOT/$SOURCE_BASE.0 -type f -exec chmod $PERMS_FILE {} \; rsync $RSYNC_OPTS $SOURCE_DIRECTORY/. $BACKUP_ROOT/$SOURCE_BASE.0/. find $BACKUP_ROOT/$SOURCE_BASE.0 -type d -exec chmod $PERMS_DIR {} \; find $BACKUP_ROOT/$SOURCE_BASE.0 -type f -exec chmod $PERMS_FILE {} \; # Create a timestamp file to show when backup process was complete. date > $BACKUP_ROOT/$SOURCE_BASE.0/BACKUP_TIMESTAMP # Create a shortcut to the latest backup. ln -sf $BACKUP_ROOT/backup.0 $BACKUP_ROOT/latest