1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # rsync backup handler for backupninja
4 # requires rsync and optional freedups
7 # http://www.stearns.org/freedups/
8 # http://freshmeat.net/projects/freedups/
11 # http://samba.anu.edu.au/rsync/
16 # System commands used by this script
17 # replace with absolute path's if neccecary
33 getconf testconnect no
34 getconf srchost localhost
37 getconf bandwidthlimit 1000
38 getconf remote_rsync rsync
48 getconf enable_mv_timestamp_bug no
49 getconf freedups freedups
50 getconf enable_freedups no
51 getconf incremental yes
53 # Apparently, a bug in some Linux kernels between 2.4.4 and 2.4.9 causes mv to update timestamps;
54 # this may result in inaccurate timestamps on the snapshot directories.
55 # Set enable_mv_timestamp_bug=1 to enable this workaround
56 if [ $enable_mv_timestamp_bug == "yes" ]; then
61 ref=/tmp/makesnapshot-mymv-$$;
68 if [ $enable_freedups == "yes" ]; then
70 debug "Not implemented yet!"
74 [ "$directory" != "" ] || fatal "Destination directory not set"
75 [ "$include" != "" ] || fatal "No source includes specified"
77 ### vservers stuff ###
79 # If vservers are configured, check that the ones listed in $vsnames do exist.
81 if [ $vservers_are_available = yes ]; then
82 if [ "$vsnames" = all ]; then
83 vsnames="$found_vservers"
85 if ! vservers_exist "$vsnames" ; then
86 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
89 if [ -n "$vsinclude" ]; then
90 info "Using vservers '$vsnames'"
94 [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
95 [ -z "$vsnames" ] || warning 'vservers support disabled in backupninja.conf, vsnames configuration line will be ignored'
98 ### see if we can login ###
100 if [ "$testconnect" == "yes" ]; then
101 debug "$ssh $sshoptions -o PasswordAuthentication=no $srchost 'echo -n 1'"
103 result=`ssh $sshoptions -o PasswordAuthentication=no $srchost 'echo -n 1'`
104 if [ "$result" != "1" ]; then
105 fatal "Can't connect to $srchost."
107 debug "Connected to $srchost successfully"
112 ### COMMAND-LINE MANGLING ###
114 [ "$bandwidthlimit" == 1000 ] || options="$options --bwlimit=$bandwidthlimit"
115 [ "$numericids" == 1 ] || options="$options --numeric-ids "
116 [ "$compress" == 1 ] || options="$options --compress "
117 [ "$remote_rsync" == "rsync" ] || options="$options --rsync-path=$remote_rsync"
119 if [ "$nicelevel" -ne 0 ]; then
120 nice="nice -n $nicelevel" ;
125 execstr="$options --exclude '/' --delete-during --delete-excluded --archive $sshoptions "
127 if [ "$incremental" == "no" ]; then
128 execstr="${execstr} --whole-file "
131 execstr_serverpart="$srchost:/"
139 for i in $exclude; do
140 str="${i//__star__/*}"
141 #execstr="${execstr}--exclude '$str' "
142 execstr="${execstr}--exclude $str "
146 for i in $include; do
147 str="${i//__star__/*}"
148 #execstr="${execstr}--include '$str' "
149 execstr="${execstr}--include $str "
153 if [ $usevserver = yes ]; then
154 for vserver in $vsnames; do
155 for vi in $vsinclude; do
156 str="${vi//__star__/*}"
157 execstr="${execstr}--include '$label/$vserver$str' "
163 ### SNAPSHOT ROTATION ###
165 if [ "$incremental" == "yes" ]; then
166 debug "starting to rotate the old dirs"
168 # delete the oldest snapshot, if it exists:
169 debug "does $directory/$label/$keep exist?"
170 if [ -d "$directory/$label/$keep" ] ; then
171 debug "$rm -rf $directory/$label/$keep"
173 #$rm -rf "$directory/$label/$keep" ;
174 debug "$rm -rf $directory/$label/$keep";
178 # shift the snapshots(s) back by one, if they exist
179 for (( i=$keep; $i>=0; i--)) ; do
180 debug "does $directory/$label/$i exist?"
181 if [ -d "$directory/$label/$i" ] ; then
182 debug "$mv $directory/$label/$i $directory/$label/$(($i + 1))"
184 $mv "$directory/$label/$i" "$directory/$label/$(($i + 1))"
189 # make a hard-link-only (except for dirs) copy of
190 # assuming that exists, into the new dir
191 if [ -d "$directory/$label/1" ]; then
192 debug "$cp -al $directory/$label/1 $directory/$label/0"
194 $cp -al $directory/$label/1 $directory/$label/0 ;
205 # exclude everything else, start with root
206 #execstr="${execstr}--exclude '*' "
208 # include client-part and server-part
209 #execstr="$execstr $execstr_serverpart"
211 execstr=${execstr//\\*/\\\\\\*}
213 if [ "$debug" == "1" ]; then
214 execstr=" --verbose $execstr";
215 # execstr=" --verbose --dry-run $execstr";
217 execstr=" --quiet $execstr";
220 debug "$rsync $execstr $execstr_serverpart $directory/$label/0"
223 # rsync from the system into the latest snapshot (notice that
224 # rsync behaves like cp --remove-destination by default, so the destination
225 # is unlinked first. If it were not so, this would copy over the other
227 output=`$nice $rsync $execstr $execstr_serverpart $directory/$label/0 2>&1`
230 # update the mtime of the 0 dir to reflect the snapshot time
231 $touch $directory/$label/0
233 if [ $code -eq 0 ]; then
235 info "rsync finished successfully.";
237 debug "returncode $code : $output "
238 #fatal "rsync failed.";
239 warning "rsync failed.";