90ca2a3ebf39f72e7d626c11d72ab28a3d48193c
[matthijs/upstream/backupninja.git] / handlers / rdiff
1 #
2 # rdiff-backup handler script for backupninja
3 # requires rdiff-backup
4 #
5
6 setsection source
7 getconf type; sourcetype=$type
8 getconf label
9 getconf user root; sourceuser=$user
10 getconf keep
11 getconf include
12 getconf exclude
13
14 ### DESTINATION ###
15
16 setsection dest
17 getconf directory; destdir=$directory
18 # strip trailing /
19 destdir=${destdir%/}
20 getconf type; desttype=$type
21 getconf user; destuser=$user
22 getconf host; desthost=$host
23
24 [ "$destdir" != "" ] || fatal "Destination directory not set"
25 [ "$desttype" == "remote" ] || fatal "Only remote destinations are supported"
26
27 # see if we can login
28 debug "su $sourceuser -c \"ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'\""
29 if [ ! $test ]; then
30         result=`su $sourceuser -c "ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'" 2>&1`
31         if [ "$result" != "1" ]; then
32                 fatal "Can't connect to $desthost as $destuser."
33         fi
34 fi
35
36 # see that rdiff-backup has the same version as here
37 debug "su $sourceuser -c \"ssh $desthost -l $destuser '$RDIFFBACKUP -V'\""
38 if [ ! $test ]; then
39         remoteversion=`su $sourceuser -c "ssh $desthost -l $destuser '$RDIFFBACKUP -V'" 2>&1`
40         localversion=`$RDIFFBACKUP -V`
41         if [ "$remoteversion" != "$localversion" ]; then
42                 fatal "rdiff-backup does not have the same version on this computer and the backup server."
43         fi
44 fi
45
46 execstr_serverpart="$destuser@$desthost::$destdir/$label"
47                 
48 ### SOURCE ###
49
50 [ "$label" != "" ] || fatal "Source missing label"
51 [ "$sourcetype" == "local" ] || fatal "Only local source type supported"
52 [ "$include" != "" ] || fatal "No source includes specified"
53
54 execstr_clientpart="/"
55         
56 ## REMOVE OLD BACKUPS
57
58 if [ "$keep" -gt "0" ]; then
59         removestr="rdiff-backup --force --remove-older-than ${keep}D "
60         if [ "$desttype" == "remote" ]; then
61                 removestr="${removestr}${destuser}@${desthost}::"
62         fi
63         removestr="${removestr}${destdir}/${label}";
64         
65         debug "su $sourceuser -c '$removestr'"
66         if [ ! $test ]; then
67                 output=`su $sourceuser -c "$removestr" 2>&1`
68                 code=$?
69                 if [ "$code" == "0" ]; then
70                         debug $output
71                         info "Removing backups older than $keep days succeeded."
72                 else
73                         warning $output
74                         warning "Failed removing backups older than $keep."
75                 fi
76         fi
77 fi
78
79 ## EXECUTE ##
80         
81 execstr="$RDIFFBACKUP --print-statistics "
82
83 # TODO: order the includes and excludes
84
85 # excludes
86 for i in $exclude; do
87         str="${i//__star__/*}"
88         execstr="${execstr}--exclude '$str' "
89 done
90         
91 # includes 
92 for i in $include; do
93         str="${i//__star__/*}"
94         execstr="${execstr}--include '$str' "
95 done
96
97 # exclude everything else
98 execstr="${execstr}--exclude '/*' "
99                 
100 # include client-part and server-part
101 execstr="${execstr}$execstr_clientpart $execstr_serverpart"
102
103 debug "su $sourceuser -c '$execstr'"
104 if [ ! $test ]; then
105         output=`su $sourceuser -c "$execstr" 2>&1`
106         code=$?
107         if [ "$code" == "0" ]; then
108                 debug $output
109                 info "Successfully finished backing up source '$label'"
110         else
111                 warning $output
112                 warning "Failed backup up source '$label'"
113         fi
114 fi      
115
116 return 0