typo in backupninja.1
[matthijs/upstream/backupninja.git] / handlers / rdiff
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 | cut -d. -f1,2`
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 | cut -d. -f1,2"`
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
81 setsection source
82 getconf type; sourcetype=$type
83 getconf user; sourceuser=$user
84 getconf host; sourcehost=$host
85 check_consistency "source" "$type" "$user" "$host"
86 getconf label
87 getconf keep 60
88 getconf include
89 getconf vsnames all
90 getconf vsinclude
91 getconf exclude
92
93 setsection dest
94 getconf directory; destdir=$directory
95 # strip trailing /
96 destdir=${destdir%/}
97 getconf type; desttype=$type
98 getconf user; destuser=$user
99 getconf host; desthost=$host
100 getconf sshoptions
101 check_consistency "destination" "$type" "$user" "$host"
102
103 ### CHECK CONFIG ###
104
105 # If vservers are configured, check that the ones listed in $vsnames do exist.
106 local usevserver=no
107 if [ $vservers_are_available = yes ]; then
108    if [ "$vsnames" = all ]; then
109       vsnames="$found_vservers"
110    else
111       if ! vservers_exist "$vsnames" ; then
112             fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
113       fi
114    fi
115    if [ -n "$vsinclude" ]; then
116       info "Using vservers '$vsnames'"
117       usevserver=yes
118    fi
119 else
120    [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
121 fi
122
123 # check the connection at the source and destination
124 [ -n "$test" ] || test=0
125 if [ "$testconnect" = "yes" ] || [ "${test}" -eq 1 ]; then
126         test_connection $sourceuser $sourcehost
127         test_connection $destuser $desthost
128 fi
129
130 # see that rdiff-backup has the same version at the source and destination
131 sourceversion=`get_version $sourceuser $sourcehost`
132 destversion=`get_version $destuser $desthost`
133 if [ "$sourceversion" != "$destversion" ]; then
134         fatal "rdiff-backup does not have the same version at the source and at the destination."
135 fi
136
137 # source specific checks
138 [ "$include" != "" -o "$vsinclude" != "" ] || fatal "No source includes specified"
139 case $sourcetype in 
140         remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
141         local  ) execstr_sourcepart="/" ;;
142         *      ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
143 esac
144
145 # destination specific checks
146 [ "$destdir" != "" ] || fatal "Destination directory not set"
147 case $desttype in 
148         remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
149         local  ) execstr_destpart="$destdir/$label" ;;
150         *      ) fatal "desttype '$desttype' is neither local nor remote" ;;
151 esac
152         
153 ### REMOVE OLD BACKUPS ###
154
155 if [ "$keep" != yes ]; then
156
157    if [ "`echo $keep | tr -d 0-9`" == "" ]; then
158         # add D if no other date unit is specified
159       keep="${keep}D"
160    fi
161
162    removestr="$RDIFFBACKUP $options --force --remove-older-than $keep "
163    if [ "$desttype" == "remote" ]; then
164       removestr="${removestr}${destuser}@${desthost}::"
165    fi
166    removestr="${removestr}${destdir}/${label}";
167
168    debug "$removestr"
169    if [ $test = 0 ]; then
170       output="`su -c "$removestr" 2>&1`"
171       if [ $? = 0 ]; then
172          debug $output
173          info "Removing backups older than $keep days succeeded."
174       else
175          warning $output
176          warning "Failed removing backups older than $keep."
177       fi
178    fi
179
180 fi
181
182 # Add cstream 
183
184 if [ ! -z $bwlimit ]; then
185         check_cstream $CSTREAM;
186         if [ "$desttype" = "remote" ]; then
187                 RDIFFBACKUP="$RDIFFBACKUP --remote-schema 'cstream -t $bwlimit | ssh %s \''rdiff-backup --server\'''"
188         elif [ "$sourcetype" = "remote" ]; then
189                 RDIFFBACKUP="$RDIFFBACKUP --remote-schema 'ssh %s \''rdiff-backup --server\'' | cstream -t $bwlimit'"
190         else
191                 fatal "You specified a bandwidth limit but neither your source nor destination types are remote."
192         fi
193 fi
194
195 ### EXECUTE ###
196
197 execstr="$RDIFFBACKUP $options --print-statistics "
198
199 set -o noglob
200
201 symlinks_warning="Maybe you have mixed symlinks and '*' in this statement, which is not supported."
202
203 # TODO: order the includes and excludes
204 # excludes
205 for i in $exclude; do
206    str="${i//__star__/*}"
207    execstr="${execstr}--exclude '$str' "
208 done
209 # includes 
210 for i in $include; do
211    [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
212    str="${i//__star__/*}"
213    execstr="${execstr}--include '$str' "
214 done
215
216 # vsinclude
217 if [ $usevserver = yes ]; then
218    for vserver in $vsnames; do
219       for vi in $vsinclude; do
220          str="${vi//__star__/*}"
221          str="$VROOTDIR/$vserver$str"
222          if [ -n "$str" ]; then
223             execstr="${execstr}--include '$str' "
224          else
225             warning "vsinclude statement '${vi//__star__/*}' will be ignored for VServer $vserver. $symlinks_warning"
226          fi
227       done
228    done
229 fi
230
231 set +o noglob
232
233 # exclude everything else
234 execstr="${execstr}--exclude '/*' "
235                 
236 # include client-part and server-part
237 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
238
239 debug "$execstr"
240 if [ $test = 0 ]; then
241         output=`nice -n $nicelevel su -c "$execstr" 2>&1`
242         if [ $? = 0 ]; then
243                 debug $output
244                 info "Successfully finished backing up source $label"
245         else
246                 warning $output
247                 warning "Failed backup up source $label"
248         fi
249 fi      
250
251 return 0