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