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