Add autotools Makefile templates
[matthijs/upstream/backupninja.git] / handlers / rdiff
1 #
2 # rdiff-backup handler script for backupninja
3 # requires rdiff-backup
4 #
5
6 ### FUNCTIONS ###
7
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).
13         if [ $# -lt 2 ]; then
14                 debug "(local is assumed to be a good connection)"
15                 return 0
16         fi
17         local user=$1
18         local host=$2
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"
23         else
24                 fatal "Can't connect to $host as $user."
25         fi
26 }
27
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`
35         else
36                 local user=$1
37                 local host=$2
38                 debug "ssh $host -l $user '$RDIFFBACKUP -V'"
39                 echo `ssh $host -l $user "$RDIFFBACKUP -V | grep rdiff-backup"`
40         fi
41 }
42
43 function check_consistency() {
44         local section=$1
45         local type=$2
46         local user=$3
47         local host=$4
48         if [ "$type" == "local" ]; then
49                 if [ "$user" != "" ]; then
50                         warning "User should not be specified for local $section."
51                 fi
52                 if [ "$host" != "" ]; then
53                         warning "Host should not be specified for local $section."
54                 fi
55         fi
56         if [ "$type" == "remote" ]; then
57                 if [ "$user" == "" ]; then
58                         fatal "User must be specified for remote $section."
59                 fi
60                 if [ "host" == "" ]; then
61                         fatal "Host must be specifed for remote $section."
62                 fi
63         fi
64 }
65
66 ### GET CONFIG ###
67
68 getconf options
69 getconf testconnect yes
70 getconf nicelevel 0
71
72 setsection source
73 getconf type; sourcetype=$type
74 getconf user; sourceuser=$user
75 getconf host; sourcehost=$host
76 check_consistency "source" "$type" "$user" "$host"
77 getconf label
78 getconf keep 60
79 getconf include
80 getconf vsinclude
81 getconf exclude
82
83 setsection dest
84 getconf directory; destdir=$directory
85 # strip trailing /
86 destdir=${destdir%/}
87 getconf type; desttype=$type
88 getconf user; destuser=$user
89 getconf host; desthost=$host
90 check_consistency "destination" "$type" "$user" "$host"
91
92 ### CHECK CONFIG ###
93
94 # See if vservers are configured
95 if [ "$vservers" = "yes" ]
96 then
97         if [ ! -d $VROOTDIR ]
98         then
99                 fatal "vservers enabled, but $VROOTDIR does not exist!"
100         else
101                 info "vserver method enabled"
102                 usevserver=1
103         fi
104 fi
105
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
110 fi
111
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."
117 fi
118
119 # source specific checks
120 [ "$include" != "" -o "$vsinclude" != "" ] || fatal "No source includes specified"
121 #TODO should I test for vsinclude if usevservers=1?
122 case $sourcetype in 
123         remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
124         local  ) execstr_sourcepart="/" ;;
125         *      ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
126 esac
127
128 # destination specific checks
129 [ "$destdir" != "" ] || fatal "Destination directory not set"
130 case $desttype in 
131         remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
132         local  ) execstr_destpart="$destdir/$label" ;;
133         *      ) fatal "desttype '$desttype' is neither local nor remote" ;;
134 esac
135         
136 ### REMOVE OLD BACKUPS ###
137
138 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
139         # add D if no other date unit is specified
140         keep="${keep}D"
141 fi
142
143 removestr="$RDIFFBACKUP --force --remove-older-than $keep "
144 if [ "$desttype" == "remote" ]; then
145         removestr="${removestr}${destuser}@${desthost}::"
146 fi
147 removestr="${removestr}${destdir}/${label}";
148
149 debug "$removestr"
150 if [ ! $test ]; then
151         output=`$removestr 2>&1`
152         if [ $? = 0 ]; then
153                 debug $output
154                 info "Removing backups older than $keep days succeeded."
155         else
156                 warning $output
157                 warning "Failed removing backups older than $keep."
158         fi
159 fi
160
161 ### EXECUTE ###
162
163 execstr="$RDIFFBACKUP $options --print-statistics "
164
165 # TODO: order the includes and excludes
166 # excludes
167 for i in $exclude; do
168         str="${i//__star__/*}"
169         execstr="${execstr}--exclude '$str' "
170 done
171 # includes 
172 for i in $include; do
173         [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
174         str="${i//__star__/*}"
175         execstr="${execstr}--include '$str' "
176 done
177
178 # vsinclude
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' "
184                 done
185         done
186 fi
187
188 # exclude everything else
189 execstr="${execstr}--exclude '/*' "
190                 
191 # include client-part and server-part
192 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
193
194 debug "$execstr"
195 if [ ! $test ]; then
196         output=`nice -n $nicelevel su -c "$execstr" 2>&1`
197         if [ $? = 0 ]; then
198                 debug $output
199                 info "Successfully finished backing up source $label"
200         else
201                 warning $output
202                 warning "Failed backup up source $label"
203         fi
204 fi      
205
206 return 0