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 ##############################################################
27 getconf srcdir /var/maildir
41 #getconf testuser elijah
44 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"
46 [ -d $srcdir ] || fatal "source directory $srcdir doesn't exist"
48 [ ! $test ] || testflags="--dry-run -v"
49 rsyncflags="$testflags -e 'ssh -p $destport' -r -v --ignore-existing --delete --size-only --bwlimit=$speedlimit"
50 excludes="--exclude '.Trash/\*' --exclude '.Mistakes/\*' --exclude '.Spam/\*'"
53 debug "ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
55 result=`ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1' 2>&1`
56 if [ "$result" != "1" ]; then
57 fatal "Can't connect to $desthost as $destuser."
61 ##################################################################
67 local letter=${user:0:1}
68 local dir="$srcdir/$letter/$user"
69 [ -d $dir ] || fatal "maildir $dir not found".
72 # load=`uptime | sed 's/^.*load average: \\([^,]*\\).*$/\\1/'`
73 # over=`expr $load \> $loadlimit`
74 # if [ $over == 1 ]; then
75 # info "load $load, sleeping..."
82 cmd="$RSYNC $rsyncflags $excludes $dir $destuser@$desthost:$destdir/$letter"
83 ret=`rsync -e "ssh -p $destport" -r \
84 --links --ignore-existing --delete --size-only --bwlimit=$speedlimit \
85 --exclude '.Trash/*' --exclude '.Mistakes/*' --exclude '.Spam/*' \
86 $dir $destuser@$desthost:$destdir/$letter \
89 warning "rsync $user failed"
90 warning " returned: $ret"
91 let "failedcount = failedcount + 1"
92 if [ $failedcount -gt 100 ]; then
93 fatal "100 rsync errors -- something is not working right. bailing out."
98 # remove any maildirs from backup which might have been deleted
99 # and add new ones which have just been created.
101 function do_remove() {
102 local tmp1=/tmp/maildirtmpfile$$
103 local tmp2=/tmp/maildirtmpfile$$
105 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
106 ls -1 "$srcdir/$i" | sort > $tmp1
107 ssh -p $destport $desthost ls -1 '$destdir/maildir/$i' | sort > $tmp2
108 for deluser in `join -v 2 $tmp1 $tmp2`; do
109 cmd="ssh -p $destport $desthost rm -vr '$destdir/maildir/$i/$deluser/'"
117 function do_rotate() {
121 debug Connecting to $desthost
122 ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
123 ##### BEGIN REMOTE SCRIPT #####
125 seconds_weekly=604800
126 seconds_monthly=2628000
128 keepweekly=$keepweekly
129 keepmonthly=$keepmonthly
132 for rottype in daily weekly monthly; do
133 seconds=\$((seconds_\${rottype}))
135 dir="$backuproot/\$rottype"
136 if [ ! -d \$dir.1 ]; then
137 echo "Info: \$dir.1 does not exist. This backup is missing, so we are skipping the rotation."
139 elif [ ! -f \$dir.1/created ]; then
140 echo "Warning: \$dir.1/created does not exist. This backup may be only partially completed. Skipping rotation."
144 # Rotate the current list of backups, if we can.
145 oldest=\`find $backuproot -type d -maxdepth 1 -name \$rottype'.*' | sed 's/^.*\.//' | sort -n | tail -1\`
146 echo "Debug: oldest \$oldest"
147 [ "\$oldest" == "" ] && oldest=0
148 for (( i=\$oldest; i > 0; i-- )); do
149 if [ -d \$dir.\$i ]; then
150 if [ -f \$dir.\$i/rotated ]; then
151 rotated=\`tail -1 \$dir.\$i/rotated\`
155 cutoff_time=\$(( now - (seconds*i) ))
156 if [ \$rotated -lt \$cutoff_time ]; then
158 if [ ! -d \$dir.\$next ]; then
159 echo "mv \$dir.\$i \$dir.\$next"
160 mv \$dir.\$i \$dir.\$next
161 date +%c%n%s > \$dir.\$next/rotated
163 echo "Info: skipping rotation of \$dir.\$i because \$dir.\$next already exists."
166 echo "Info: skipping rotation of \$dir.\$i because it was rotated" \$(( (now-rotated)/86400)) "days ago ("\$(( (now-cutoff_time)/86400))" needed)."
172 max=\$((keepdaily+1))
173 if [ \( \$keepweekly -gt 0 -a -d $backuproot/daily.\$max \) -a ! -d $backuproot/weekly.1 ]; then
174 echo mv $backuproot/daily.\$max $backuproot/weekly.1
175 mv $backuproot/daily.\$max $backuproot/weekly.1
176 date +%c%n%s > $backuproot/weekly.1/rotated
179 max=\$((keepweekly+1))
180 if [ \( \$keepmonthly -gt 0 -a -d $backuproot/weekly.\$max \) -a ! -d $backuproot/monthly.1 ]; then
181 echo mv $backuproot/weekly.\$max $backuproot/monthly.1
182 mv $backuproot/weekly.\$max $backuproot/monthly.1
183 date +%c%n%s > $backuproot/monthly.1/rotated
186 for rottype in daily weekly monthly; do
187 max=\$((keep\${rottype}+1))
188 dir="$backuproot/\$rottype"
189 oldest=\`find $backuproot -type d -maxdepth 1 -name \$rottype'.*' | sed 's/^.*\.//' | sort -n | tail -1\`
190 [ "\$oldest" == "" ] && oldest=0
191 # if we've rotated the last backup off the stack, remove it.
192 for (( i=\$oldest; i >= \$max; i-- )); do
193 if [ -d \$dir.\$i ]; then
194 if [ -d $backuproot/rotate.tmp ]; then
195 echo "Info: removing $backuproot/rotate.tmp"
196 rm -rf $backuproot/rotate.tmp
198 echo "Info: moving \$dir.\$i to $backuproot/rotate.tmp"
199 mv \$dir.\$i $backuproot/rotate.tmp
204 ####### END REMOTE SCRIPT #######
206 ) | (while read a; do passthru $a; done)
211 function setup_remote_dirs() {
213 local dir="$destdir/$backuptype"
216 ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
217 if [ ! -d $destdir ]; then
218 echo "Fatal: Destination directory $destdir does not exist on host $desthost."
220 elif [ -d $dir.1 ]; then
221 if [ -f $dir.1/created ]; then
222 echo "Warning: $dir.1 already exists. Overwriting contents."
224 echo "Warning: we seem to be resuming a partially written $dir.1"
227 if [ -d $destdir/rotate.tmp ]; then
228 mv $destdir/rotate.tmp $dir.1
229 if [ \$? == 1 ]; then
230 echo "Fatal: could mv $destdir/rotate.tmp $dir.1 on host $desthost"
235 if [ \$? == 1 ]; then
236 echo "Fatal: could not create directory $dir.1 on host $desthost"
240 if [ -d $destdir/$backuptype.2 ]; then
241 echo "Info: updating hard links to $dir.1. This may take a while."
242 cp -al $destdir/$backuptype.2/. $dir.1
243 if [ \$? == 1 ]; then
244 echo "Fatal: could not create hard links to $dir.1 on host $desthost"
249 [ -f $dir.1/created ] && rm $dir.1/created
250 [ -f $dir.1/rotated ] && rm $dir.1/rotated
253 ) | (while read a; do passthru $a; done)
255 if [ $? == 1 ]; then exit; fi
259 ##################################################################
261 ### ROTATE BACKUPS ###
263 if [ "$rotate" == "yes" ]; then
267 ### REMOVE OLD MAILDIRS ###
269 if [ "$remove" == "yes" ]; then
275 if [ "$backup" == "yes" ]; then
276 if [ $keepdaily -gt 0 ]; then btype=daily
277 elif [ $keepweekly -gt 0 ]; then btype=weekly
278 elif [ $keepmonthly -gt 0 ]; then btype=monthly
279 else fatal "keeping no backups"; fi
281 setup_remote_dirs $btype
283 for i in $letters; do
284 [ -d "$srcdir/$i" ] || fatal "directory $srcdir/$i not found."
287 for user in `ls -1`; do
288 if [ "$testuser" != "" -a "$testuser" != "$user" ]; then continue; fi
289 do_user $user $destdir/$btype.1
293 ssh -o PasswordAuthentication=no $desthost -l $destuser "date +%c%n%s > $destdir/$btype.1/created"