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.
9 # if destdir is /backup/maildir/, then it will contain the files
16 # if keepdaily is 3, keepweekly is 2, and keepmonthly is 1.
18 # The basic algorithm is to rsync each maildir individually,
19 # and to use hard links for retaining historical data.
21 # We rsync each maildir individually because it becomes very
22 # unweldy to start a single rsync of many hundreds of thousands
25 # For the backup rotation to work, destuser must be able to run
26 # arbitrary bash commands on the desthost.
28 ##############################################################
39 getconf srcdir /var/maildir
53 #getconf testuser elijah
56 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"
58 [ -d $srcdir ] || fatal "source directory $srcdir doesn't exist"
60 [ ! $test ] || testflags="--dry-run -v"
61 rsyncflags="$testflags -e 'ssh -p $destport' -r -v --ignore-existing --delete --size-only --bwlimit=$speedlimit"
62 excludes="--exclude '.Trash/\*' --exclude '.Mistakes/\*' --exclude '.Spam/\*'"
65 debug "ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
67 result=`ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1' 2>&1`
68 if [ "$result" != "1" ]; then
69 fatal "Can't connect to $desthost as $destuser."
73 ##################################################################
79 local letter=${user:0:1}
80 local dir="$srcdir/$letter/$user"
81 [ -d $dir ] || fatal "maildir $dir not found".
84 # load=`uptime | sed 's/^.*load average: \\([^,]*\\).*$/\\1/'`
85 # over=`expr $load \> $loadlimit`
86 # if [ $over == 1 ]; then
87 # info "load $load, sleeping..."
94 cmd="$RSYNC $rsyncflags $excludes $dir $destuser@$desthost:$destdir/$letter"
95 ret=`rsync -e "ssh -p $destport" -r \
96 --links --ignore-existing --delete --size-only --bwlimit=$speedlimit \
97 --exclude '.Trash/*' --exclude '.Mistakes/*' --exclude '.Spam/*' \
98 $dir $destuser@$desthost:$destdir/$letter \
101 # ignore 0 (success) and 24 (file vanished before it could be copied)
102 if [ $ret != 0 -a $ret != 24 ]; then
103 warning "rsync $user failed"
104 warning " returned: $ret"
105 let "failedcount = failedcount + 1"
106 if [ $failedcount -gt 100 ]; then
107 fatal "100 rsync errors -- something is not working right. bailing out."
112 # remove any maildirs from backup which might have been deleted
113 # and add new ones which have just been created.
115 function do_remove() {
116 local tmp1=`maketemp maildir-tmp-file`
117 local tmp2=`maketemp maildir-tmp-file`
119 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
120 ls -1 "$srcdir/$i" | sort > $tmp1
121 ssh -p $destport $desthost ls -1 '$destdir/maildir/$i' | sort > $tmp2
122 for deluser in `join -v 2 $tmp1 $tmp2`; do
123 cmd="ssh -p $destport $desthost rm -vr '$destdir/maildir/$i/$deluser/'"
131 function do_rotate() {
135 debug Connecting to $desthost
136 ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
137 ##### BEGIN REMOTE SCRIPT #####
139 seconds_weekly=604800
140 seconds_monthly=2628000
142 keepweekly=$keepweekly
143 keepmonthly=$keepmonthly
146 for rottype in daily weekly monthly; do
147 seconds=\$((seconds_\${rottype}))
149 dir="$backuproot/\$rottype"
150 if [ ! -d \$dir.1 ]; then
151 echo "Info: \$dir.1 does not exist. This backup is missing, so we are skipping the rotation."
153 elif [ ! -f \$dir.1/created ]; then
154 echo "Warning: \$dir.1/created does not exist. This backup may be only partially completed. Skipping rotation."
158 # Rotate the current list of backups, if we can.
159 oldest=\`find $backuproot -type d -maxdepth 1 -name \$rottype'.*' | sed 's/^.*\.//' | sort -n | tail -1\`
160 echo "Debug: oldest \$oldest"
161 [ "\$oldest" == "" ] && oldest=0
162 for (( i=\$oldest; i > 0; i-- )); do
163 if [ -d \$dir.\$i ]; then
164 if [ -f \$dir.\$i/created ]; then
165 created=\`tail -1 \$dir.\$i/created\`
169 cutoff_time=\$(( now - (seconds*(i-1)) ))
170 if [ ! \$created -gt \$cutoff_time ]; then
172 if [ ! -d \$dir.\$next ]; then
173 echo "Debug: mv \$dir.\$i \$dir.\$next"
174 mv \$dir.\$i \$dir.\$next
175 date +%c%n%s > \$dir.\$next/rotated
177 echo "Info: skipping rotation of \$dir.\$i because \$dir.\$next already exists."
180 echo "Info: skipping rotation of \$dir.\$i because it was created" \$(( (now-created)/86400)) "days ago ("\$(( (now-cutoff_time)/86400))" needed)."
186 max=\$((keepdaily+1))
187 if [ \( \$keepweekly -gt 0 -a -d $backuproot/daily.\$max \) -a ! -d $backuproot/weekly.1 ]; then
188 echo mv $backuproot/daily.\$max $backuproot/weekly.1
189 mv $backuproot/daily.\$max $backuproot/weekly.1
190 date +%c%n%s > $backuproot/weekly.1/rotated
193 max=\$((keepweekly+1))
194 if [ \( \$keepmonthly -gt 0 -a -d $backuproot/weekly.\$max \) -a ! -d $backuproot/monthly.1 ]; then
195 echo mv $backuproot/weekly.\$max $backuproot/monthly.1
196 mv $backuproot/weekly.\$max $backuproot/monthly.1
197 date +%c%n%s > $backuproot/monthly.1/rotated
200 for rottype in daily weekly monthly; do
201 max=\$((keep\${rottype}+1))
202 dir="$backuproot/\$rottype"
203 oldest=\`find $backuproot -type d -maxdepth 1 -name \$rottype'.*' | sed 's/^.*\.//' | sort -n | tail -1\`
204 [ "\$oldest" == "" ] && oldest=0
205 # if we've rotated the last backup off the stack, remove it.
206 for (( i=\$oldest; i >= \$max; i-- )); do
207 if [ -d \$dir.\$i ]; then
208 if [ -d $backuproot/rotate.tmp ]; then
209 echo "Info: removing $backuproot/rotate.tmp"
210 rm -rf $backuproot/rotate.tmp
212 echo "Info: moving \$dir.\$i to $backuproot/rotate.tmp"
213 mv \$dir.\$i $backuproot/rotate.tmp
217 ####### END REMOTE SCRIPT #######
219 ) | (while read a; do passthru $a; done)
224 function setup_remote_dirs() {
226 local dir="$destdir/$backuptype"
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 $destdir/rotate.tmp ]; then
241 mv $destdir/rotate.tmp $dir.1
242 if [ \$? == 1 ]; then
243 echo "Fatal: could mv $destdir/rotate.tmp $dir.1 on host $desthost"
248 if [ \$? == 1 ]; then
249 echo "Fatal: could not create directory $dir.1 on host $desthost"
252 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
256 if [ -d $destdir/$backuptype.2 ]; then
257 echo "Info: updating hard links to $dir.1. This may take a while."
258 cp -alf $destdir/$backuptype.2/. $dir.1
259 #if [ \$? == 1 ]; then
260 # echo "Fatal: could not create hard links to $dir.1 on host $desthost"
265 [ -f $dir.1/created ] && rm $dir.1/created
266 [ -f $dir.1/rotated ] && rm $dir.1/rotated
269 ) | (while read a; do passthru $a; done)
271 if [ $? == 1 ]; then exit; fi
275 ##################################################################
277 ### ROTATE BACKUPS ###
279 if [ "$rotate" == "yes" ]; then
283 ### REMOVE OLD MAILDIRS ###
285 if [ "$remove" == "yes" ]; then
291 if [ "$backup" == "yes" ]; then
292 if [ $keepdaily -gt 0 ]; then btype=daily
293 elif [ $keepweekly -gt 0 ]; then btype=weekly
294 elif [ $keepmonthly -gt 0 ]; then btype=monthly
295 else fatal "keeping no backups"; fi
297 setup_remote_dirs $btype
299 for i in $letters; do
300 [ -d "$srcdir/$i" ] || fatal "directory $srcdir/$i not found."
303 for user in `ls -1`; do
304 if [ "$testuser" != "" -a "$testuser" != "$user" ]; then continue; fi
305 do_user $user $destdir/$btype.1
309 ssh -o PasswordAuthentication=no $desthost -l $destuser "date +%c%n%s > $destdir/$btype.1/created"