Added the "Emacs comment line" on top of every shell file.
[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 -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 vsinclude
82 getconf exclude
83
84 setsection dest
85 getconf directory; destdir=$directory
86 # strip trailing /
87 destdir=${destdir%/}
88 getconf type; desttype=$type
89 getconf user; destuser=$user
90 getconf host; desthost=$host
91 check_consistency "destination" "$type" "$user" "$host"
92
93 ### CHECK CONFIG ###
94
95 # See if vservers are configured
96 if [ "$vservers" = "yes" ]
97 then
98         if [ ! -d $VROOTDIR ]
99         then
100                 fatal "vservers enabled, but $VROOTDIR does not exist!"
101         else
102                 info "vserver method enabled"
103                 usevserver=1
104         fi
105 fi
106
107 # check the connection at the source and destination
108 if [ "$testconnect" = "yes" ] || [ "${test}" -eq 1 ]; then
109         test_connection $sourceuser $sourcehost
110         test_connection $destuser $desthost
111 fi
112
113 # see that rdiff-backup has the same version at the source and destination
114 sourceversion=`get_version $sourceuser $sourcehost`
115 destversion=`get_version $destuser $desthost`
116 if [ "$sourceversion" != "$destversion" ]; then
117         fatal "rdiff-backup does not have the same version at the source and at the destination."
118 fi
119
120 # source specific checks
121 [ "$include" != "" -o "$vsinclude" != "" ] || fatal "No source includes specified"
122 #TODO should I test for vsinclude if usevservers=1?
123 case $sourcetype in 
124         remote ) execstr_sourcepart="$sourceuser@$sourcehost::/" ;;
125         local  ) execstr_sourcepart="/" ;;
126         *      ) fatal "sourcetype '$sourcetype' is neither local nor remote" ;;
127 esac
128
129 # destination specific checks
130 [ "$destdir" != "" ] || fatal "Destination directory not set"
131 case $desttype in 
132         remote ) execstr_destpart="$destuser@$desthost::$destdir/$label" ;;
133         local  ) execstr_destpart="$destdir/$label" ;;
134         *      ) fatal "desttype '$desttype' is neither local nor remote" ;;
135 esac
136         
137 ### REMOVE OLD BACKUPS ###
138
139 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
140         # add D if no other date unit is specified
141         keep="${keep}D"
142 fi
143
144 removestr="$RDIFFBACKUP --force --remove-older-than $keep "
145 if [ "$desttype" == "remote" ]; then
146         removestr="${removestr}${destuser}@${desthost}::"
147 fi
148 removestr="${removestr}${destdir}/${label}";
149
150 debug "$removestr"
151 if [ ! $test ]; then
152         output=`$removestr 2>&1`
153         if [ $? = 0 ]; then
154                 debug $output
155                 info "Removing backups older than $keep days succeeded."
156         else
157                 warning $output
158                 warning "Failed removing backups older than $keep."
159         fi
160 fi
161
162 ### EXECUTE ###
163
164 execstr="$RDIFFBACKUP $options --print-statistics "
165
166 # TODO: order the includes and excludes
167 # excludes
168 for i in $exclude; do
169         str="${i//__star__/*}"
170         execstr="${execstr}--exclude '$str' "
171 done
172 # includes 
173 for i in $include; do
174         [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
175         str="${i//__star__/*}"
176         execstr="${execstr}--include '$str' "
177 done
178
179 # vsinclude
180 if [ $usevserver ]; then
181         for vserver in `ls $VROOTDIR|grep -v lost+found`; do
182                 for vi in $vsinclude; do
183                         str="${vi//__star__/*}"
184                         execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
185                 done
186         done
187 fi
188
189 # exclude everything else
190 execstr="${execstr}--exclude '/*' "
191                 
192 # include client-part and server-part
193 execstr="${execstr}$execstr_sourcepart $execstr_destpart"
194
195 debug "$execstr"
196 if [ ! $test ]; then
197         output=`nice -n $nicelevel su -c "$execstr" 2>&1`
198         if [ $? = 0 ]; then
199                 debug $output
200                 info "Successfully finished backing up source $label"
201         else
202                 warning $output
203                 warning "Failed backup up source $label"
204         fi
205 fi      
206
207 return 0