8de2b979b7a959f2b3d36e948f00920e29770dcb
[matthijs/upstream/backupninja.git] / handlers / maildir
1 ###############################################################
2 #
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.
6 #
7 #  if destdir is /backup/maildir/, then it will contain the files
8 #    daily.1
9 #    daily.2
10 #    daily.3
11 #    weekly.1
12 #    weekly.2
13 #    monthly.1
14 #  if keepdaily is 3, keepweekly is 2, and keepmonthly is 1. 
15
16 ##############################################################
17
18 getconf rotate yes
19 getconf remove yes
20
21 getconf loadlimit 5
22 getconf speedlimit 0
23 getconf keepdaily 5
24 getconf keepweekly 3
25 getconf keepmonthly 1
26
27 getconf srcdir /var/maildir
28 getconf destdir
29 getconf desthost
30 getconf destport 22
31 getconf destuser
32
33 failedcount=0
34
35 # strip trailing /
36 destdir=${destdir%/}
37 srcdir=${srcdir%/}
38
39 # used for testing
40 #getconf letter
41 #getconf testuser elijah
42 getconf backup yes
43 #letters=e
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"
45
46 [ -d $srcdir ] || fatal "source directory $srcdir doesn't exist"
47
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/\*'"
51
52 # see if we can login
53 debug "ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
54 if [ ! $test ]; then
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."
58         fi
59 fi
60
61 ##################################################################
62 ### FUNCTIONS
63
64 function do_user() {
65         local user=$1
66         local destdir=$2
67         local letter=${user:0:1}
68         local dir="$srcdir/$letter/$user"
69         [ -d $dir ] || fatal "maildir $dir not found".
70
71 #       while true; do
72 #               load=`uptime | sed 's/^.*load average: \\([^,]*\\).*$/\\1/'`
73 #               over=`expr $load \> $loadlimit`
74 #               if [ $over == 1 ]; then
75 #                       info "load $load, sleeping..."
76 #                       sleep 600
77 #               else
78 #                       break
79 #               fi
80 #       done
81         
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 \
87 2>&1`
88         if [ $? != 0 ]; then
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."
94                 fi
95         fi
96 }
97
98 # remove any maildirs from backup which might have been deleted
99 # and add new ones which have just been created.
100
101 function do_remove() {
102         local tmp1=/tmp/maildirtmpfile$$
103         local tmp2=/tmp/maildirtmpfile$$
104         
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/'"
110                         debug $cmd
111                 done
112         done
113         rm $tmp1
114         rm $tmp2        
115 }
116
117 function do_rotate() {
118         backuproot=$destdir
119
120 (
121         debug Connecting to $desthost
122         ssh -T -o PasswordAuthentication=no $desthost -l $destuser <<EOF
123 ##### BEGIN REMOTE SCRIPT #####
124         seconds_daily=86400
125         seconds_weekly=604800
126         seconds_monthly=2628000
127         keepdaily=$keepdaily
128         keepweekly=$keepweekly
129         keepmonthly=$keepmonthly
130         now=\`date +%s\`
131
132         for rottype in daily weekly monthly; do
133                 seconds=\$((seconds_\${rottype}))
134
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."
138                         continue 1
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."
141                         continue 1
142                 fi
143                 
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\`
152                                 else
153                                         rotated=0
154                                 fi
155                                 cutoff_time=\$(( now - (seconds*i) ))
156                                 if [ \$rotated -lt \$cutoff_time ]; then
157                                         next=\$(( i + 1 ))
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
162                                         else
163                                                 echo "Info: skipping rotation of \$dir.\$i because \$dir.\$next already exists."
164                                         fi
165                                 else
166                                         echo "Info: skipping rotation of \$dir.\$i because it was rotated" \$(( (now-rotated)/86400)) "days ago ("\$(( (now-cutoff_time)/86400))" needed)."
167                                 fi
168                         fi
169                 done
170         done
171
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
177         fi
178
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
184         fi
185
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
197                                 else
198                                         echo "Info: moving \$dir.\$i to $backuproot/rotate.tmp"
199                                         mv \$dir.\$i $backuproot/rotate.tmp
200                                 fi
201                         fi
202                 done
203         done
204 ####### END REMOTE SCRIPT #######
205 EOF
206 ) | (while read a; do passthru $a; done)
207
208 }
209
210
211 function setup_remote_dirs() {
212         local backuptype=$1
213         local dir="$destdir/$backuptype"
214
215 (
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."
219                         exit 1
220                 elif [ -d $dir.1 ]; then
221                         if [ -f $dir.1/created ]; then
222                                 echo "Warning: $dir.1 already exists. Overwriting contents."
223                         else
224                                 echo "Warning: we seem to be resuming a partially written $dir.1"
225                         fi
226                 else
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"
231                                         exit 1
232                                 fi
233                         else
234                                 mkdir $dir.1
235                                 if [ \$? == 1 ]; then
236                                         echo "Fatal: could not create directory $dir.1 on host $desthost"
237                                         exit 1
238                                 fi
239                         fi
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"
245                                         exit 1
246                                 fi
247                         fi
248                 fi
249                 [ -f $dir.1/created ] && rm $dir.1/created
250                 [ -f $dir.1/rotated ] && rm $dir.1/rotated
251                 exit 0
252 EOF
253 ) | (while read a; do passthru $a; done)
254
255         if [ $? == 1 ]; then exit; fi
256 }
257
258 ###
259 ##################################################################
260
261 ### ROTATE BACKUPS ###
262
263 if [ "$rotate" == "yes" ]; then
264         do_rotate
265 fi
266
267 ### REMOVE OLD MAILDIRS ###
268
269 if [ "$remove" == "yes" ]; then
270         debug remove
271 fi
272
273 ### MAKE BACKUPS ###
274
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
280
281         setup_remote_dirs $btype
282         
283         for i in $letters; do
284                 [ -d "$srcdir/$i" ] || fatal "directory $srcdir/$i not found."
285                 cd "$srcdir/$i"
286                 debug $i
287                 for user in `ls -1`; do
288                         if [ "$testuser" != "" -a "$testuser" != "$user" ]; then continue; fi
289                         do_user $user $destdir/$btype.1
290                 done
291         done
292
293         ssh -o PasswordAuthentication=no $desthost -l $destuser "date +%c%n%s > $destdir/$btype.1/created"
294 fi