#/bin/bash # # SyncUsers # # Generate sync backup of all user data to $EXTDISK. # $EXTDISK is the volume name of a mounted volume. In my case a firewire external disk. The script # will go through all users (actually all subdirectories of /Users) and syncronise that users directory # (/Users/aUser) with a copy at $EXTDISK/aUser. You end up with a copy of the Users files on $EXTDISK # # We omit the itunes user most of the time. With a 30gb library it would take a while. Include itunes by calling # as: SyncUsers ALL # # Notes: # The script should be executed via sudo to ensure that all users can be archived and so that mdutil works. # # rsync often when used with a large number of files because the Spotlight indexer gets overwhelmed # (rsync: fstat failed: No such file or directory). To avoid this error, you can switch off Spotlight # indexing for the duration of the sync process using function spotlight_switch. This is currently commented # out. # # /usr/local/bin/rsync is RsyncX (http://archive.macosxlabs.org) and not the standard rsync which has problems # with modification times (even at 10.4.x) # RESET=`tput sgr0` RED='\E[1;31m' GREEN='\E[1;32m' RSYNCX=/usr/local/bin/rsync # Location of rsyncx EXCLUDES="--exclude tmp* --exclude *Cache* --exclude .Trash* --exclude *itunes/*" EXTDISK=DataFormac OMIT=itunes function spotlight_switch () { echo Spotlight $1 # /usr/bin/mdutil -i $1 / /usr/bin/mdutil -i $1 /Volumes/${EXTDISK} } function ld () { ls -F /Users | grep '/$' } # # Start here # echo ${0##/*/} - Version 1.0 [ ! -z $SDEBUG ] && set -x echo -e ${GREEN}Sync /Users to ${EXTDISK}${RESET} # # Check the required volume is mounted # [ ! -d /Volumes/${EXTDISK} ] && echo -e ${RED}${EXTDISK} not mounted${RESET} && exit # # Check we have the right version of rsync # if [ ! -f $RSYNCX ]; then echo -e $RSYNCX ${RED}does not exist - fatal error${RESET} exit 1 else echo -e $RSYNCX ${GREEN}found OK${RESET} fi #spotlight_switch off for dirs in `ld`; do echo -n `date +%H:%M:%S` ${dirs%/*} if [ "$dirs" = "$OMIT/" ] && [ ! "$1" = "ALL" ]; then echo " IGNORED" else # rsync the user here # -a = archive mode which ensures that symbolic links, devices, attributes, permissions, # ownerships, etc. are preserved in the transfer # -e = enables the transfer of extended attributes # --delete = delete files in the target folder that are not in the source folder # --delete-after = Only delete old files when new files have been copied. $RSYNCX -ae --delete --delete-after $EXCLUDES /Users/${dirs} /Volumes/$EXTDISK/${dirs} state=$? if [ $state -ne 0 ]; then echo -e ${RED} failed ${state}${RESET} else echo -e ${GREEN} OK${RESET} fi fi done echo `date +%H:%M:%S` Complete #spotlight_switch on