1 ###############################################################
3 # This handler slowly creates a backup of each user's maildir
4 # to a remote server. It is designed to be run with low overhead
5 # in terms of cpu and bandwidth so it runs pretty slow.
7 # if destdir is /backup/maildir/, then it will contain the files
14 # if keepdaily is 3, keepweekly is 2, and keepmonthly is 1.
16 # The basic algorithm is to rsync each maildir individually,
17 # and to use hard links for retaining historical data.
19 # We rsync each maildir individually because it becomes very
20 # unweldy to start a single rsync of many hundreds of thousands
23 # For the backup rotation to work, destuser must be able to run
24 # arbitrary bash commands on the desthost.
26 ##############################################################
37 getconf srcdir /var/maildir
51 #getconf testuser elijah
54 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"
56 [ -d $srcdir ] || fatal "source directory $srcdir doesn't exist"
58 [ ! $test ] || testflags="--dry-run -v"
59 rsyncflags="$testflags -e 'ssh -p $destport' -r -v --ignore-existing --delete --size-only --bwlimit=$speedlimit"
60 excludes="--exclude '.Trash/\*' --exclude '.Mistakes/\*' --exclude '.Spam/\*'"
63 debug "ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
65 result=`ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1' 2>&1`
66 if [ "$result" != "1" ]; then
67 fatal "Can't connect to $desthost as $destuser."
71 ##################################################################
77 local letter=${user:0:1}
78 local dir="$srcdir/$letter/$user"
79 [ -d $dir ] || fatal "maildir $dir not found".
82 # load=`uptime | sed 's/^.*load average: \\([^,]*\\).*$/\\1/'`
83 # over=`expr $load \> $loadlimit`
84 # if [ $over == 1 ]; then
85 # info "load $load, sleeping..."
92 cmd="$RSYNC $rsyncflags $excludes $dir $destuser@$desthost:$destdir/$letter"
93 ret=`rsync -e "ssh -p $destport" -r \
94 --links --ignore-existing --delete --size-only --bwlimit=$speedlimit \
95 --exclude '.Trash/*' --exclude '.Mistakes/*' --exclude '.Spam/*' \
96 $dir $destuser@$desthost:$destdir/$letter \
99 # ignore 0 (success) and 24 (file vanished before it could be copied)
100 if [ $ret != 0 -a $ret != 24 ]; then
101 warning "rsync $user failed"
102 warning " returned: $ret"
103 let "failedcount = failedcount + 1"
104 if [ $failedcount -gt 100 ]; then
105 fatal "100 rsync errors -- something is not working right. bailing out."
110 # remove any maildirs from backup which might have been deleted
111 # and add new ones which have just been created.
113 function do_remove() {
114 local tmp1=`maketemp maildir-tmp-file`
115 local tmp2=`maketemp maildir-tmp-file`
117 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
118 ls -1 "$srcdir/$i" | sort > $tmp1
119 ssh -p $destport $desthost ls -1 '$destdir/maildir/$i' | sort > $tmp2
120 for deluser in `join -v 2 $tmp1 $tmp2`; do
121 cmd="ssh -p $destport $desthost rm -vr '$destdir/maildir/$i/$deluser/'"
129 function do_rotate() {
133 debug Connecting to $desthost
134 ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
135 ##### BEGIN REMOTE SCRIPT #####
137 seconds_weekly=604800
138 seconds_monthly=2628000
140 keepweekly=$keepweekly
141 keepmonthly=$keepmonthly
144 for rottype in daily weekly monthly; do
145 seconds=\$((seconds_\${rottype}))
147 dir="$backuproot/\$rottype"
148 if [ ! -d \$dir.1 ]; then
149 echo "Info: \$dir.1 does not exist. This backup is missing, so we are skipping the rotation."
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: mv \$dir.\$i \$dir.\$next"
172 mv \$dir.\$i \$dir.\$next
173 date +%c%n%s > \$dir.\$next/rotated
175 echo "Info: skipping rotation of \$dir.\$i because \$dir.\$next already exists."
178 echo "Info: 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 mv $backuproot/daily.\$max $backuproot/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 mv $backuproot/weekly.\$max $backuproot/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 "Info: removing $backuproot/rotate.tmp"
208 rm -rf $backuproot/rotate.tmp
210 echo "Info: moving \$dir.\$i to $backuproot/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() {
224 local dir="$destdir/$backuptype"
227 ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
228 if [ ! -d $destdir ]; then
229 echo "Fatal: Destination directory $destdir does not exist on host $desthost."
231 elif [ -d $dir.1 ]; then
232 if [ -f $dir.1/created ]; then
233 echo "Warning: $dir.1 already exists. Overwriting contents."
235 echo "Warning: we seem to be resuming a partially written $dir.1"
238 if [ -d $destdir/rotate.tmp ]; then
239 mv $destdir/rotate.tmp $dir.1
240 if [ \$? == 1 ]; then
241 echo "Fatal: could mv $destdir/rotate.tmp $dir.1 on host $desthost"
246 if [ \$? == 1 ]; then
247 echo "Fatal: could not create directory $dir.1 on host $desthost"
250 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
254 if [ -d $destdir/$backuptype.2 ]; then
255 echo "Info: updating hard links to $dir.1. This may take a while."
256 cp -alf $destdir/$backuptype.2/. $dir.1
257 #if [ \$? == 1 ]; then
258 # echo "Fatal: could not create hard links to $dir.1 on host $desthost"
263 [ -f $dir.1/created ] && rm $dir.1/created
264 [ -f $dir.1/rotated ] && rm $dir.1/rotated
267 ) | (while read a; do passthru $a; done)
269 if [ $? == 1 ]; then exit; fi
273 ##################################################################
275 ### ROTATE BACKUPS ###
277 if [ "$rotate" == "yes" ]; then
281 ### REMOVE OLD MAILDIRS ###
283 if [ "$remove" == "yes" ]; then
289 if [ "$backup" == "yes" ]; then
290 if [ $keepdaily -gt 0 ]; then btype=daily
291 elif [ $keepweekly -gt 0 ]; then btype=weekly
292 elif [ $keepmonthly -gt 0 ]; then btype=monthly
293 else fatal "keeping no backups"; fi
295 setup_remote_dirs $btype
297 for i in $letters; do
298 [ -d "$srcdir/$i" ] || fatal "directory $srcdir/$i not found."
301 for user in `ls -1`; do
302 if [ "$testuser" != "" -a "$testuser" != "$user" ]; then continue; fi
303 do_user $user $destdir/$btype.1
307 ssh -o PasswordAuthentication=no $desthost -l $destuser "date +%c%n%s > $destdir/$btype.1/created"