#!/bin/bash # Mac backup script for OS X. # $Id: netbackup,v 1.12 2006/10/03 03:18:56 perette Exp perette $ # Copyright 2003 - 2006 Perette Barella. # Released under the GNU public license. # Backs up to a remote machine using rsync over ssh. # Hidden resource files are copied to .resource while the # backup takes place, thus you'll need some free space for this to # take place. The files are deleted on completion, and a signal # handler cleans up in case of an interruption. # If going from Mac to Mac, you can use the -E version of rsync... # but that doesn't work with most non-mac rsyncs, so it's disabled. # # To restore: # rsync manually back to the Mac, then resourcefork -r integrate status=0 ###################################################################### # Function: usage # Purpose: Displays the usage of this command. # Author: Perette Barella #--------------------------------------------------------------------- function usage { echo "Usage: $arg0 [-d] [-i]" exit 1 } ##### End of function usage ##### ###################################################################### # Function: parse_arguments # Purpose: Parses the command line arguments # Arguments: The command line arguments. # Returns: Number of arguments parsed. # Author: Perette Barella #--------------------------------------------------------------------- function parse_arguments { typeset option DELETE="" RESTORE=false while getopts 'dr?' option do case "$option" in d) DELETE="--delete --delete-excluded" ;; r) RESTORE=true ;; *) usage ;; esac done typeset status=0 return $((OPTIND - 1)) } ##### End of function parse_arguments ##### function cleanup_resource { echo "$arg0: Cleaning up .resource files." >/dev/tty [ -f "$resourcefiles" ] && resourcefork -f "$resourcefiles" hide $user exit $? } function backup_user { resourcefiles="/var/tmp/$arg0.$$.tmp" typeset resourceopts="-E" # Copy the Mac resource files into viewable resources. IFS="" if ! $GOOD_RSYNC then trap "cleanup_resource" SIGINT SIGABRT SIGQUIT SIGHUP resourcefork -u -f "$resourcefiles" expose $user resourceopts="" fi $GOOD_RSYNC && return 0 $HOME/bin/rsync -v -a -H -x $resourceopts --delete $compress \ --exclude="Library/Caches/*" \ --exclude="Public/Video Queue/*" \ --exclude=".Trash/*" \ --delete-excluded \ $user/ \ $user@$dest/$host typeset status=$? # Remove the resource file copies in place. resourcefork -f "$resourcefiles" hide $user return $status } cd ~/.. || exit 1 arg0=$(basename "$0") || exit 1 parse_arguments "$@" shift $? # Mac OS X 10.4 has a -E version for rsync which does all the # handling of the resource files that this script did before, # plus it handles the new file metadata. Use -E if it's available. release=$(uname -r) major_release=$(echo $release | cut -d. -f1) GOOD_RSYNC=false [ $major_release -ge 8 ] && GOOD_RSYNC=false user="$USER" compress="" host="$(hostname | cut -d. -f1)" if ping -c1 -q BACKUPHOST >/dev/null 2>&1 then backuphost=BACKUPHOST compress="" else backuphost=BACKUPHOST.DOMAIN.NET compress="-z" host=MY_HOSTNAME fi dest="$backuphost:/Users/perette" if $RESTORE then echo "Can't automatically restore." exit $? fi backup_user status=$? # Integrate the resource forks on the destination machine ssh -n $backuphost "resourcefork -u integrate $host" exit $status