1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
4 # rdiff-backup handler script for backupninja
5 # requires rdiff-backup
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).
16 debug "(local is assumed to be a good connection)"
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"
26 fatal "Can't connect to $host as $user."
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.
35 if [ "$#" -lt 2 ]; then
36 debug "$RDIFFBACKUP -V"
37 echo `$RDIFFBACKUP -V`
41 debug "ssh $sshoptions $host -l $user '$RDIFFBACKUP -V'"
42 version=`ssh $sshoptions $host -l $user "$RDIFFBACKUP -V"`
44 fatal "Unable to execute rdiff-backup on remote server. It probably isn't installed"
46 echo "$version" | grep rdiff-backup
51 function check_consistency() {
56 if [ "$type" == "local" ]; then
57 if [ "$user" != "" ]; then
58 warning "User should not be specified for local $section."
60 if [ "$host" != "" ]; then
61 warning "Host should not be specified for local $section."
64 if [ "$type" == "remote" ]; then
65 if [ "$user" == "" ]; then
66 fatal "User must be specified for remote $section."
68 if [ "$host" == "" ]; then
69 fatal "Host must be specifed for remote $section."
74 function check_cstream() {
76 if [ ! -x $cstream ]; then
77 fatal "Can't find your cstream binary (trying: $cstream). If you use bwlimit you must have cstream installed."
84 getconf testconnect yes
87 getconf ignore_version no
90 getconf type; sourcetype=$type
91 getconf user; sourceuser=$user
92 getconf host; sourcehost=$host
93 check_consistency "source" "$type" "$user" "$host"
102 getconf directory; destdir=$directory
105 getconf type; desttype=$type
106 getconf user; destuser=$user
107 getconf host; desthost=$host
109 check_consistency "destination" "$type" "$user" "$host"
111 if [ -n "$sshoptions" ] && echo $options | grep -qv "remote-schema"; then
112 options="$options --remote-schema 'ssh -C $sshoptions %s rdiff-backup --server'"
117 # If vservers are configured, check that the ones listed in $vsnames do exist.
119 if [ $vservers_are_available = yes ]; then
120 if [ "$vsnames" = all ]; then
121 vsnames="$found_vservers"
123 if ! vservers_exist "$vsnames" ; then
124 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
127 if [ -n "$vsinclude" ]; then
128 info "Using vservers '$vsnames'"
132 [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
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
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."
151 # source specific checks
153 remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
154 local ) execstr_sourcepart="/" ;;
155 * ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
158 # destination specific checks
159 [ "$destdir" != "" ] || fatal "Destination directory not set"
161 remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
162 local ) execstr_destpart="$destdir/$label" ;;
163 * ) fatal "desttype '$desttype' is neither local nor remote" ;;
166 ### REMOVE OLD BACKUPS ###
168 if [ "$keep" != yes ]; then
170 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
171 # add D if no other date unit is specified
175 removestr="$RDIFFBACKUP $options --force --remove-older-than $keep "
176 if [ "$desttype" == "remote" ]; then
177 removestr="${removestr}${destuser}@${desthost}::"
179 removestr="${removestr}${destdir}/${label}";
182 if [ $test = 0 ]; then
183 output="`su -c "$removestr" 2>&1`"
186 info "Removing backups older than $keep days succeeded."
189 warning "Failed removing backups older than $keep."
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'"
204 fatal "You specified a bandwidth limit but neither your source nor destination types are remote."
210 execstr="$RDIFFBACKUP $options --print-statistics "
214 symlinks_warning="Maybe you have mixed symlinks and '*' in this statement, which is not supported."
216 # TODO: order the includes and excludes
219 IFS=$(echo -en "\n\b")
220 for i in $exclude; do
221 str="${i//__star__/*}"
222 execstr="${execstr}--exclude '$str' "
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' "
236 if [ $usevserver = yes ]; then
237 for vserver in $vsnames; do
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' "
246 warning "vsinclude statement '${vi//__star__/*}' will be ignored for VServer $vserver. $symlinks_warning"
255 # exclude everything else
256 [ "$include" != "" -o "$vsinclude" != "" ] && execstr="${execstr}--exclude '/*' "
258 # include client-part and server-part
259 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
262 if [ $test = 0 ]; then
263 output=`nice -n $nicelevel su -c "$execstr" 2>&1`
266 info "Successfully finished backing up source $label"
269 warning "Failed backup up source $label"