#!/bin/bash # last updated Friday May 17, 2013 # # web site mirror backup script v0.3.. (run as daily cron) # # This runs on a Linux box, where both the real dev mirror, and the backup directory # live (though the latter is on a separate physical drive, of course, it is mounted # in /bax ) you could backup to a different machine, remember to ensure the # share/network-drive is mounted first. You could mount it in this script, of course. # # This script updates the backup mirror in $home with all changed files from my dev # mirror in $backupdir all activity is logged to a file which we parse into sftp # commands with the "website-sync" php script. # # This script now creates a local backup and then copies the backup to remote drive # and on success, deletes the local copy. This could be a mounted NAS backup volume, # or whatever. I find this two-stage approach safer, more robust. # # Normally you would create a wrapper for this script, something like.. # # #!/bin/bash # echo "running backup for mydomain.com.." # /usr/local/bin/corz-backup mydomain.com # exit 0 # # Name that script something short, like "cb" and drop it into your PATH. # # ;o) # # © corz.org 2003-> # # get host name.. if [ -z $1 ]; then echo echo "Please supply a domain name on the command-line. e.g.." echo echo "$0 foobar.net" echo echo "corz-backup will then use settings file: ~/.sftp/foobar.net" echo exit 1 fi # grab settings.. . ~/.sftp/$1 # name of the backup file.. newname="$now.$host.tar.gz" echo "backup for $host: "`date "+%Y.%m.%d-%H.%M"`.. >> $logfile echo >> $logfile echo "backup for $host (full version - with archiving)" cd $backupdir # remove certain files.. (for backup size) for i in $( find $host -name \"*.mp3\" ); do rm "$i"; done echo "archiving current dev mirror.." tar cfz $newname $host >> $logfile # copy the tar.gz to the backup directory with unique dated filename if [ ! -d $arcroot ]; then mkdir -pv $arcroot > /dev/null fi mv $newname $arcroot/ >> $logfile # remount remote storage volumes.. remount > /dev/null if [ ! -d $ol_daily ]; then mkdir -pv $ol_daily > /dev/null fi # If possible, move the archive over to the storage facility.. if [ -e "$ol_daily" ];then echo -n "Copying site backup to offline storage facility.. " cp $arcroot/$newname $ol_daily/ if [ -e "$ol_daily/$newname" ];then echo "Success!" rm $arcroot/$newname >> $logfile &> /dev/null rmdir $arcroot &> /dev/null fi fi # copy over (& log) all the newer files (Linux cp rocks! Apple take note.) echo "copying (& logging) changed files to dev mirror backup.." cp -d -f -p -r -u -v $home/* $backupdir/$host >> $logfile now=`date "+%Y.%m.%d-%H.%M"` echo >> $logfile command -v fortune >/dev/null 2>&1 || { echo "backup for $host complete!"; exit 0; } fortune echo "backup complete at $now" exit 0