1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
4 ###############################################################
6 # This handler slowly creates a backup of each user's maildir
7 # to a remote server. It is designed to be run with low overhead
8 # in terms of cpu and bandwidth so it runs pretty slow.
9 # Hardlinking is used to save storage space.
11 # This handler expects that your maildir directory structure is
12 # either one of the following:
14 # 1. /$srcdir/[a-zA-Z0-9]/$user for example:
15 # /var/maildir/a/anarchist
16 # /var/maildir/a/arthur
18 # /var/maildir/Z/Zaphod
19 # /var/maildir/Z/Zebra
21 # 2. or the following:
22 # /var/maildir/domain.org/user1
23 # /var/maildir/domain.org/user2
25 # /var/maildir/anotherdomain.org/user1
26 # /var/maildir/anotherdomain.org/user2
29 # if the configuration is setup to have keepdaily at 3,
30 # keepweekly is 2, and keepmonthly is 1, then each user's
31 # maildir backup snapshot directory will contain these files:
39 # The basic algorithm is to rsync each maildir individually,
40 # and to use hard links for retaining historical data.
42 # We handle each maildir individually because it becomes very
43 # unweldy to hardlink and rsync many hundreds of thousands
44 # of files at once. It is much faster to take on smaller
47 # For the backup rotation to work, destuser must be able to run
48 # arbitrary bash commands on the desthost.
50 # Any maildir which is deleted from the source will be moved to
51 # "deleted" directory in the destination. It is up to you to
52 # periodically remove this directory or old maildirs in it.
54 ##############################################################
66 getconf srcdir /var/maildir
71 getconf destid_file /root/.ssh/id_rsa
73 getconf multiconnection notset
80 [ -d $srcdir ] || fatal "source directory $srcdir doesn't exist"
82 [ "$multiconnection" == "notset" ] && fatal "The maildir handler uses a very different destination format. See the example .maildir for more information"
85 testflags="--dry-run -v"
88 rsyncflags="$testflags -e 'ssh -p $destport -i $destid_file' -r -v --ignore-existing --delete --size-only --bwlimit=$speedlimit"
89 excludes="--exclude '.Trash/\*' --exclude '.Mistakes/\*' --exclude '.Spam/\*'"
91 ##################################################################
98 local source="$srcdir/$userdir/$user/"
99 local target="$destdir/$userdir/$user/$btype.1"
100 if [ ! -d $source ]; then
101 warning "maildir $source not found"
106 ret=`$RSYNC -e "ssh -p $destport -i $destid_file" -r \
107 --links --ignore-existing --delete --size-only --bwlimit=$speedlimit \
108 --exclude '.Trash/*' --exclude '.Mistakes/*' --exclude '.Spam/*' \
109 $source $destuser@$desthost:$target \
112 # ignore 0 (success) and 24 (file vanished before it could be copied)
113 if [ $ret != 0 -a $ret != 24 ]; then
114 warning "rsync $user failed"
115 warning " returned: $ret"
116 let "failedcount = failedcount + 1"
117 if [ $failedcount -gt 100 ]; then
118 fatal "100 rsync errors -- something is not working right. bailing out."
121 ssh -o PasswordAuthentication=no $desthost -l $destuser -i $destid_file "date +%c%n%s > $target/created"
124 # remove any maildirs from backup which might have been deleted
125 # and add new ones which have just been created.
126 # (actually, it just moved them to the directory "deleted")
128 function do_remove() {
129 local tmp1=`maketemp maildir-tmp-file`
130 local tmp2=`maketemp maildir-tmp-file`
132 ssh -p $destport -i $destid_file $destuser@$desthost mkdir -p "$destdir/deleted"
134 for userdir in `ls -d1 */`; do
135 ls -1 "$srcdir/$userdir" | sort > $tmp1
136 ssh -p $destport -i $destid_file $destuser@$desthost ls -1 "$destdir/$userdir" | sort > $tmp2
137 for deluser in `join -v 2 $tmp1 $tmp2`; do
138 [ "$deluser" != "" ] || continue
139 info "removing $destuser@$desthost:$destdir/$userdir$deluser/"
140 ssh -p $destport -i $destid_file $destuser@$desthost mv "$destdir/$userdir$deluser/" "$destdir/deleted"
141 ssh -p $destport -i $destid_file $destuser@$desthost "date +%c%n%s > '$destdir/deleted/$deluser/deleted_on'"
148 function do_rotate() {
149 [ "$rotate" == "yes" ] || return;
152 local backuproot="$destdir/$userdir/$user"
154 ssh -T -o PasswordAuthentication=no $desthost -l $destuser -i $destid_file <<EOF
155 ##### BEGIN REMOTE SCRIPT #####
157 seconds_weekly=604800
158 seconds_monthly=2628000
160 keepweekly=$keepweekly
161 keepmonthly=$keepmonthly
164 if [ ! -d "$backuproot" ]; then
165 echo "Debug: skipping rotate of $user. $backuproot doesn't exist."
168 for rottype in daily weekly monthly; do
169 seconds=\$((seconds_\${rottype}))
171 dir="$backuproot/\$rottype"
172 if [ ! -d \$dir.1 ]; then
173 echo "Debug: \$dir.1 does not exist, skipping."
175 elif [ ! -f \$dir.1/created ]; then
176 echo "Warning: \$dir.1/created does not exist. This backup may be only partially completed. Skipping rotation."
180 # Rotate the current list of backups, if we can.
181 oldest=\`find $backuproot -maxdepth 1 -type d -name \$rottype'.*' | @SED@ 's/^.*\.//' | sort -n | tail -1\`
182 #echo "Debug: oldest \$oldest"
183 [ "\$oldest" == "" ] && oldest=0
184 for (( i=\$oldest; i > 0; i-- )); do
185 if [ -d \$dir.\$i ]; then
186 if [ -f \$dir.\$i/created ]; then
187 created=\`tail -1 \$dir.\$i/created\`
191 cutoff_time=\$(( now - (seconds*(i-1)) ))
192 if [ ! \$created -gt \$cutoff_time ]; then
194 if [ ! -d \$dir.\$next ]; then
195 echo "Debug: \$rottype.\$i --> \$rottype.\$next"
196 mv \$dir.\$i \$dir.\$next
197 date +%c%n%s > \$dir.\$next/rotated
199 echo "Debug: skipping rotation of \$dir.\$i because \$dir.\$next already exists."
202 echo "Debug: skipping rotation of \$dir.\$i because it was created" \$(( (now-created)/86400)) "days ago ("\$(( (now-cutoff_time)/86400))" needed)."
208 max=\$((keepdaily+1))
209 if [ \( \$keepweekly -gt 0 -a -d $backuproot/daily.\$max \) -a ! -d $backuproot/weekly.1 ]; then
210 echo "Debug: daily.\$max --> weekly.1"
211 mv $backuproot/daily.\$max $backuproot/weekly.1
212 date +%c%n%s > $backuproot/weekly.1/rotated
215 max=\$((keepweekly+1))
216 if [ \( \$keepmonthly -gt 0 -a -d $backuproot/weekly.\$max \) -a ! -d $backuproot/monthly.1 ]; then
217 echo "Debug: weekly.\$max --> monthly.1"
218 mv $backuproot/weekly.\$max $backuproot/monthly.1
219 date +%c%n%s > $backuproot/monthly.1/rotated
222 for rottype in daily weekly monthly; do
223 max=\$((keep\${rottype}+1))
224 dir="$backuproot/\$rottype"
225 oldest=\`find $backuproot -maxdepth 1 -type d -name \$rottype'.*' | @SED@ 's/^.*\.//' | sort -n | tail -1\`
226 [ "\$oldest" == "" ] && oldest=0
227 # if we've rotated the last backup off the stack, remove it.
228 for (( i=\$oldest; i >= \$max; i-- )); do
229 if [ -d \$dir.\$i ]; then
230 if [ -d $backuproot/rotate.tmp ]; then
231 echo "Debug: removing rotate.tmp"
232 rm -rf $backuproot/rotate.tmp
234 echo "Debug: moving \$rottype.\$i to rotate.tmp"
235 mv \$dir.\$i $backuproot/rotate.tmp
239 ####### END REMOTE SCRIPT #######
241 ) | (while read a; do passthru $a; done)
246 function setup_remote_dirs() {
250 local dir="$destdir/$userdir/$user/$backuptype"
251 local tmpdir="$destdir/$userdir/$user/rotate.tmp"
253 ssh -T -o PasswordAuthentication=no $desthost -l $destuser -i $destid_file <<EOF
254 if [ ! -d $destdir ]; then
255 echo "Fatal: Destination directory $destdir does not exist on host $desthost."
257 elif [ -d $dir.1 ]; then
258 if [ -f $dir.1/created ]; then
259 echo "Warning: $dir.1 already exists. Overwriting contents."
261 echo "Warning: we seem to be resuming a partially written $dir.1"
264 if [ -d $tmpdir ]; then
266 if [ \$? == 1 ]; then
267 echo "Fatal: could mv $destdir/rotate.tmp $dir.1 on host $desthost"
271 mkdir --parents $dir.1
272 if [ \$? == 1 ]; then
273 echo "Fatal: could not create directory $dir.1 on host $desthost"
277 if [ -d $dir.2 ]; then
278 echo "Debug: update links $backuptype.2 --> $backuptype.1"
279 cp -alf $dir.2/. $dir.1
280 #if [ \$? == 1 ]; then
281 # echo "Fatal: could not create hard links to $dir.1 on host $desthost"
286 [ -f $dir.1/created ] && rm $dir.1/created
287 [ -f $dir.1/rotated ] && rm $dir.1/rotated
290 ) | (while read a; do passthru $a; done)
292 if [ $? == 1 ]; then exit; fi
295 function start_mux() {
296 if [ "$multiconnection" == "yes" ]; then
297 debug "Starting dummy ssh connection"
298 ssh -p $destport -i $destid_file $destuser@$desthost sleep 1d &
304 if [ "$multiconnection" == "yes" ]; then
305 debug "Stopping dummy ssh connection"
306 ssh -p $destport -i $destid_file $destuser@$desthost pkill sleep
311 ##################################################################
313 # see if we can login
314 debug "ssh -o PasswordAuthentication=no $desthost -l $destuser -i $destid_file 'echo -n 1'"
316 result=`ssh -o PasswordAuthentication=no $desthost -l $destuser -i $destid_file 'echo -n 1' 2>&1`
317 if [ "$result" != "1" ]; then
318 fatal "Can't connect to $desthost as $destuser using $destid_file."
326 status=`ssh -p $destport -i $destid_file $destuser@$desthost "[ -d \"$destdir\" ] && echo 'ok'"`
327 if [ "$status" != "ok" ]; then
329 fatal "Destination directory $destdir doesn't exist!"
333 ### REMOVE OLD MAILDIRS ###
335 if [ "$remove" == "yes" ]; then
341 if [ "$backup" == "yes" ]; then
342 if [ $keepdaily -gt 0 ]; then btype=daily
343 elif [ $keepweekly -gt 0 ]; then btype=weekly
344 elif [ $keepmonthly -gt 0 ]; then btype=monthly
345 else fatal "keeping no backups"; fi
347 if [ "$testuser" != "" ]; then
348 cd "$srcdir/${user:0:1}"
350 setup_remote_dirs $testuser $btype
351 do_user $testuser $btype
353 [ -d "$srcdir" ] || fatal "directory $srcdir not found."
355 for userdir in `ls -d1 */`; do
356 [ -d "$srcdir/$userdir" ] || fatal "directory $srcdir/$userdir not found."
357 cd "$srcdir/$userdir"
359 for user in `ls -1`; do
360 [ "$user" != "" ] || continue
361 debug "$user $userdir"
362 do_rotate $user $userdir
363 setup_remote_dirs $user $btype $userdir
364 do_user $user $btype $userdir