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