#!/bin/bash # This script makes a snapshot of several directories using rsync. # It's based on Rob Bos's snapshot script found at mikerubel.org. # Version 0.1, Geordy Kitchen, November 26, 2002 # This script is released under the terms of the GNU General Public License, # version 2.0. # Your crontab will typically look something like this: # 0 */4 * * * /bin/this_script hourly # 59 3 * * * /bin/this_script daily # 58 3 * * 0 /bin/this_script weekly # 57 3 1 * * /bin/this_script monthly # Configuration # Destination directory. dst=/backup srcroot="user1@hostname" # Use a trailing slash for directories, just the filename for files. src="/var/mail/user1 /home/user1/" # Number of times to run per day. hourly=6 # Constants daily=7 weekly=4 monthly=12 case $1 in hourly) [ -d $dst/$1.1 ] && cp -al $dst/$1.1 $dst/$1.0 for i in $src; do dirdst=`echo $i | sed 's|^/||; s|[^/]*$||'` [ -d $dst/$1.0/$dirdst ] || mkdir -p $dst/$1.0/$dirdst rsync -a --delete $srcroot:$i $dst/$1.0/$dirdst done ;; daily) [ -d $dst/hourly.$hourly ] && mv $dst/hourly.$hourly $dst/daily.0 ;; weekly) [ -d $dst/daily.$daily ] && mv $dst/daily.$daily $dst/weekly.0 ;; monthly) [ -d $dst/weekly.$weekly ] && mv $dst/weekly.$weekly $dst/monthly.0 ;; *) echo "syntax: $0 " exit 1 ;; esac # Rotate the current list of backups, if we can. if [ -d $dst/$1.0 ]; then oldest=`ls -d $dst/$1.* | tail -n 1 | sed 's/^.*\.//'` for i in `seq $oldest 0`; do mv $dst/$1.$i $dst/$1.$((i+1)) done fi # if we've rotated the last backup off the stack, remove it. [ -d $dst/$1.$((${!1}+1)) ] && rm -rf $dst/$1.$((${!1}+1)) # finally, let's make the new snapshot reflect the current date. [ -d $dst/$1.1 ] && touch $dst/$1.1