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.
34 if [ "$#" -lt 2 ]; then
35 debug "$RDIFFBACKUP -V"
36 echo `$RDIFFBACKUP -V`
40 debug "ssh $sshoptions $host -l $user '$RDIFFBACKUP -V'"
41 echo `ssh $sshoptions $host -l $user "$RDIFFBACKUP -V | grep rdiff-backup"`
45 function check_consistency() {
50 if [ "$type" == "local" ]; then
51 if [ "$user" != "" ]; then
52 warning "User should not be specified for local $section."
54 if [ "$host" != "" ]; then
55 warning "Host should not be specified for local $section."
58 if [ "$type" == "remote" ]; then
59 if [ "$user" == "" ]; then
60 fatal "User must be specified for remote $section."
62 if [ "$host" == "" ]; then
63 fatal "Host must be specifed for remote $section."
68 function check_cstream() {
70 if [ ! -x $cstream ]; then
71 fatal "Can't find your cstream binary (trying: $cstream). If you use bwlimit you must have cstream installed."
78 getconf testconnect yes
81 getconf ignore_version no
84 getconf type; sourcetype=$type
85 getconf user; sourceuser=$user
86 getconf host; sourcehost=$host
87 check_consistency "source" "$type" "$user" "$host"
96 getconf directory; destdir=$directory
99 getconf type; desttype=$type
100 getconf user; destuser=$user
101 getconf host; desthost=$host
103 check_consistency "destination" "$type" "$user" "$host"
105 if [ -n "$sshoptions" ] && echo $options | grep -qv "remote-schema"; then
106 options="$options --remote-schema 'ssh -C $sshoptions %s rdiff-backup --server'"
111 # If vservers are configured, check that the ones listed in $vsnames do exist.
113 if [ $vservers_are_available = yes ]; then
114 if [ "$vsnames" = all ]; then
115 vsnames="$found_vservers"
117 if ! vservers_exist "$vsnames" ; then
118 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
121 if [ -n "$vsinclude" ]; then
122 info "Using vservers '$vsnames'"
126 [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
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
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."
145 # source specific checks
147 remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
148 local ) execstr_sourcepart="/" ;;
149 * ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
152 # destination specific checks
153 [ "$destdir" != "" ] || fatal "Destination directory not set"
155 remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
156 local ) execstr_destpart="$destdir/$label" ;;
157 * ) fatal "desttype '$desttype' is neither local nor remote" ;;
160 ### REMOVE OLD BACKUPS ###
162 if [ "$keep" != yes ]; then
164 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
165 # add D if no other date unit is specified
169 removestr="$RDIFFBACKUP $options --force --remove-older-than $keep "
170 if [ "$desttype" == "remote" ]; then
171 removestr="${removestr}${destuser}@${desthost}::"
173 removestr="${removestr}${destdir}/${label}";
176 if [ $test = 0 ]; then
177 output="`su -c "$removestr" 2>&1`"
180 info "Removing backups older than $keep days succeeded."
183 warning "Failed removing backups older than $keep."
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'"
198 fatal "You specified a bandwidth limit but neither your source nor destination types are remote."
204 execstr="$RDIFFBACKUP $options --print-statistics "
208 symlinks_warning="Maybe you have mixed symlinks and '*' in this statement, which is not supported."
210 # TODO: order the includes and excludes
212 for i in $exclude; do
213 str="${i//__star__/*}"
214 execstr="${execstr}--exclude '$str' "
217 for i in $include; do
218 [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
219 str="${i//__star__/*}"
220 execstr="${execstr}--include '$str' "
224 if [ $usevserver = yes ]; then
225 for vserver in $vsnames; do
226 for vi in $vsinclude; do
227 str="${vi//__star__/*}"
228 str="$VROOTDIR/$vserver$str"
229 if [ -n "$str" ]; then
230 execstr="${execstr}--include '$str' "
232 warning "vsinclude statement '${vi//__star__/*}' will be ignored for VServer $vserver. $symlinks_warning"
240 # exclude everything else
241 [ "$include" != "" -o "$vsinclude" != "" ] && execstr="${execstr}--exclude '/*' "
243 # include client-part and server-part
244 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
247 if [ $test = 0 ]; then
248 output=`nice -n $nicelevel su -c "$execstr" 2>&1`
251 info "Successfully finished backing up source $label"
254 warning "Failed backup up source $label"