1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 ###############################################################
5 # This handler slowly creates a backup of each user's maildir
6 # to a remote server. It is designed to be run with low overhead
7 # in terms of cpu and bandwidth so it runs pretty slow.
8 # Hardlinking is used to save storage space.
10 # each users maildir will contain these files:
17 # if keepdaily is 3, keepweekly is 2, and keepmonthly is 1.
18 # the actual maildir is stored within each snapshot directory.
20 # The basic algorithm is to rsync each maildir individually,
21 # and to use hard links for retaining historical data.
23 # We handle each maildir individually because it becomes very
24 # unweldy to hardlink and rsync many hundreds of thousands
25 # of files at once. It is much faster to take on smaller
28 # For the backup rotation to work, destuser must be able to run
29 # arbitrary bash commands on the desthost.
31 # Any maildir which is deleted from the source will be moved to
32 # "deleted" directory in the destination. It is up to you to
33 # periodically remove this directory or old maildirs in it.
35 ##############################################################
47 getconf srcdir /var/maildir
53 getconf multiconnection notset
55 letters="a b c d e f g h i j k l m n o p q r s t u v w x y z"
62 #getconf testuser elijah
64 [ -d $srcdir ] || fatal "source directory $srcdir doesn't exist"
66 [ "$multiconnection" == "notset" ] && fatal "The maildir handler uses a very different destination format. See the example .maildir for more information"
68 [ ! $test ] || testflags="--dry-run -v"
69 rsyncflags="$testflags -e 'ssh -p $destport' -r -v --ignore-existing --delete --size-only --bwlimit=$speedlimit"
70 excludes="--exclude '.Trash/\*' --exclude '.Mistakes/\*' --exclude '.Spam/\*'"
72 ##################################################################
78 local letter=${user:0:1}
79 local source="$srcdir/$letter/$user/"
80 local target="$destdir/$letter/$user/$btype.1"
81 [ -d $source ] || fatal "maildir $source not found".
84 ret=`$RSYNC -e "ssh -p $destport" -r \
85 --links --ignore-existing --delete --size-only --bwlimit=$speedlimit \
86 --exclude '.Trash/*' --exclude '.Mistakes/*' --exclude '.Spam/*' \
87 $source $destuser@$desthost:$target \
90 # ignore 0 (success) and 24 (file vanished before it could be copied)
91 if [ $ret != 0 -a $ret != 24 ]; then
92 warning "rsync $user failed"
93 warning " returned: $ret"
94 let "failedcount = failedcount + 1"
95 if [ $failedcount -gt 100 ]; then
96 fatal "100 rsync errors -- something is not working right. bailing out."
99 ssh -o PasswordAuthentication=no $desthost -l $destuser "date +%c%n%s > $target/created"
102 # remove any maildirs from backup which might have been deleted
103 # and add new ones which have just been created.
104 # (actually, it just moved them to the directory "deleted")
106 function do_remove() {
107 local tmp1=`maketemp maildir-tmp-file`
108 local tmp2=`maketemp maildir-tmp-file`
110 ssh -p $destport $destuser@$desthost mkdir -p "$destdir/deleted"
111 for i in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
112 ls -1 "$srcdir/$i/" | sort > $tmp1
113 ssh -p $destport $destuser@$desthost ls -1 "$destdir/$i/" | sort > $tmp2
114 for deluser in `join -v 2 $tmp1 $tmp2`; do
115 [ "$deluser" != "" ] || continue
116 info "removing $destuser@$desthost:$destdir/$i/$deluser/"
117 ssh -p $destport $destuser@$desthost mv "$destdir/$i/$deluser/" "$destdir/deleted"
124 function do_rotate() {
125 [ "$rotate" == "yes" ] || return;
127 local letter=${user:0:1}
128 local backuproot="$destdir/$letter/$user"
130 ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
131 ##### BEGIN REMOTE SCRIPT #####
133 seconds_weekly=604800
134 seconds_monthly=2628000
136 keepweekly=$keepweekly
137 keepmonthly=$keepmonthly
140 if [ ! -d "$backuproot" ]; then
141 echo "Debug: skipping rotate of $user. $backuproot doesn't exist."
144 for rottype in daily weekly monthly; do
145 seconds=\$((seconds_\${rottype}))
147 dir="$backuproot/\$rottype"
148 if [ ! -d \$dir.1 ]; then
149 echo "Debug: \$dir.1 does not exist, skipping."
151 elif [ ! -f \$dir.1/created ]; then
152 echo "Warning: \$dir.1/created does not exist. This backup may be only partially completed. Skipping rotation."
156 # Rotate the current list of backups, if we can.
157 oldest=\`find $backuproot -type d -maxdepth 1 -name \$rottype'.*' | sed 's/^.*\.//' | sort -n | tail -1\`
158 #echo "Debug: oldest \$oldest"
159 [ "\$oldest" == "" ] && oldest=0
160 for (( i=\$oldest; i > 0; i-- )); do
161 if [ -d \$dir.\$i ]; then
162 if [ -f \$dir.\$i/created ]; then
163 created=\`tail -1 \$dir.\$i/created\`
167 cutoff_time=\$(( now - (seconds*(i-1)) ))
168 if [ ! \$created -gt \$cutoff_time ]; then
170 if [ ! -d \$dir.\$next ]; then
171 echo "Debug: \$rottype.\$i --> \$rottype.\$next"
172 mv \$dir.\$i \$dir.\$next
173 date +%c%n%s > \$dir.\$next/rotated
175 echo "Debug: skipping rotation of \$dir.\$i because \$dir.\$next already exists."
178 echo "Debug: skipping rotation of \$dir.\$i because it was created" \$(( (now-created)/86400)) "days ago ("\$(( (now-cutoff_time)/86400))" needed)."
184 max=\$((keepdaily+1))
185 if [ \( \$keepweekly -gt 0 -a -d $backuproot/daily.\$max \) -a ! -d $backuproot/weekly.1 ]; then
186 echo "Debug: daily.\$max --> weekly.1"
187 mv $backuproot/daily.\$max $backuproot/weekly.1
188 date +%c%n%s > $backuproot/weekly.1/rotated
191 max=\$((keepweekly+1))
192 if [ \( \$keepmonthly -gt 0 -a -d $backuproot/weekly.\$max \) -a ! -d $backuproot/monthly.1 ]; then
193 echo "Debug: weekly.\$max --> monthly.1"
194 mv $backuproot/weekly.\$max $backuproot/monthly.1
195 date +%c%n%s > $backuproot/monthly.1/rotated
198 for rottype in daily weekly monthly; do
199 max=\$((keep\${rottype}+1))
200 dir="$backuproot/\$rottype"
201 oldest=\`find $backuproot -type d -maxdepth 1 -name \$rottype'.*' | sed 's/^.*\.//' | sort -n | tail -1\`
202 [ "\$oldest" == "" ] && oldest=0
203 # if we've rotated the last backup off the stack, remove it.
204 for (( i=\$oldest; i >= \$max; i-- )); do
205 if [ -d \$dir.\$i ]; then
206 if [ -d $backuproot/rotate.tmp ]; then
207 echo "Debug: removing rotate.tmp"
208 rm -rf $backuproot/rotate.tmp
210 echo "Debug: moving \$rottype.\$i to rotate.tmp"
211 mv \$dir.\$i $backuproot/rotate.tmp
215 ####### END REMOTE SCRIPT #######
217 ) | (while read a; do passthru $a; done)
222 function setup_remote_dirs() {
225 local letter=${user:0:1}
226 local dir="$destdir/$letter/$user/$backuptype"
227 local tmpdir="$destdir/$letter/$user/rotate.tmp"
229 ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
230 if [ ! -d $destdir ]; then
231 echo "Fatal: Destination directory $destdir does not exist on host $desthost."
233 elif [ -d $dir.1 ]; then
234 if [ -f $dir.1/created ]; then
235 echo "Warning: $dir.1 already exists. Overwriting contents."
237 echo "Warning: we seem to be resuming a partially written $dir.1"
240 if [ -d $tmpdir ]; then
242 if [ \$? == 1 ]; then
243 echo "Fatal: could mv $destdir/rotate.tmp $dir.1 on host $desthost"
247 mkdir --parents $dir.1
248 if [ \$? == 1 ]; then
249 echo "Fatal: could not create directory $dir.1 on host $desthost"
253 if [ -d $dir.2 ]; then
254 echo "Debug: update links $backuptype.2 --> $backuptype.1"
255 cp -alf $dir.2/. $dir.1
256 #if [ \$? == 1 ]; then
257 # echo "Fatal: could not create hard links to $dir.1 on host $desthost"
262 [ -f $dir.1/created ] && rm $dir.1/created
263 [ -f $dir.1/rotated ] && rm $dir.1/rotated
266 ) | (while read a; do passthru $a; done)
268 if [ $? == 1 ]; then exit; fi
271 function start_mux() {
272 if [ "$multiconnection" == "yes" ]; then
273 debug "Starting dummy ssh connection"
274 ssh -p $destport $destuser@$desthost sleep 1d &
280 if [ "$multiconnection" == "yes" ]; then
281 debug "Stopping dummy ssh connection"
282 ssh -p $destport $destuser@$desthost pkill sleep
287 ##################################################################
289 # see if we can login
290 debug "ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
292 result=`ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1' 2>&1`
293 if [ "$result" != "1" ]; then
294 fatal "Can't connect to $desthost as $destuser."
302 status=`ssh -p $destport $destuser@$desthost "[ -d \"$destdir\" ] && echo 'ok'"`
303 if [ "$status" != "ok" ]; then
305 fatal "Destination directory $destdir doesn't exist!"
309 ### REMOVE OLD MAILDIRS ###
311 if [ "$remove" == "yes" ]; then
317 if [ "$backup" == "yes" ]; then
318 if [ $keepdaily -gt 0 ]; then btype=daily
319 elif [ $keepweekly -gt 0 ]; then btype=weekly
320 elif [ $keepmonthly -gt 0 ]; then btype=monthly
321 else fatal "keeping no backups"; fi
323 if [ "$testuser" != "" ]; then
324 cd "$srcdir/${user:0:1}"
326 setup_remote_dirs $testuser $btype
327 do_user $testuser $btype
329 for i in $letters; do
330 [ -d "$srcdir/$i" ] || fatal "directory $srcdir/$i not found."
333 for user in `ls -1`; do
334 [ "$user" != "" ] || continue
337 setup_remote_dirs $user $btype