1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # rdiff-backup handler script for backupninja
4 # requires rdiff-backup
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).
15 debug "(local is assumed to be a good connection)"
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"
25 fatal "Can't connect to $host as $user."
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`
39 debug "ssh $sshoptions $host -l $user '$RDIFFBACKUP -V'"
40 echo `ssh $sshoptions $host -l $user "$RDIFFBACKUP -V | grep rdiff-backup"`
44 function check_consistency() {
49 if [ "$type" == "local" ]; then
50 if [ "$user" != "" ]; then
51 warning "User should not be specified for local $section."
53 if [ "$host" != "" ]; then
54 warning "Host should not be specified for local $section."
57 if [ "$type" == "remote" ]; then
58 if [ "$user" == "" ]; then
59 fatal "User must be specified for remote $section."
61 if [ "host" == "" ]; then
62 fatal "Host must be specifed for remote $section."
67 function check_cstream() {
69 if [ ! -x $cstream ]; then
70 fatal "Can't find your cstream binary (trying: $cstream). If you use bwlimit you must have cstream installed."
77 getconf testconnect yes
80 getconf ignore_version no
83 getconf type; sourcetype=$type
84 getconf user; sourceuser=$user
85 getconf host; sourcehost=$host
86 check_consistency "source" "$type" "$user" "$host"
95 getconf directory; destdir=$directory
98 getconf type; desttype=$type
99 getconf user; destuser=$user
100 getconf host; desthost=$host
102 check_consistency "destination" "$type" "$user" "$host"
106 # If vservers are configured, check that the ones listed in $vsnames do exist.
108 if [ $vservers_are_available = yes ]; then
109 if [ "$vsnames" = all ]; then
110 vsnames="$found_vservers"
112 if ! vservers_exist "$vsnames" ; then
113 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
116 if [ -n "$vsinclude" ]; then
117 info "Using vservers '$vsnames'"
121 [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
124 # check the connection at the source and destination
125 [ -n "$test" ] || test=0
126 if [ "$testconnect" = "yes" ] || [ "${test}" -eq 1 ]; then
127 test_connection $sourceuser $sourcehost
128 test_connection $destuser $desthost
131 if [ "$ignore_version" != "yes" ]; then
132 # see that rdiff-backup has the same version at the source and destination
133 sourceversion=`get_version $sourceuser $sourcehost`
134 destversion=`get_version $destuser $desthost`
135 if [ "$sourceversion" != "$destversion" ]; then
136 fatal "rdiff-backup does not have the same version at the source and at the destination."
140 # source specific checks
142 remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
143 local ) execstr_sourcepart="/" ;;
144 * ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
147 # destination specific checks
148 [ "$destdir" != "" ] || fatal "Destination directory not set"
150 remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
151 local ) execstr_destpart="$destdir/$label" ;;
152 * ) fatal "desttype '$desttype' is neither local nor remote" ;;
155 ### REMOVE OLD BACKUPS ###
157 if [ "$keep" != yes ]; then
159 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
160 # add D if no other date unit is specified
164 removestr="$RDIFFBACKUP $options --force --remove-older-than $keep "
165 if [ "$desttype" == "remote" ]; then
166 removestr="${removestr}${destuser}@${desthost}::"
168 removestr="${removestr}${destdir}/${label}";
171 if [ $test = 0 ]; then
172 output="`su -c "$removestr" 2>&1`"
175 info "Removing backups older than $keep days succeeded."
178 warning "Failed removing backups older than $keep."
186 if [ ! -z $bwlimit ]; then
187 check_cstream $CSTREAM;
188 if [ "$desttype" = "remote" ]; then
189 RDIFFBACKUP="$RDIFFBACKUP --remote-schema 'cstream -t $bwlimit | ssh %s \''rdiff-backup --server\'''"
190 elif [ "$sourcetype" = "remote" ]; then
191 RDIFFBACKUP="$RDIFFBACKUP --remote-schema 'ssh %s \''rdiff-backup --server\'' | cstream -t $bwlimit'"
193 fatal "You specified a bandwidth limit but neither your source nor destination types are remote."
199 execstr="$RDIFFBACKUP $options --print-statistics "
203 symlinks_warning="Maybe you have mixed symlinks and '*' in this statement, which is not supported."
205 # TODO: order the includes and excludes
207 for i in $exclude; do
208 str="${i//__star__/*}"
209 execstr="${execstr}--exclude '$str' "
212 for i in $include; do
213 [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
214 str="${i//__star__/*}"
215 execstr="${execstr}--include '$str' "
219 if [ $usevserver = yes ]; then
220 for vserver in $vsnames; do
221 for vi in $vsinclude; do
222 str="${vi//__star__/*}"
223 str="$VROOTDIR/$vserver$str"
224 if [ -n "$str" ]; then
225 execstr="${execstr}--include '$str' "
227 warning "vsinclude statement '${vi//__star__/*}' will be ignored for VServer $vserver. $symlinks_warning"
235 # exclude everything else
236 [ "$include" != "" -o "$vsinclude" != "" ] && execstr="${execstr}--exclude '/*' "
238 # include client-part and server-part
239 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
242 if [ $test = 0 ]; then
243 output=`nice -n $nicelevel su -c "$execstr" 2>&1`
246 info "Successfully finished backing up source $label"
249 warning "Failed backup up source $label"