rdiff: incorporate sshoptions into options via remote-schema if not there
[matthijs/upstream/backupninja.git] / handlers / rdiff.in
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 #
3 # rdiff-backup handler script for backupninja
4 # requires rdiff-backup
5 #
6
7 ### FUNCTIONS ###
8
9 function test_connection() {
10         # given a user and host,
11         # tests the connection.
12         # if user or host is missing, returns 0
13         # (ie, assume it's a local connection).
14         if [ $# -lt 2 ]; then
15                 debug "(local is assumed to be a good connection)"
16                 return 0
17         fi
18         local user=$1
19         local host=$2
20         debug "ssh $sshoptions -o PasswordAuthentication=no $host -l $user 'echo -n 1'"
21         local ret=`ssh $sshoptions -o PasswordAuthentication=no $host -l $user 'echo -n host is alive'`
22         if echo $ret | grep "host is alive"; then
23                 debug "Connected to $host as $user successfully"
24         else
25                 fatal "Can't connect to $host as $user."
26         fi
27 }
28
29 function get_version() {
30         # given no arguments, returns the local version.
31         # given a user and host, returns the remote version.
32         # if user or host is missing, returns the local version.
33         if [ "$#" -lt 2 ]; then
34                 debug "$RDIFFBACKUP -V"
35                 echo `$RDIFFBACKUP -V`
36         else
37                 local user=$1
38                 local host=$2
39                 debug "ssh $sshoptions $host -l $user '$RDIFFBACKUP -V'"
40                 echo `ssh $sshoptions $host -l $user "$RDIFFBACKUP -V | grep rdiff-backup"`
41         fi
42 }
43
44 function check_consistency() {
45         local section=$1
46         local type=$2
47         local user=$3
48         local host=$4
49         if [ "$type" == "local" ]; then
50                 if [ "$user" != "" ]; then
51                         warning "User should not be specified for local $section."
52                 fi
53                 if [ "$host" != "" ]; then
54                         warning "Host should not be specified for local $section."
55                 fi
56         fi
57         if [ "$type" == "remote" ]; then
58                 if [ "$user" == "" ]; then
59                         fatal "User must be specified for remote $section."
60                 fi
61                 if [ "host" == "" ]; then
62                         fatal "Host must be specifed for remote $section."
63                 fi
64         fi
65 }
66
67 function check_cstream() {
68         local cstream=$1
69         if [ ! -x $cstream ]; then
70                 fatal "Can't find your cstream binary (trying: $cstream). If you use bwlimit you must have cstream installed."
71         fi
72 }
73
74 ### GET CONFIG ###
75
76 getconf options
77 getconf testconnect yes
78 getconf nicelevel 0
79 getconf bwlimit
80 getconf ignore_version no
81
82 setsection source
83 getconf type; sourcetype=$type
84 getconf user; sourceuser=$user
85 getconf host; sourcehost=$host
86 check_consistency "source" "$type" "$user" "$host"
87 getconf label
88 getconf keep 60
89 getconf include
90 getconf vsnames all
91 getconf vsinclude
92 getconf exclude
93
94 setsection dest
95 getconf directory; destdir=$directory
96 # strip trailing /
97 destdir=${destdir%/}
98 getconf type; desttype=$type
99 getconf user; destuser=$user
100 getconf host; desthost=$host
101 getconf sshoptions
102 check_consistency "destination" "$type" "$user" "$host"
103
104 if [ -n "$sshoptions" ] && echo $options | grep -qv "remote-schema"; then
105         options="$options --remote-schema 'ssh -C $sshoptions %s rdiff-backup --server'"
106 fi
107
108 ### CHECK CONFIG ###
109
110 # If vservers are configured, check that the ones listed in $vsnames do exist.
111 local usevserver=no
112 if [ $vservers_are_available = yes ]; then
113    if [ "$vsnames" = all ]; then
114       vsnames="$found_vservers"
115    else
116       if ! vservers_exist "$vsnames" ; then
117             fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
118       fi
119    fi
120    if [ -n "$vsinclude" ]; then
121       info "Using vservers '$vsnames'"
122       usevserver=yes
123    fi
124 else
125    [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
126 fi
127
128 # check the connection at the source and destination
129 [ -n "$test" ] || test=0
130 if [ "$testconnect" = "yes" ] || [ "${test}" -eq 1 ]; then
131         test_connection $sourceuser $sourcehost
132         test_connection $destuser $desthost
133 fi
134
135 if [ "$ignore_version" != "yes" ]; then
136         # see that rdiff-backup has the same version at the source and destination
137         sourceversion=`get_version $sourceuser $sourcehost`
138         destversion=`get_version $destuser $desthost`
139         if [ "$sourceversion" != "$destversion" ]; then
140                 fatal "rdiff-backup does not have the same version at the source and at the destination."
141         fi
142 fi
143
144 # source specific checks
145 case $sourcetype in 
146         remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
147         local  ) execstr_sourcepart="/" ;;
148         *      ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
149 esac
150
151 # destination specific checks
152 [ "$destdir" != "" ] || fatal "Destination directory not set"
153 case $desttype in 
154         remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
155         local  ) execstr_destpart="$destdir/$label" ;;
156         *      ) fatal "desttype '$desttype' is neither local nor remote" ;;
157 esac
158         
159 ### REMOVE OLD BACKUPS ###
160
161 if [ "$keep" != yes ]; then
162
163    if [ "`echo $keep | tr -d 0-9`" == "" ]; then
164         # add D if no other date unit is specified
165       keep="${keep}D"
166    fi
167
168    removestr="$RDIFFBACKUP $options --force --remove-older-than $keep "
169    if [ "$desttype" == "remote" ]; then
170       removestr="${removestr}${destuser}@${desthost}::"
171    fi
172    removestr="${removestr}${destdir}/${label}";
173
174    debug "$removestr"
175    if [ $test = 0 ]; then
176       output="`su -c "$removestr" 2>&1`"
177       if [ $? = 0 ]; then
178          debug $output
179          info "Removing backups older than $keep days succeeded."
180       else
181          warning $output
182          warning "Failed removing backups older than $keep."
183       fi
184    fi
185
186 fi
187
188 # Add cstream 
189
190 if [ ! -z $bwlimit ]; then
191         check_cstream $CSTREAM;
192         if [ "$desttype" = "remote" ]; then
193                 RDIFFBACKUP="$RDIFFBACKUP --remote-schema 'cstream -t $bwlimit | ssh %s \''rdiff-backup --server\'''"
194         elif [ "$sourcetype" = "remote" ]; then
195                 RDIFFBACKUP="$RDIFFBACKUP --remote-schema 'ssh %s \''rdiff-backup --server\'' | cstream -t $bwlimit'"
196         else
197                 fatal "You specified a bandwidth limit but neither your source nor destination types are remote."
198         fi
199 fi
200
201 ### EXECUTE ###
202
203 execstr="$RDIFFBACKUP $options --print-statistics "
204
205 set -o noglob
206
207 symlinks_warning="Maybe you have mixed symlinks and '*' in this statement, which is not supported."
208
209 # TODO: order the includes and excludes
210 # excludes
211 for i in $exclude; do
212    str="${i//__star__/*}"
213    execstr="${execstr}--exclude '$str' "
214 done
215 # includes 
216 for i in $include; do
217    [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
218    str="${i//__star__/*}"
219    execstr="${execstr}--include '$str' "
220 done
221
222 # vsinclude
223 if [ $usevserver = yes ]; then
224    for vserver in $vsnames; do
225       for vi in $vsinclude; do
226          str="${vi//__star__/*}"
227          str="$VROOTDIR/$vserver$str"
228          if [ -n "$str" ]; then
229             execstr="${execstr}--include '$str' "
230          else
231             warning "vsinclude statement '${vi//__star__/*}' will be ignored for VServer $vserver. $symlinks_warning"
232          fi
233       done
234    done
235 fi
236
237 set +o noglob
238
239 # exclude everything else
240 [ "$include" != "" -o "$vsinclude" != "" ] && execstr="${execstr}--exclude '/*' "
241                 
242 # include client-part and server-part
243 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
244
245 debug "$execstr"
246 if [ $test = 0 ]; then
247         output=`nice -n $nicelevel su -c "$execstr" 2>&1`
248         if [ $? = 0 ]; then
249                 debug $output
250                 info "Successfully finished backing up source $label"
251         else
252                 warning $output
253                 warning "Failed backup up source $label"
254         fi
255 fi      
256
257 return 0