79577c655ebd9c9c76e1b77d993281e18ff84ba5
[matthijs/upstream/backupninja.git] / handlers / rdiff
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 #
3 # rdiff-backup handler script for backupninja
4 # requires rdiff-backup
5 #
6
7 ### FUNCTIONS ###
8
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).
14         if [ $# -lt 2 ]; then
15                 debug "(local is assumed to be a good connection)"
16                 return 0
17         fi
18         local user=$1
19         local host=$2
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"
24         else
25                 fatal "Can't connect to $host as $user."
26         fi
27 }
28
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`
36         else
37                 local user=$1
38                 local host=$2
39                 debug "ssh $sshoptions $host -l $user '$RDIFFBACKUP -V'"
40                 echo `ssh $sshoptions $host -l $user "$RDIFFBACKUP -V | grep rdiff-backup"`
41         fi
42 }
43
44 function check_consistency() {
45         local section=$1
46         local type=$2
47         local user=$3
48         local host=$4
49         if [ "$type" == "local" ]; then
50                 if [ "$user" != "" ]; then
51                         warning "User should not be specified for local $section."
52                 fi
53                 if [ "$host" != "" ]; then
54                         warning "Host should not be specified for local $section."
55                 fi
56         fi
57         if [ "$type" == "remote" ]; then
58                 if [ "$user" == "" ]; then
59                         fatal "User must be specified for remote $section."
60                 fi
61                 if [ "host" == "" ]; then
62                         fatal "Host must be specifed for remote $section."
63                 fi
64         fi
65 }
66
67 ### GET CONFIG ###
68
69 getconf options
70 getconf testconnect yes
71 getconf nicelevel 0
72
73 setsection source
74 getconf type; sourcetype=$type
75 getconf user; sourceuser=$user
76 getconf host; sourcehost=$host
77 check_consistency "source" "$type" "$user" "$host"
78 getconf label
79 getconf keep 60
80 getconf include
81 getconf vsnames all
82 getconf vsinclude
83 getconf exclude
84
85 setsection dest
86 getconf directory; destdir=$directory
87 # strip trailing /
88 destdir=${destdir%/}
89 getconf type; desttype=$type
90 getconf user; destuser=$user
91 getconf host; desthost=$host
92 getconf sshoptions
93 check_consistency "destination" "$type" "$user" "$host"
94
95 ### CHECK CONFIG ###
96
97 # If vservers are configured, check that the ones listed in $vsnames do exist.
98 local usevserver=no
99 if [ $vservers_are_available = yes ]; then
100    if [ "$vsnames" = all ]; then
101       vsnames="$found_vservers"
102    else
103       if ! vservers_exist "$vsnames" ; then
104             fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
105       fi
106    fi
107    if [ -n "$vsinclude" ]; then
108       info "Using vservers '$vsnames'"
109       usevserver=yes
110    fi
111 else
112    [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
113 fi
114
115 # check the connection at the source and destination
116 [ -n "$test" ] || test=0
117 if [ "$testconnect" = "yes" ] || [ "${test}" -eq 1 ]; then
118         test_connection $sourceuser $sourcehost
119         test_connection $destuser $desthost
120 fi
121
122 # see that rdiff-backup has the same version at the source and destination
123 sourceversion=`get_version $sourceuser $sourcehost`
124 destversion=`get_version $destuser $desthost`
125 if [ "$sourceversion" != "$destversion" ]; then
126         fatal "rdiff-backup does not have the same version at the source and at the destination."
127 fi
128
129 # source specific checks
130 [ "$include" != "" -o "$vsinclude" != "" ] || fatal "No source includes specified"
131 case $sourcetype in 
132         remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
133         local  ) execstr_sourcepart="/" ;;
134         *      ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
135 esac
136
137 # destination specific checks
138 [ "$destdir" != "" ] || fatal "Destination directory not set"
139 case $desttype in 
140         remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
141         local  ) execstr_destpart="$destdir/$label" ;;
142         *      ) fatal "desttype '$desttype' is neither local nor remote" ;;
143 esac
144         
145 ### REMOVE OLD BACKUPS ###
146
147 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
148         # add D if no other date unit is specified
149         keep="${keep}D"
150 fi
151
152 removestr="$RDIFFBACKUP $options --force --remove-older-than $keep "
153 if [ "$desttype" == "remote" ]; then
154         removestr="${removestr}${destuser}@${desthost}::"
155 fi
156 removestr="${removestr}${destdir}/${label}";
157
158 debug "$removestr"
159 if [ $test = 0 ]; then
160         output="`su -c "$removestr" 2>&1`"
161         if [ $? = 0 ]; then
162                 debug $output
163                 info "Removing backups older than $keep days succeeded."
164         else
165                 warning $output
166                 warning "Failed removing backups older than $keep."
167         fi
168 fi
169
170 ### EXECUTE ###
171
172 execstr="$RDIFFBACKUP $options --print-statistics "
173
174 set -o noglob
175
176 symlinks_warning="Maybe you have mixed symlinks and '*' in this statement, which is not supported."
177
178 # TODO: order the includes and excludes
179 # excludes
180 for i in $exclude; do
181    str="${i//__star__/*}"
182    str=`readlink -f $str`
183    if [ -n "$str" ]; then
184       execstr="${execstr}--exclude '$str' "
185    else
186       warning "exclude statement '${i//__star__/*}' will be ignored. $symlinks_warning"
187    fi
188 done
189 # includes 
190 for i in $include; do
191    [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
192    str="${i//__star__/*}"
193    str=`readlink -f $str`
194    if [ -n "$str" ]; then
195       execstr="${execstr}--include '$str' "
196    else
197       warning "include statement '${i//__star__/*}' will be ignored. $symlinks_warning"
198    fi
199 done
200
201 # vsinclude
202 if [ $usevserver = yes ]; then
203    for vserver in $vsnames; do
204       for vi in $vsinclude; do
205          str="${vi//__star__/*}"
206          str=`readlink -f $VROOTDIR/$vserver$str`
207          if [ -n "$str" ]; then
208             execstr="${execstr}--include '$str' "
209          else
210             warning "vsinclude statement '${vi//__star__/*}' will be ignored for VServer $vserver. $symlinks_warning"
211          fi
212       done
213    done
214 fi
215
216 set +o noglob
217
218 # exclude everything else
219 execstr="${execstr}--exclude '/*' "
220                 
221 # include client-part and server-part
222 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
223
224 debug "$execstr"
225 if [ $test = 0 ]; then
226         output=`nice -n $nicelevel su -c "$execstr" 2>&1`
227         if [ $? = 0 ]; then
228                 debug $output
229                 info "Successfully finished backing up source $label"
230         else
231                 warning $output
232                 warning "Failed backup up source $label"
233         fi
234 fi      
235
236 return 0