#!/bin/bash # This script makes a snapshot of several directories using rsync. # Version 0.5, Rob Bos, September 8, 2002 # This script is released under the terms of the GNU General Public License, version 2.0. # run "hourly" from a crontab; run daily, weekly, and monthly from /etc/cron.{daily,weekly,monthly}/. [ -z "$1" ] && echo syntax: $0 snapshottype && exit 1 type=$1 # hourly, daily, monthly snapshot? logfile=/var/log/snapshot.log backuproot=/home/.backup backup=`cat /etc/snapshot/include.text` excludefile=/etc/snapshot/exclude.text # if you run every two hours, make it 12, every hour, 24, etc. try to span the entire day. # The below setting runs 8 times a day (every three hours), keeps 7 days, 4 weeks, and 12 months # of backup. hourlymax=8 dailymax=7 weeklymax=4 monthlymax=12 # This simplifies things down below so I can use $max to delete # the overrotated snapshot, as well as $hourlymax/$dailymax to do # rotation checks. [ $type == "hourly" ] && max=hourlymax [ $type == "daily" ] && max=dailymax [ $type == "weekly" ] && max=weeklymax [ $type == "monthly" ] && max=monthlymax # Rotate the current list of backups, if we can. if [ -d $backuproot/$type.1 ]; then oldest=`ls -dt $backuproot/$type.* | tail -n 1 | sed 's/^.*\.//'` for i in `seq $oldest 1`; do mv $backuproot/$type.$i $backuproot/$type.$(( i + 1 )) done fi if [ "$type" == "hourly" ]; then # recreate directory structure with hard links. cp -al $backuproot/$type.2 $backuproot/$type.1 rsync -va --delete --delete-excluded --exclude-from=$excludefile $backup $backuproot/$type.1/ elif [ "$type" == "daily" ]; then [ -d $backuproot/hourly.$hourlymax ] && mv $backuproot/hourly.$hourlymax $backuproot/daily.1 elif [ "$type" == "weekly" ]; then [ -d $backuproot/daily.$dailymax ] && mv $backuproot/daily.$hourlymax $backuproot/weekly.1 elif [ "$type" == "monthly" ]; then [ -d $backuproot/weekly.$weeklymax ] && mv $backuproot/weekly.$weeklymax $backuproot/monthly.1 elif [ "$type" == "symlinks" ]; then # assumes that /home is the only thing being backed up. temporary thing. cd $backuproot; for i in /home/*;do for f in *;do ln -s $backuproot/$f/$i $i/.snapshot/$f done; done fi # if we've rotated the last backup off the stack, remove it. [ -d $backuproot/$type.$((max+1)) ] && rm -rf $backuproot/$type.$((max+1)) # finally, let's make the new snapshot reflect the current date. [ -d $backuproot/$type.1 ] && touch $backuproot/$type.1