#!/bin/bash # back up an IMAP mail tree # $Id$ OIFS="$IFS" IFS=" " if test -f "$HOME/bin/functions" then . "$HOME/bin/functions" else echo "Cannot open functions library $HOME/bin/functions" 1>&2 exit 1 fi abort() { notice "Aborted" } cleanup() { info "Cleaning up" if test -f "$dfout" then run rm -rf -- "$dfout" fi if test -d "$snapshot" then run rm -rf -- "$snapshot" fi } # # default configuration # debug= quiet=true simulate= src=/home/michael/mail #dst=voot:/home/kadmos/mail dst=dreamhost:/home/mikelward/mail logfile=/home/michael/log/mailbackup.log dateformat='%-e %B %Y' timeformat='%H:%M:%S' # # start # trap 'cleanup' EXIT trap 'abort' INT TERM info "Starting backup of $src on "$(date +"$dateformat")" at "$(date +"$timeformat") # # ensure required programs are available # if ! silent type rsync then error "Cannot find rsync in PATH" exit 1 fi # # check disk space requirements # size=$(get_size_of_file_or_directory "$src") if test $? -ne 0 then error "Cannot determine disk usage of $src" exit 1 fi tmpdir=$(get_temporary_directory) if test $? -ne 0 then error "Cannot determine temporary directory" exit 1 fi available=$(get_free_space_on_filesystem "$tmpdir") if test $? -ne 0 then error "Cannot determine free space on $tmpdir" exit 1 fi if test $available -lt $size then error "Need at least $size kilobytes free on $tmpdir" exit 1 fi # # take a snapshot of the repository # src=${src%/} dirname=$(get_filename_part "$src") if test -z "$dirname" then dirname="root" fi snapshot="$tmpdir"/"$dirname".$$ if test -d "$snapshot" then error "Snapshot directory $snapshot already exists" exit 1 fi info "Creating a hot copy of $src" run cp -a "$src" "$snapshot" if test $? -ne 0 then error "Cannot create hot copy of $src" exit 1 fi # # copy the hot copy to the backup directory # if is_remote "$dst" then debug "Remote destination" #dsthost=$(get_hostname_part "$dst") #dstdir=$(get_location_part "$dst") #notice "Removing $dstdir on $dsthost" #ssh "$dsthost" "test -d \"$dstdir\" && rm -rf -- \"$dstdir\"" #info "Copying $snapshot to $dst" info "Synchronizing $snapshot to $dst" flags= if test "$debug" then flags="$flags -v" fi if test "$quiet" then flags="$flags -q" fi #scp -C -r $flags "$snapshot" "$dst" # ensure source has a trailing slash so we only copy the directory contents # (local:~/svn.12345/ -> remote:~/svn rather than # local:~/svn.12345/ -> remote:~/svn/svn.12345) snapshot=${snapshot%/}/ run rsync -a --delete $flags "$snapshot" "$dst" else debug "Local destination, using cp" error "Local files are currently unsupported" exit 1 fi info "Finished backup of $src on "$(date +"$dateformat")" at "$(date +"$timeformat")