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="0 1 2 3 4 5 6 7 8 9 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"
61 [ -d $srcdir ] || fatal "source directory $srcdir doesn't exist"
63 [ "$multiconnection" == "notset" ] && fatal "The maildir handler uses a very different destination format. See the example .maildir for more information"
66 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 if [ ! -d $source ]; then
82 warning "maildir $source not found"
87 ret=`$RSYNC -e "ssh -p $destport" -r \
88 --links --ignore-existing --delete --size-only --bwlimit=$speedlimit \
89 --exclude '.Trash/*' --exclude '.Mistakes/*' --exclude '.Spam/*' \
90 $source $destuser@$desthost:$target \
93 # ignore 0 (success) and 24 (file vanished before it could be copied)
94 if [ $ret != 0 -a $ret != 24 ]; then
95 warning "rsync $user failed"
96 warning " returned: $ret"
97 let "failedcount = failedcount + 1"
98 if [ $failedcount -gt 100 ]; then
99 fatal "100 rsync errors -- something is not working right. bailing out."
102 ssh -o PasswordAuthentication=no $desthost -l $destuser "date +%c%n%s > $target/created"
105 # remove any maildirs from backup which might have been deleted
106 # and add new ones which have just been created.
107 # (actually, it just moved them to the directory "deleted")
109 function do_remove() {
110 local tmp1=`maketemp maildir-tmp-file`
111 local tmp2=`maketemp maildir-tmp-file`
113 ssh -p $destport $destuser@$desthost mkdir -p "$destdir/deleted"
114 for i in 0 1 2 3 4 5 6 7 8 9 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
115 ls -1 "$srcdir/$i/" | sort > $tmp1
116 ssh -p $destport $destuser@$desthost ls -1 "$destdir/$i/" | sort > $tmp2
117 for deluser in `join -v 2 $tmp1 $tmp2`; do
118 [ "$deluser" != "" ] || continue
119 info "removing $destuser@$desthost:$destdir/$i/$deluser/"
120 ssh -p $destport $destuser@$desthost mv "$destdir/$i/$deluser/" "$destdir/deleted"
121 ssh -p $destport $destuser@$desthost "date > '$destdir/$i/$deluser/deleted_on'"
128 function do_rotate() {
129 [ "$rotate" == "yes" ] || return;
131 local letter=${user:0:1}
132 local backuproot="$destdir/$letter/$user"
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 if [ ! -d "$backuproot" ]; then
145 echo "Debug: skipping rotate of $user. $backuproot doesn't exist."
148 for rottype in daily weekly monthly; do
149 seconds=\$((seconds_\${rottype}))
151 dir="$backuproot/\$rottype"
152 if [ ! -d \$dir.1 ]; then
153 echo "Debug: \$dir.1 does not exist, skipping."
155 elif [ ! -f \$dir.1/created ]; then
156 echo "Warning: \$dir.1/created does not exist. This backup may be only partially completed. Skipping rotation."
160 # Rotate the current list of backups, if we can.
161 oldest=\`find $backuproot -maxdepth 1 -type d -name \$rottype'.*' | @SED@ 's/^.*\.//' | sort -n | tail -1\`
162 #echo "Debug: oldest \$oldest"
163 [ "\$oldest" == "" ] && oldest=0
164 for (( i=\$oldest; i > 0; i-- )); do
165 if [ -d \$dir.\$i ]; then
166 if [ -f \$dir.\$i/created ]; then
167 created=\`tail -1 \$dir.\$i/created\`
171 cutoff_time=\$(( now - (seconds*(i-1)) ))
172 if [ ! \$created -gt \$cutoff_time ]; then
174 if [ ! -d \$dir.\$next ]; then
175 echo "Debug: \$rottype.\$i --> \$rottype.\$next"
176 mv \$dir.\$i \$dir.\$next
177 date +%c%n%s > \$dir.\$next/rotated
179 echo "Debug: skipping rotation of \$dir.\$i because \$dir.\$next already exists."
182 echo "Debug: skipping rotation of \$dir.\$i because it was created" \$(( (now-created)/86400)) "days ago ("\$(( (now-cutoff_time)/86400))" needed)."
188 max=\$((keepdaily+1))
189 if [ \( \$keepweekly -gt 0 -a -d $backuproot/daily.\$max \) -a ! -d $backuproot/weekly.1 ]; then
190 echo "Debug: daily.\$max --> weekly.1"
191 mv $backuproot/daily.\$max $backuproot/weekly.1
192 date +%c%n%s > $backuproot/weekly.1/rotated
195 max=\$((keepweekly+1))
196 if [ \( \$keepmonthly -gt 0 -a -d $backuproot/weekly.\$max \) -a ! -d $backuproot/monthly.1 ]; then
197 echo "Debug: weekly.\$max --> monthly.1"
198 mv $backuproot/weekly.\$max $backuproot/monthly.1
199 date +%c%n%s > $backuproot/monthly.1/rotated
202 for rottype in daily weekly monthly; do
203 max=\$((keep\${rottype}+1))
204 dir="$backuproot/\$rottype"
205 oldest=\`find $backuproot -maxdepth 1 -type d -name \$rottype'.*' | @SED@ 's/^.*\.//' | sort -n | tail -1\`
206 [ "\$oldest" == "" ] && oldest=0
207 # if we've rotated the last backup off the stack, remove it.
208 for (( i=\$oldest; i >= \$max; i-- )); do
209 if [ -d \$dir.\$i ]; then
210 if [ -d $backuproot/rotate.tmp ]; then
211 echo "Debug: removing rotate.tmp"
212 rm -rf $backuproot/rotate.tmp
214 echo "Debug: moving \$rottype.\$i to rotate.tmp"
215 mv \$dir.\$i $backuproot/rotate.tmp
219 ####### END REMOTE SCRIPT #######
221 ) | (while read a; do passthru $a; done)
226 function setup_remote_dirs() {
229 local letter=${user:0:1}
230 local dir="$destdir/$letter/$user/$backuptype"
231 local tmpdir="$destdir/$letter/$user/rotate.tmp"
233 ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
234 if [ ! -d $destdir ]; then
235 echo "Fatal: Destination directory $destdir does not exist on host $desthost."
237 elif [ -d $dir.1 ]; then
238 if [ -f $dir.1/created ]; then
239 echo "Warning: $dir.1 already exists. Overwriting contents."
241 echo "Warning: we seem to be resuming a partially written $dir.1"
244 if [ -d $tmpdir ]; then
246 if [ \$? == 1 ]; then
247 echo "Fatal: could mv $destdir/rotate.tmp $dir.1 on host $desthost"
251 mkdir --parents $dir.1
252 if [ \$? == 1 ]; then
253 echo "Fatal: could not create directory $dir.1 on host $desthost"
257 if [ -d $dir.2 ]; then
258 echo "Debug: update links $backuptype.2 --> $backuptype.1"
259 cp -alf $dir.2/. $dir.1
260 #if [ \$? == 1 ]; then
261 # echo "Fatal: could not create hard links to $dir.1 on host $desthost"
266 [ -f $dir.1/created ] && rm $dir.1/created
267 [ -f $dir.1/rotated ] && rm $dir.1/rotated
270 ) | (while read a; do passthru $a; done)
272 if [ $? == 1 ]; then exit; fi
275 function start_mux() {
276 if [ "$multiconnection" == "yes" ]; then
277 debug "Starting dummy ssh connection"
278 ssh -p $destport $destuser@$desthost sleep 1d &
284 if [ "$multiconnection" == "yes" ]; then
285 debug "Stopping dummy ssh connection"
286 ssh -p $destport $destuser@$desthost pkill sleep
291 ##################################################################
293 # see if we can login
294 debug "ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
296 result=`ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1' 2>&1`
297 if [ "$result" != "1" ]; then
298 fatal "Can't connect to $desthost as $destuser."
306 status=`ssh -p $destport $destuser@$desthost "[ -d \"$destdir\" ] && echo 'ok'"`
307 if [ "$status" != "ok" ]; then
309 fatal "Destination directory $destdir doesn't exist!"
313 ### REMOVE OLD MAILDIRS ###
315 if [ "$remove" == "yes" ]; then
321 if [ "$backup" == "yes" ]; then
322 if [ $keepdaily -gt 0 ]; then btype=daily
323 elif [ $keepweekly -gt 0 ]; then btype=weekly
324 elif [ $keepmonthly -gt 0 ]; then btype=monthly
325 else fatal "keeping no backups"; fi
327 if [ "$testuser" != "" ]; then
328 cd "$srcdir/${user:0:1}"
330 setup_remote_dirs $testuser $btype
331 do_user $testuser $btype
333 for i in $letters; do
334 [ -d "$srcdir/$i" ] || fatal "directory $srcdir/$i not found."
337 for user in `ls -1`; do
338 [ "$user" != "" ] || continue
341 setup_remote_dirs $user $btype