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
52 getconf destid_file /root/.ssh/id_rsa
54 getconf multiconnection notset
56 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"
62 [ -d $srcdir ] || fatal "source directory $srcdir doesn't exist"
64 [ "$multiconnection" == "notset" ] && fatal "The maildir handler uses a very different destination format. See the example .maildir for more information"
67 testflags="--dry-run -v"
70 rsyncflags="$testflags -e 'ssh -p $destport -i $destid_file' -r -v --ignore-existing --delete --size-only --bwlimit=$speedlimit"
71 excludes="--exclude '.Trash/\*' --exclude '.Mistakes/\*' --exclude '.Spam/\*'"
73 ##################################################################
79 local letter=${user:0:1}
80 local source="$srcdir/$letter/$user/"
81 local target="$destdir/$letter/$user/$btype.1"
82 if [ ! -d $source ]; then
83 warning "maildir $source not found"
88 ret=`$RSYNC -e "ssh -p $destport -i $destid_file" -r \
89 --links --ignore-existing --delete --size-only --bwlimit=$speedlimit \
90 --exclude '.Trash/*' --exclude '.Mistakes/*' --exclude '.Spam/*' \
91 $source $destuser@$desthost:$target \
94 # ignore 0 (success) and 24 (file vanished before it could be copied)
95 if [ $ret != 0 -a $ret != 24 ]; then
96 warning "rsync $user failed"
97 warning " returned: $ret"
98 let "failedcount = failedcount + 1"
99 if [ $failedcount -gt 100 ]; then
100 fatal "100 rsync errors -- something is not working right. bailing out."
103 ssh -o PasswordAuthentication=no $desthost -l $destuser -i $destid_file "date +%c%n%s > $target/created"
106 # remove any maildirs from backup which might have been deleted
107 # and add new ones which have just been created.
108 # (actually, it just moved them to the directory "deleted")
110 function do_remove() {
111 local tmp1=`maketemp maildir-tmp-file`
112 local tmp2=`maketemp maildir-tmp-file`
114 ssh -p $destport -i $estid_file $destuser@$desthost mkdir -p "$destdir/deleted"
115 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
116 ls -1 "$srcdir/$i/" | sort > $tmp1
117 ssh -p $destport $destuser@$desthost ls -1 "$destdir/$i/" | sort > $tmp2
118 for deluser in `join -v 2 $tmp1 $tmp2`; do
119 [ "$deluser" != "" ] || continue
120 info "removing $destuser@$desthost:$destdir/$i/$deluser/"
121 ssh -p $destport -i $destid_file $destuser@$desthost mv "$destdir/$i/$deluser/" "$destdir/deleted"
122 ssh -p $destport -i $destid_file $destuser@$desthost "date +%c%n%s > '$destdir/$i/$deluser/deleted_on'"
129 function do_rotate() {
130 [ "$rotate" == "yes" ] || return;
132 local letter=${user:0:1}
133 local backuproot="$destdir/$letter/$user"
135 ssh -T -o PasswordAuthentication=no $desthost -l $destuser -i $destid_file <<EOF
136 ##### BEGIN REMOTE SCRIPT #####
138 seconds_weekly=604800
139 seconds_monthly=2628000
141 keepweekly=$keepweekly
142 keepmonthly=$keepmonthly
145 if [ ! -d "$backuproot" ]; then
146 echo "Debug: skipping rotate of $user. $backuproot doesn't exist."
149 for rottype in daily weekly monthly; do
150 seconds=\$((seconds_\${rottype}))
152 dir="$backuproot/\$rottype"
153 if [ ! -d \$dir.1 ]; then
154 echo "Debug: \$dir.1 does not exist, skipping."
156 elif [ ! -f \$dir.1/created ]; then
157 echo "Warning: \$dir.1/created does not exist. This backup may be only partially completed. Skipping rotation."
161 # Rotate the current list of backups, if we can.
162 oldest=\`find $backuproot -maxdepth 1 -type d -name \$rottype'.*' | @SED@ 's/^.*\.//' | sort -n | tail -1\`
163 #echo "Debug: oldest \$oldest"
164 [ "\$oldest" == "" ] && oldest=0
165 for (( i=\$oldest; i > 0; i-- )); do
166 if [ -d \$dir.\$i ]; then
167 if [ -f \$dir.\$i/created ]; then
168 created=\`tail -1 \$dir.\$i/created\`
172 cutoff_time=\$(( now - (seconds*(i-1)) ))
173 if [ ! \$created -gt \$cutoff_time ]; then
175 if [ ! -d \$dir.\$next ]; then
176 echo "Debug: \$rottype.\$i --> \$rottype.\$next"
177 mv \$dir.\$i \$dir.\$next
178 date +%c%n%s > \$dir.\$next/rotated
180 echo "Debug: skipping rotation of \$dir.\$i because \$dir.\$next already exists."
183 echo "Debug: skipping rotation of \$dir.\$i because it was created" \$(( (now-created)/86400)) "days ago ("\$(( (now-cutoff_time)/86400))" needed)."
189 max=\$((keepdaily+1))
190 if [ \( \$keepweekly -gt 0 -a -d $backuproot/daily.\$max \) -a ! -d $backuproot/weekly.1 ]; then
191 echo "Debug: daily.\$max --> weekly.1"
192 mv $backuproot/daily.\$max $backuproot/weekly.1
193 date +%c%n%s > $backuproot/weekly.1/rotated
196 max=\$((keepweekly+1))
197 if [ \( \$keepmonthly -gt 0 -a -d $backuproot/weekly.\$max \) -a ! -d $backuproot/monthly.1 ]; then
198 echo "Debug: weekly.\$max --> monthly.1"
199 mv $backuproot/weekly.\$max $backuproot/monthly.1
200 date +%c%n%s > $backuproot/monthly.1/rotated
203 for rottype in daily weekly monthly; do
204 max=\$((keep\${rottype}+1))
205 dir="$backuproot/\$rottype"
206 oldest=\`find $backuproot -maxdepth 1 -type d -name \$rottype'.*' | @SED@ 's/^.*\.//' | sort -n | tail -1\`
207 [ "\$oldest" == "" ] && oldest=0
208 # if we've rotated the last backup off the stack, remove it.
209 for (( i=\$oldest; i >= \$max; i-- )); do
210 if [ -d \$dir.\$i ]; then
211 if [ -d $backuproot/rotate.tmp ]; then
212 echo "Debug: removing rotate.tmp"
213 rm -rf $backuproot/rotate.tmp
215 echo "Debug: moving \$rottype.\$i to rotate.tmp"
216 mv \$dir.\$i $backuproot/rotate.tmp
220 ####### END REMOTE SCRIPT #######
222 ) | (while read a; do passthru $a; done)
227 function setup_remote_dirs() {
230 local letter=${user:0:1}
231 local dir="$destdir/$letter/$user/$backuptype"
232 local tmpdir="$destdir/$letter/$user/rotate.tmp"
234 ssh -T -o PasswordAuthentication=no $desthost -l $destuser -i $destid_file <<EOF
235 if [ ! -d $destdir ]; then
236 echo "Fatal: Destination directory $destdir does not exist on host $desthost."
238 elif [ -d $dir.1 ]; then
239 if [ -f $dir.1/created ]; then
240 echo "Warning: $dir.1 already exists. Overwriting contents."
242 echo "Warning: we seem to be resuming a partially written $dir.1"
245 if [ -d $tmpdir ]; then
247 if [ \$? == 1 ]; then
248 echo "Fatal: could mv $destdir/rotate.tmp $dir.1 on host $desthost"
252 mkdir --parents $dir.1
253 if [ \$? == 1 ]; then
254 echo "Fatal: could not create directory $dir.1 on host $desthost"
258 if [ -d $dir.2 ]; then
259 echo "Debug: update links $backuptype.2 --> $backuptype.1"
260 cp -alf $dir.2/. $dir.1
261 #if [ \$? == 1 ]; then
262 # echo "Fatal: could not create hard links to $dir.1 on host $desthost"
267 [ -f $dir.1/created ] && rm $dir.1/created
268 [ -f $dir.1/rotated ] && rm $dir.1/rotated
271 ) | (while read a; do passthru $a; done)
273 if [ $? == 1 ]; then exit; fi
276 function start_mux() {
277 if [ "$multiconnection" == "yes" ]; then
278 debug "Starting dummy ssh connection"
279 ssh -p $destport -i $destid_file $destuser@$desthost sleep 1d &
285 if [ "$multiconnection" == "yes" ]; then
286 debug "Stopping dummy ssh connection"
287 ssh -p $destport -i $destid_file $destuser@$desthost pkill sleep
292 ##################################################################
294 # see if we can login
295 debug "ssh -o PasswordAuthentication=no $desthost -l $destuser -i $destid_file 'echo -n 1'"
297 result=`ssh -o PasswordAuthentication=no $desthost -l $destuser -i $destid_file 'echo -n 1' 2>&1`
298 if [ "$result" != "1" ]; then
299 fatal "Can't connect to $desthost as $destuser using $destid_file."
307 status=`ssh -p $destport -i $destid_file $destuser@$desthost "[ -d \"$destdir\" ] && echo 'ok'"`
308 if [ "$status" != "ok" ]; then
310 fatal "Destination directory $destdir doesn't exist!"
314 ### REMOVE OLD MAILDIRS ###
316 if [ "$remove" == "yes" ]; then
322 if [ "$backup" == "yes" ]; then
323 if [ $keepdaily -gt 0 ]; then btype=daily
324 elif [ $keepweekly -gt 0 ]; then btype=weekly
325 elif [ $keepmonthly -gt 0 ]; then btype=monthly
326 else fatal "keeping no backups"; fi
328 if [ "$testuser" != "" ]; then
329 cd "$srcdir/${user:0:1}"
331 setup_remote_dirs $testuser $btype
332 do_user $testuser $btype
334 for i in $letters; do
335 [ -d "$srcdir/$i" ] || fatal "directory $srcdir/$i not found."
338 for user in `ls -1`; do
339 [ "$user" != "" ] || continue
342 setup_remote_dirs $user $btype