2 # backupninja handler to do incremental backups using
3 # rsync and hardlinks, based on
5 # http://www.mikerubel.org/computers/rsync_snapshots/
7 # feedback: rhatto at riseup.net | gpl
8 # lot of enhancements grabbed from "rsnap" handler by paulv at bikkel.org
14 # log = rsync log file
15 # partition = partition where the backup lives
16 # fsck = set to 1 if fsck should run on $partition after the backup is made
17 # read_only = set to 1 if $partition is mounted read-only
18 # mountpoint = backup partition mountpoint or backup main folder
19 # backupdir = folder relative do $mountpoint where the backup should be stored
20 # days = number of backup increments (min = 5)
21 # lockfile = lockfile to be kept during backup execution
22 # enable_mv_timestamp_bug = set to "yes" if your system isnt handling timestamps correctly
26 # from = local or remote
27 # testconnect = when "yes", test the connection for a remote source before backup
28 # include = include folder on backup
29 # exclude = exclude folder on backup
30 # ssh = ssh command line (remote only)
31 # rsync = rsync program
32 # rsync_options = rsync command options
33 # exclude_vserver = vserver-name (valid only if vservers = yes on backupninja.conf)
34 # numericids = when set to 1, use numeric ids instead of user/group mappings on rsync
35 # compress = if set to 1, compress data on rsync (remote source only)
36 # bandwidthlimit = set a badnwidth limit in kbps (remote source only)
37 # remote_rsync = remove rsync program (remote source only)
40 # initscripts = absolute path where scripts are located
41 # service = script name to be stoped at the begining of the backup and started at its end
43 # You can also specify some system comands:
48 # touch = touch command
52 # You dont need to manually specify vservers using "include = /vservers".
53 # They are automatically backuped if vserver is set to "yes" on you backupninja.conf.
56 # config file evaluation
66 getconf log /var/log/backupninja-rub.log
76 getconf enable_mv_timestamp_bug no
81 getconf testconnect no
83 getconf rsync_options "-av --delete"
89 getconf exclude_vserver
92 getconf bandwidthlimit
93 getconf remote_rsync rsync
99 # function definitions
103 # TODO: force to an absolute path
105 if [[ "$2" < 4 ]]; then
106 error "Rotate: minimum of 4 rotations"
110 if [ -d $1.$2 ]; then
111 $nice $mv $1.$2 $1.tmp
114 for ((n=`echo "$2 - 1" | bc`; n >= 0; n--)); do
115 if [ -d $1.$n ]; then
116 dest=`echo "$n + 1" | bc`
117 $nice $mv $1.$n $1.$dest
122 if [ -d $1.tmp ]; then
123 $nice $mv $1.tmp $1.0
127 $nice $cp -alf $1.1/. $1.0
132 function move_files {
134 ref=$tmp/makesnapshot-mymv-$$;
142 backupdir="$mountpoint/$backupdir"
144 # does $backupdir exists?
146 if [ ! -d "$backupdir" ]; then
147 error "Backupdir $backupdir does not exist"
151 # setup number of increments
153 if [ -z "$days" ]; then
156 keep="`echo $days - 1 | bc -l`"
161 if [ ! -z "$lockfile" ]; then
162 $touch $lockfile || warning "Could not create lockfile $lockfile"
167 if [ ! -z "$nicelevel" ]; then
168 nice="nice -n $nicelevel"
175 if [ "$from" == "remote" ] && [ "$testconnect" == "yes" ]; then
176 debug "$ssh -o PasswordAuthentication=no $user@$host 'echo -n 1'"
177 result=`ssh -o PasswordAuthentication=no $user@$host 'echo -n 1'`
178 if [ "$result" != "1" ]; then
179 fatal "Can't connect to $host as $user."
181 debug "Connected to $srchost successfully"
185 # rsync options for local sources
187 if [ "$from" == "local" ]; then
189 rsync_local_options="$rsync_options"
191 if [ ! -z "$numericids" ]; then
192 rsync_local_options="$rsync_local_options --numeric-ids "
197 # rsync options for remote sources
199 if [ "$from" == "remote" ]; then
201 rsync_remote_options="$rsync_options --rsync-path=$remote_rsync"
203 if [ "$compress" == "1" ]; then
204 rsync_remote_options="$rsync_remote_options --compress"
207 fi [ ! -z "$bandwidthlimit" ]; then
208 rsync_remote_options="$rsync_remote_options --bwlimit=$bandwidthlimit"
211 if [ ! -z "$numericids" ]; then
212 rsync_remote_options="$rsync_remote_options --numeric-ids"
219 if [ $enable_mv_timestamp_bug == "yes" ]; then
225 for path in $exclude; do
226 EXCLUDES="$EXCLUDES --exclude=$path"
231 if [ ! -z "$service" ]; then
232 for daemon in $service; do
233 info "Stopping service $daemon..."
234 $initscripts/$daemon stop
238 echo "Starting backup at `date`" >> $log
240 # mount backup destination folder as read-write
242 if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
243 if [ -d "$mountpoint" ]; then
244 mount -o remount,rw $mountpoint
246 error "Could not mount $mountpoint"
252 # add vservers to included folders
254 if [ "$vservers_are_available" == "yes" ]; then
256 # sane permission on backup
257 mkdir -p $backupdir/$VROOTDIR
258 chmod 000 $backupdir/$VROOTDIR
260 for candidate in `ls $VROOTDIR`; do
261 found_excluded_vserver="0"
262 if [ "$candidate" != "lost+found" ]; then
263 for excluded_vserver in $exclude_vserver; do
264 if [ "$excluded_vserver" == "$candidate" ]; then
265 found_excluded_vserver="1"
269 if [ "$found_excluded_vserver" == "0" ]; then
270 include="$include $VROOTDIR/$candidate"
276 # the backup procedure
278 for SECTION in $include; do
280 section="`basename $SECTION`"
282 if [ ! -d "$backupdir/$SECTION/$section.0" ]; then
283 mkdir -p $backupdir/$SECTION/$section.0
286 info "Rotating $backupdir/$SECTION/$section..."
287 echo "Rotating $backupdir/$SECTION/$section..." >> $log
288 rotate $backupdir/$SECTION/$section $keep
289 info "Syncing $SECTION on $backupdir/$SECTION/$section.0..."
291 if [ "$from" == "local" ]; then
292 debug $rsync $rsync_local_options $EXCLUDES /$SECTION/ $backupdir/$SECTION/$section.0/
293 $nice $rsync $rsync_local_options $EXCLUDES /$SECTION/ $backupdir/$SECTION/$section.0/ >> $log
294 if [ "$?" != "0" ]; then
295 warning "Rsync error when trying to transfer $SECTION"
297 elif [ "$from" == "remote" ]; then
298 if [ -z "$user" ] || [ -z "$host" ]; then
299 error "Config file error: either user or host was not specified"
302 debug $nice $rsync $rsync_remote_options $EXCLUDES -e "$ssh" $user@$host:/$SECTION/ $backupdir/$SECTION/$section.0
303 $nice $rsync $rsync_remote_options $EXCLUDES -e "$ssh" $user@$host:/$SECTION/ $backupdir/$SECTION/$section.0 >> $log
304 if [ "$?" != "0" ]; then
305 warning "Rsync error when trying to transfer $SECTION"
309 error "Invalid source $from"
313 $touch $backupdir/$SECTION/$section.0
317 # remount backup destination as read-only
319 if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
320 mount -o remount,ro $mountpoint
323 # check partition for errors
325 if [ "$fsck" == "1" ] || [ "$fsck" == "yes" ]; then
328 warning "Could not umount $mountpoint to run fsck"
330 $nice $fsck -v -y $partition >> $log
337 if [ ! -z "$service" ]; then
338 for daemon in $service; do
339 info "Starting service $daemon..."
340 $initscripts/$daemon start
344 # removes the lockfile
346 if [ ! -z "$lockfile" ]; then
347 $rm $lockfile || warning "Could not remove lockfile $lockfile"
350 echo "Finnishing backup at `date`" >> $log