#!/bin/bash # This script makes a snapshot of several databases using rsync. # It's based on Geordy Kitchen's snapshot script found at mikerubel.org # which was based on Rob Bos's snapshot script found at mikerubel.org. # You will need to have ssh keys set up to allow access witout password # Version 0.1, Darrel O'Pry, April 18, 2003 # 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/db remoteserver="server.com" # Use a trailing slash for directories, just the filename for files. database="database1 database2" # Number of times to run per day. hourly=1 # Constants daily=7 weekly=4 monthly=12 MOUNT_DEVICE=/dev/hda3 SNAPSHOT_RW=/backup date # attempt to remount the RW mount point as RW; else abort mount -o remount,rw $MOUNT_DEVICE $SNAPSHOT_RW ; if (( $? )); then { $ECHO "snapshot: could not remount $SNAPSHOT_RW readwrite"; exit; } fi; case $1 in hourly) [ -d $dst/$1.1 ] && cp -al $dst/$1.1 $dst/$1.0 for i in $database; do [ -d $dst/$1.0/$i ] || mkdir -p $dst/$1.0/$i ssh $remoteserver mysqlhotcopy -p xxxx --allowold $i /root rsync -e ssh -a --delete $srcroot:/root/$i $dst/$1.0/ 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 mount -o remount,ro $MOUNT_DEVICE $SNAPSHOT_RW ; if (( $? )); then { $ECHO "snapshot: could not remount $SNAPSHOT_RW readonly"; exit; } fi;