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 -o PasswordAuthentication=no $host -l $user 'echo -n 1'"
21 local ret=`ssh -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 $host -l $user '$RDIFFBACKUP -V'"
40 echo `ssh $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."
70 getconf testconnect yes
74 getconf type; sourcetype=$type
75 getconf user; sourceuser=$user
76 getconf host; sourcehost=$host
77 check_consistency "source" "$type" "$user" "$host"
85 getconf directory; destdir=$directory
88 getconf type; desttype=$type
89 getconf user; destuser=$user
90 getconf host; desthost=$host
91 check_consistency "destination" "$type" "$user" "$host"
95 # See if vservers are configured
97 if [ $vservers_are_available = yes ]; then
98 info "vserver method enabled"
102 # check the connection at the source and destination
103 if [ "$testconnect" = "yes" ] || [ "${test}" -eq 1 ]; then
104 test_connection $sourceuser $sourcehost
105 test_connection $destuser $desthost
108 # see that rdiff-backup has the same version at the source and destination
109 sourceversion=`get_version $sourceuser $sourcehost`
110 destversion=`get_version $destuser $desthost`
111 if [ "$sourceversion" != "$destversion" ]; then
112 fatal "rdiff-backup does not have the same version at the source and at the destination."
115 # source specific checks
116 [ "$include" != "" -o "$vsinclude" != "" ] || fatal "No source includes specified"
117 #TODO should I test for vsinclude if usevservers=yes?
119 remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
120 local ) execstr_sourcepart="/" ;;
121 * ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
124 # destination specific checks
125 [ "$destdir" != "" ] || fatal "Destination directory not set"
127 remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
128 local ) execstr_destpart="$destdir/$label" ;;
129 * ) fatal "desttype '$desttype' is neither local nor remote" ;;
132 ### REMOVE OLD BACKUPS ###
134 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
135 # add D if no other date unit is specified
139 removestr="$RDIFFBACKUP --force --remove-older-than $keep "
140 if [ "$desttype" == "remote" ]; then
141 removestr="${removestr}${destuser}@${desthost}::"
143 removestr="${removestr}${destdir}/${label}";
147 output=`$removestr 2>&1`
150 info "Removing backups older than $keep days succeeded."
153 warning "Failed removing backups older than $keep."
159 execstr="$RDIFFBACKUP $options --print-statistics "
161 # TODO: order the includes and excludes
163 for i in $exclude; do
164 str="${i//__star__/*}"
165 execstr="${execstr}--exclude '$str' "
168 for i in $include; do
169 [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
170 str="${i//__star__/*}"
171 execstr="${execstr}--include '$str' "
175 if [ $usevserver = yes ]; then
176 for vserver in `ls $VROOTDIR|grep -v lost+found`; do
177 for vi in $vsinclude; do
178 str="${vi//__star__/*}"
179 execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
184 # exclude everything else
185 execstr="${execstr}--exclude '/*' "
187 # include client-part and server-part
188 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
192 output=`nice -n $nicelevel su -c "$execstr" 2>&1`
195 info "Successfully finished backing up source $label"
198 warning "Failed backup up source $label"