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