(no commit message)
[matthijs/upstream/backupninja.git] / handlers / maildir
1 #!/usr/bin/php4 -q
2 <?php
3
4 ###############################################################
5 #
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 #
10 ##############################################################
11
12 getconf rotate yes
13 getconf remove yes
14
15 getconf loadlimit 5
16 getconf speedlimit 0
17 getconf keepdaily 7
18 getconf keepweekly 4
19
20 getconf srcdir /var/maildir
21 getconf destdir
22 getconf desthost
23 getconf destport 22
24 getconf destuser
25
26 # used for testing
27 getconf letter
28 getconf user
29
30 [ -d $srcdir ] || fatal "source directory $srcdir doesn't exist"
31
32 [ ! $test ] || testflags="--dry-run -v"
33 rsyncflags="$testflags -e 'ssh -p $destport'"
34 flags_mail="$rsyncflags --archive --ignore-existing --delete --numeric-ids --size-only --bwlimit=$speedlimit"
35 flags_folders="$rsyncflags --archive --delete --numeric-ids"
36 excludes='--exclude ".Trash/*" --exclude ".Mistakes/*" --exclude ".Spam/*"'
37
38 # see if we can login
39 debug "ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
40 if [ ! $test ]; then
41         result=`ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1' 2>&1`
42         if [ "$result" != "1" ]; then
43                 fatal "Can't connect to $desthost as $destuser."
44         fi
45 fi
46
47 ##################################################################
48 ### FUNCTIONS
49
50 function do_letters() {
51         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
52                 do_maildirs "$srcdir/$i"
53         done
54 }
55
56 function do_maildirs() {
57         local dir=$1
58         [ -d $dir ] || fatal "directory $dir not found."
59         for userdir in `ls -1 $dir`; do
60                 do_userdir $userdir
61         done
62 }
63
64 function do_user() {
65         local user=$1
66         local letter=${user:0:1}
67         local dir="$srcdir/$letter/$user"
68         [ -d $dir ] || fatal "maildir $dir not found".
69
70         while 1; do
71                 load=`uptime | sed 's/^.*load average: \\([^,]*\\).*$/\\1/'`
72                 if [ $load -lt $loadlimit ]; then
73                         info "load $load, sleeping..."
74                         sleep 600
75                 else
76                         break
77                 fi
78         done
79         
80         cmd="rsync $maildirrsyncflags $excludes '$dir' '$destuser@$desthost:$destdir/maildir/$letter'"
81         debug $cmd
82         # ret=`rsync $maildirrsyncflags $excludes '$dir' '$destuser@$desthost:$destdir/maildir/$letter' 2>&1`
83 }
84
85 # remove any maildirs from backup which might have been deleted
86 # and add new ones which have just been created.
87
88 function do_remove() {
89         local tmp1=/tmp/maildirtmpfile$$
90         local tmp2=/tmp/maildirtmpfile$$
91         
92         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
93                 ls -1 "$srcdir/$i" | sort > $tmp1
94                 ssh -p $destport $desthost 'ls -1 '$destdir/maildir/$i' | sort > $tmp2
95                 for deluser in `join -v 2 $tmp1 $tmp2`; do
96                         cmd="ssh -p $destport $desthost rm -vr '$destdir/maildir/$i/$deluser/'"
97                         debug $cmd
98                 done
99         done
100         rm $tmp1
101         rm $tmp2        
102 }
103
104 ###
105 ##################################################################
106
107 ### ROTATE BACKUPS ###
108
109 if [ "$remove" == "yes" ]; then
110
111 fi
112
113 ### REMOVE OLD MAILDIRS ###
114
115 if [ "$rotate" == "yes" ]; then
116
117 fi
118
119 ### ROTATE BACKUPS ###
120
121 if [ "$letter" != "" ]; then
122
123 fi
124
125 if [ "$user" != "" ]; then
126
127 fi