1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 ###############################################################
4 # This handler slowly creates a backup of each user's maildir
5 # to a remote server. It is designed to be run with low overhead
6 # in terms of cpu and bandwidth so it runs pretty slow.
8 # if destdir is /backup/maildir/, then it will contain the files
15 # if keepdaily is 3, keepweekly is 2, and keepmonthly is 1.
17 # The basic algorithm is to rsync each maildir individually,
18 # and to use hard links for retaining historical data.
20 # We rsync each maildir individually because it becomes very
21 # unweldy to start a single rsync of many hundreds of thousands
24 # For the backup rotation to work, destuser must be able to run
25 # arbitrary bash commands on the desthost.
27 ##############################################################
38 getconf srcdir /var/maildir
52 #getconf testuser elijah
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"
57 [ -d $srcdir ] || fatal "source directory $srcdir doesn't exist"
59 [ ! $test ] || testflags="--dry-run -v"
60 rsyncflags="$testflags -e 'ssh -p $destport' -r -v --ignore-existing --delete --size-only --bwlimit=$speedlimit"
61 excludes="--exclude '.Trash/\*' --exclude '.Mistakes/\*' --exclude '.Spam/\*'"
64 debug "ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
66 result=`ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1' 2>&1`
67 if [ "$result" != "1" ]; then
68 fatal "Can't connect to $desthost as $destuser."
72 ##################################################################
78 local letter=${user:0:1}
79 local dir="$srcdir/$letter/$user"
80 [ -d $dir ] || fatal "maildir $dir not found".
83 # load=`uptime | sed 's/^.*load average: \\([^,]*\\).*$/\\1/'`
84 # over=`expr $load \> $loadlimit`
85 # if [ $over == 1 ]; then
86 # info "load $load, sleeping..."
93 cmd="$RSYNC $rsyncflags $excludes $dir $destuser@$desthost:$destdir/$letter"
94 ret=`rsync -e "ssh -p $destport" -r \
95 --links --ignore-existing --delete --size-only --bwlimit=$speedlimit \
96 --exclude '.Trash/*' --exclude '.Mistakes/*' --exclude '.Spam/*' \
97 $dir $destuser@$desthost:$destdir/$letter \
100 # ignore 0 (success) and 24 (file vanished before it could be copied)
101 if [ $ret != 0 -a $ret != 24 ]; then
102 warning "rsync $user failed"
103 warning " returned: $ret"
104 let "failedcount = failedcount + 1"
105 if [ $failedcount -gt 100 ]; then
106 fatal "100 rsync errors -- something is not working right. bailing out."
111 # remove any maildirs from backup which might have been deleted
112 # and add new ones which have just been created.
114 function do_remove() {
115 local tmp1=`maketemp maildir-tmp-file`
116 local tmp2=`maketemp maildir-tmp-file`
118 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
119 ls -1 "$srcdir/$i" | sort > $tmp1
120 ssh -p $destport $desthost ls -1 '$destdir/maildir/$i' | sort > $tmp2
121 for deluser in `join -v 2 $tmp1 $tmp2`; do
122 cmd="ssh -p $destport $desthost rm -vr '$destdir/maildir/$i/$deluser/'"
130 function do_rotate() {
134 debug Connecting to $desthost
135 ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
136 ##### BEGIN REMOTE SCRIPT #####
138 seconds_weekly=604800
139 seconds_monthly=2628000
141 keepweekly=$keepweekly
142 keepmonthly=$keepmonthly
145 for rottype in daily weekly monthly; do
146 seconds=\$((seconds_\${rottype}))
148 dir="$backuproot/\$rottype"
149 if [ ! -d \$dir.1 ]; then
150 echo "Info: \$dir.1 does not exist. This backup is missing, so we are skipping the rotation."
152 elif [ ! -f \$dir.1/created ]; then
153 echo "Warning: \$dir.1/created does not exist. This backup may be only partially completed. Skipping rotation."
157 # Rotate the current list of backups, if we can.
158 oldest=\`find $backuproot -type d -maxdepth 1 -name \$rottype'.*' | sed 's/^.*\.//' | sort -n | tail -1\`
159 echo "Debug: oldest \$oldest"
160 [ "\$oldest" == "" ] && oldest=0
161 for (( i=\$oldest; i > 0; i-- )); do
162 if [ -d \$dir.\$i ]; then
163 if [ -f \$dir.\$i/created ]; then
164 created=\`tail -1 \$dir.\$i/created\`
168 cutoff_time=\$(( now - (seconds*(i-1)) ))
169 if [ ! \$created -gt \$cutoff_time ]; then
171 if [ ! -d \$dir.\$next ]; then
172 echo "Debug: mv \$dir.\$i \$dir.\$next"
173 mv \$dir.\$i \$dir.\$next
174 date +%c%n%s > \$dir.\$next/rotated
176 echo "Info: skipping rotation of \$dir.\$i because \$dir.\$next already exists."
179 echo "Info: skipping rotation of \$dir.\$i because it was created" \$(( (now-created)/86400)) "days ago ("\$(( (now-cutoff_time)/86400))" needed)."
185 max=\$((keepdaily+1))
186 if [ \( \$keepweekly -gt 0 -a -d $backuproot/daily.\$max \) -a ! -d $backuproot/weekly.1 ]; then
187 echo mv $backuproot/daily.\$max $backuproot/weekly.1
188 mv $backuproot/daily.\$max $backuproot/weekly.1
189 date +%c%n%s > $backuproot/weekly.1/rotated
192 max=\$((keepweekly+1))
193 if [ \( \$keepmonthly -gt 0 -a -d $backuproot/weekly.\$max \) -a ! -d $backuproot/monthly.1 ]; then
194 echo mv $backuproot/weekly.\$max $backuproot/monthly.1
195 mv $backuproot/weekly.\$max $backuproot/monthly.1
196 date +%c%n%s > $backuproot/monthly.1/rotated
199 for rottype in daily weekly monthly; do
200 max=\$((keep\${rottype}+1))
201 dir="$backuproot/\$rottype"
202 oldest=\`find $backuproot -type d -maxdepth 1 -name \$rottype'.*' | sed 's/^.*\.//' | sort -n | tail -1\`
203 [ "\$oldest" == "" ] && oldest=0
204 # if we've rotated the last backup off the stack, remove it.
205 for (( i=\$oldest; i >= \$max; i-- )); do
206 if [ -d \$dir.\$i ]; then
207 if [ -d $backuproot/rotate.tmp ]; then
208 echo "Info: removing $backuproot/rotate.tmp"
209 rm -rf $backuproot/rotate.tmp
211 echo "Info: moving \$dir.\$i to $backuproot/rotate.tmp"
212 mv \$dir.\$i $backuproot/rotate.tmp
216 ####### END REMOTE SCRIPT #######
218 ) | (while read a; do passthru $a; done)
223 function setup_remote_dirs() {
225 local dir="$destdir/$backuptype"
228 ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
229 if [ ! -d $destdir ]; then
230 echo "Fatal: Destination directory $destdir does not exist on host $desthost."
232 elif [ -d $dir.1 ]; then
233 if [ -f $dir.1/created ]; then
234 echo "Warning: $dir.1 already exists. Overwriting contents."
236 echo "Warning: we seem to be resuming a partially written $dir.1"
239 if [ -d $destdir/rotate.tmp ]; then
240 mv $destdir/rotate.tmp $dir.1
241 if [ \$? == 1 ]; then
242 echo "Fatal: could mv $destdir/rotate.tmp $dir.1 on host $desthost"
247 if [ \$? == 1 ]; then
248 echo "Fatal: could not create directory $dir.1 on host $desthost"
251 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 y x z; do
255 if [ -d $destdir/$backuptype.2 ]; then
256 echo "Info: updating hard links to $dir.1. This may take a while."
257 cp -alf $destdir/$backuptype.2/. $dir.1
258 #if [ \$? == 1 ]; then
259 # echo "Fatal: could not create hard links to $dir.1 on host $desthost"
264 [ -f $dir.1/created ] && rm $dir.1/created
265 [ -f $dir.1/rotated ] && rm $dir.1/rotated
268 ) | (while read a; do passthru $a; done)
270 if [ $? == 1 ]; then exit; fi
274 ##################################################################
276 ### ROTATE BACKUPS ###
278 if [ "$rotate" == "yes" ]; then
282 ### REMOVE OLD MAILDIRS ###
284 if [ "$remove" == "yes" ]; then
290 if [ "$backup" == "yes" ]; then
291 if [ $keepdaily -gt 0 ]; then btype=daily
292 elif [ $keepweekly -gt 0 ]; then btype=weekly
293 elif [ $keepmonthly -gt 0 ]; then btype=monthly
294 else fatal "keeping no backups"; fi
296 setup_remote_dirs $btype
298 for i in $letters; do
299 [ -d "$srcdir/$i" ] || fatal "directory $srcdir/$i not found."
302 for user in `ls -1`; do
303 if [ "$testuser" != "" -a "$testuser" != "$user" ]; then continue; fi
304 do_user $user $destdir/$btype.1
308 ssh -o PasswordAuthentication=no $desthost -l $destuser "date +%c%n%s > $destdir/$btype.1/created"