If you include= or exclude= a directory that is actually a symlink
[matthijs/upstream/backupninja-vserver.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 -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"
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 $host -l $user '$RDIFFBACKUP -V'"
40                 echo `ssh $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 check_consistency "destination" "$type" "$user" "$host"
93
94 ### CHECK CONFIG ###
95
96 # If vservers are configured, check that the ones listed in $vsnames do exist.
97 local usevserver=no
98 if [ $vservers_are_available = yes ]; then
99    if [ "$vsnames" = all ]; then
100       vsnames="$found_vservers"
101    else
102       if ! vservers_exist "$vsnames" ; then
103             fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
104       fi
105    fi
106    if [ -n "$vsinclude" ]; then
107       info "Using vservers '$vsnames'"
108       usevserver=yes
109    fi
110 else
111    [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
112 fi
113
114 # check the connection at the source and destination
115 if [ "$testconnect" = "yes" ] || [ "${test}" -eq 1 ]; then
116         test_connection $sourceuser $sourcehost
117         test_connection $destuser $desthost
118 fi
119
120 # see that rdiff-backup has the same version at the source and destination
121 sourceversion=`get_version $sourceuser $sourcehost`
122 destversion=`get_version $destuser $desthost`
123 if [ "$sourceversion" != "$destversion" ]; then
124         fatal "rdiff-backup does not have the same version at the source and at the destination."
125 fi
126
127 # source specific checks
128 [ "$include" != "" -o "$vsinclude" != "" ] || fatal "No source includes specified"
129 case $sourcetype in 
130         remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
131         local  ) execstr_sourcepart="/" ;;
132         *      ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
133 esac
134
135 # destination specific checks
136 [ "$destdir" != "" ] || fatal "Destination directory not set"
137 case $desttype in 
138         remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
139         local  ) execstr_destpart="$destdir/$label" ;;
140         *      ) fatal "desttype '$desttype' is neither local nor remote" ;;
141 esac
142         
143 ### REMOVE OLD BACKUPS ###
144
145 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
146         # add D if no other date unit is specified
147         keep="${keep}D"
148 fi
149
150 removestr="$RDIFFBACKUP --force --remove-older-than $keep "
151 if [ "$desttype" == "remote" ]; then
152         removestr="${removestr}${destuser}@${desthost}::"
153 fi
154 removestr="${removestr}${destdir}/${label}";
155
156 debug "$removestr"
157 if [ ! $test ]; then
158         output=`$removestr 2>&1`
159         if [ $? = 0 ]; then
160                 debug $output
161                 info "Removing backups older than $keep days succeeded."
162         else
163                 warning $output
164                 warning "Failed removing backups older than $keep."
165         fi
166 fi
167
168 ### EXECUTE ###
169
170 execstr="$RDIFFBACKUP $options --print-statistics "
171
172 set -o noglob
173
174 # TODO: order the includes and excludes
175 # excludes
176 for i in $exclude; do
177         i=`readlink -f $i`
178         str="${i//__star__/*}"
179         execstr="${execstr}--exclude '$str' "
180 done
181 # includes 
182 for i in $include; do
183         [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
184         i=`readlink -f $i`
185         str="${i//__star__/*}"
186         execstr="${execstr}--include '$str' "
187 done
188
189 # vsinclude
190 if [ $usevserver = yes ]; then
191    for vserver in $vsnames; do
192       for vi in $vsinclude; do
193          i=`readlink -f $VROOTDIR/$vserver$vi`
194          str="${i//__star__/*}"
195          execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
196       done
197    done
198 fi
199
200 set +o noglob
201
202 # exclude everything else
203 execstr="${execstr}--exclude '/*' "
204                 
205 # include client-part and server-part
206 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
207
208 debug "$execstr"
209 if [ ! $test ]; then
210         output=`nice -n $nicelevel su -c "$execstr" 2>&1`
211         if [ $? = 0 ]; then
212                 debug $output
213                 info "Successfully finished backing up source $label"
214         else
215                 warning $output
216                 warning "Failed backup up source $label"
217         fi
218 fi      
219
220 return 0