2 # rdiff-backup handler script for backupninja
3 # requires rdiff-backup
8 function test_connection() {
9 # given a user and host,
10 # tests the connection.
11 # if user or host is missing, returns 0
12 # (ie, assume it's a local connection).
14 debug "(local is assumed to be a good connection)"
19 debug "ssh -o PasswordAuthentication=no $host -l $user 'echo -n 1'"
20 local ret=`ssh -o PasswordAuthentication=no $host -l $user 'echo -n host is alive'`
21 if echo $ret | grep "host is alive"; then
22 debug "Connected to $host as $user successfully"
24 fatal "Can't connect to $host as $user."
28 function get_version() {
29 # given no arguments, returns the local version.
30 # given a user and host, returns the remote version.
31 # if user or host is missing, returns the local version.
32 if [ "$#" -lt 2 ]; then
33 debug "$RDIFFBACKUP -V"
34 echo `$RDIFFBACKUP -V`
38 debug "ssh $host -l $user '$RDIFFBACKUP -V'"
39 echo `ssh $host -l $user "$RDIFFBACKUP -V | grep rdiff-backup"`
43 function check_consistency() {
48 if [ "$type" == "local" ]; then
49 if [ "$user" != "" ]; then
50 warning "User should not be specified for local $section."
52 if [ "$host" != "" ]; then
53 warning "Host should not be specified for local $section."
56 if [ "$type" == "remote" ]; then
57 if [ "$user" == "" ]; then
58 fatal "User must be specified for remote $section."
60 if [ "host" == "" ]; then
61 fatal "Host must be specifed for remote $section."
69 getconf testconnect yes
73 getconf type; sourcetype=$type
74 getconf user; sourceuser=$user
75 getconf host; sourcehost=$host
76 check_consistency "source" "$type" "$user" "$host"
84 getconf directory; destdir=$directory
87 getconf type; desttype=$type
88 getconf user; destuser=$user
89 getconf host; desthost=$host
90 check_consistency "destination" "$type" "$user" "$host"
94 # See if vservers are configured
95 if [ "$vservers" = "yes" ]
99 fatal "vservers enabled, but $VROOTDIR does not exist!"
101 info "vserver method enabled"
106 # check the connection at the source and destination
107 if [ "$testconnect" == "yes" -o $test ]; then
108 test_connection $sourceuser $sourcehost
109 test_connection $destuser $desthost
112 # see that rdiff-backup has the same version at the source and destination
113 sourceversion=`get_version $sourceuser $sourcehost`
114 destversion=`get_version $destuser $desthost`
115 if [ "$sourceversion" != "$destversion" ]; then
116 fatal "rdiff-backup does not have the same version at the source and at the destination."
119 # source specific checks
120 [ "$include" != "" -o "$vsinclude" != "" ] || fatal "No source includes specified"
121 #TODO should I test for vsinclude if usevservers=1?
123 remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
124 local ) execstr_sourcepart="/" ;;
125 * ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
128 # destination specific checks
129 [ "$destdir" != "" ] || fatal "Destination directory not set"
131 remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
132 local ) execstr_destpart="$destdir/$label" ;;
133 * ) fatal "desttype '$desttype' is neither local nor remote" ;;
136 ### REMOVE OLD BACKUPS ###
138 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
139 # add D if no other date unit is specified
143 removestr="$RDIFFBACKUP --force --remove-older-than $keep "
144 if [ "$desttype" == "remote" ]; then
145 removestr="${removestr}${destuser}@${desthost}::"
147 removestr="${removestr}${destdir}/${label}";
151 output=`$removestr 2>&1`
154 info "Removing backups older than $keep days succeeded."
157 warning "Failed removing backups older than $keep."
163 execstr="$RDIFFBACKUP $options --print-statistics "
165 # TODO: order the includes and excludes
167 for i in $exclude; do
168 str="${i//__star__/*}"
169 execstr="${execstr}--exclude '$str' "
172 for i in $include; do
173 [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
174 str="${i//__star__/*}"
175 execstr="${execstr}--include '$str' "
179 if [ $usevserver ]; then
180 for vserver in `ls $VROOTDIR|grep -v lost+found`; do
181 for vi in $vsinclude; do
182 str="${vi//__star__/*}"
183 execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
188 # exclude everything else
189 execstr="${execstr}--exclude '/*' "
191 # include client-part and server-part
192 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
196 output=`nice -n $nicelevel su -c "$execstr" 2>&1`
199 info "Successfully finished backing up source $label"
202 warning "Failed backup up source $label"