2009-05-22 15:27:09 +02:00
|
|
|
# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
|
|
|
|
# vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
|
2006-09-26 06:01:56 +02:00
|
|
|
#
|
|
|
|
# backupninja handler to do incremental backups using
|
|
|
|
# rsync and hardlinks, based on
|
|
|
|
#
|
|
|
|
# http://www.mikerubel.org/computers/rsync_snapshots/
|
|
|
|
#
|
|
|
|
# feedback: rhatto at riseup.net | gpl
|
2006-10-06 20:26:42 +02:00
|
|
|
# lot of enhancements grabbed from "rsnap" handler by paulv at bikkel.org
|
2006-09-26 06:01:56 +02:00
|
|
|
#
|
2006-10-06 20:26:42 +02:00
|
|
|
# Config file options
|
2006-09-26 06:01:56 +02:00
|
|
|
# -------------------
|
|
|
|
#
|
|
|
|
# [general]
|
|
|
|
# log = rsync log file
|
|
|
|
# partition = partition where the backup lives
|
2006-11-01 22:50:26 +01:00
|
|
|
# fscheck = set to 1 if fsck should run on $partition after the backup is made
|
2006-09-26 06:01:56 +02:00
|
|
|
# read_only = set to 1 if $partition is mounted read-only
|
|
|
|
# mountpoint = backup partition mountpoint or backup main folder
|
|
|
|
# backupdir = folder relative do $mountpoint where the backup should be stored
|
|
|
|
# days = number of backup increments (min = 5)
|
|
|
|
# lockfile = lockfile to be kept during backup execution
|
2007-11-06 22:20:36 +01:00
|
|
|
# nicelevel = rsync command nice level
|
2006-10-06 20:26:42 +02:00
|
|
|
# enable_mv_timestamp_bug = set to "yes" if your system isnt handling timestamps correctly
|
|
|
|
# tmp = temp folder
|
2006-09-26 06:01:56 +02:00
|
|
|
#
|
|
|
|
# [source]
|
2006-10-06 20:26:42 +02:00
|
|
|
# from = local or remote
|
2007-12-02 17:56:32 +01:00
|
|
|
# host = source hostname or ip, if remote backup
|
2006-10-06 20:26:42 +02:00
|
|
|
# testconnect = when "yes", test the connection for a remote source before backup
|
2006-09-26 06:01:56 +02:00
|
|
|
# include = include folder on backup
|
|
|
|
# exclude = exclude folder on backup
|
|
|
|
# ssh = ssh command line (remote only)
|
2006-10-06 20:26:42 +02:00
|
|
|
# rsync = rsync program
|
|
|
|
# rsync_options = rsync command options
|
2006-09-26 06:01:56 +02:00
|
|
|
# exclude_vserver = vserver-name (valid only if vservers = yes on backupninja.conf)
|
2006-10-06 20:26:42 +02:00
|
|
|
# numericids = when set to 1, use numeric ids instead of user/group mappings on rsync
|
|
|
|
# compress = if set to 1, compress data on rsync (remote source only)
|
|
|
|
# bandwidthlimit = set a badnwidth limit in kbps (remote source only)
|
2007-11-06 22:20:36 +01:00
|
|
|
# remote_rsync = remote rsync program (remote source only)
|
2006-09-26 06:01:56 +02:00
|
|
|
#
|
|
|
|
# [services]
|
|
|
|
# initscripts = absolute path where scripts are located
|
|
|
|
# service = script name to be stoped at the begining of the backup and started at its end
|
|
|
|
#
|
2006-11-01 22:50:26 +01:00
|
|
|
# You can also specify some system comands if you don't want the default system values:
|
2006-10-06 20:26:42 +02:00
|
|
|
#
|
|
|
|
# [system]
|
|
|
|
# rm = rm command
|
|
|
|
# cp = cp command
|
|
|
|
# touch = touch command
|
|
|
|
# mv = mv command
|
|
|
|
# fsck = fsck command
|
|
|
|
#
|
2006-09-26 06:01:56 +02:00
|
|
|
# You dont need to manually specify vservers using "include = /vservers".
|
2006-10-06 20:26:42 +02:00
|
|
|
# They are automatically backuped if vserver is set to "yes" on you backupninja.conf.
|
2006-09-26 06:01:56 +02:00
|
|
|
#
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# config file evaluation
|
|
|
|
|
|
|
|
setsection system
|
|
|
|
getconf rm rm
|
|
|
|
getconf cp cp
|
|
|
|
getconf touch touch
|
|
|
|
getconf mv mv
|
|
|
|
getconf fsck fsck
|
|
|
|
|
2006-09-26 06:01:56 +02:00
|
|
|
setsection general
|
2007-11-06 22:20:36 +01:00
|
|
|
getconf log /var/log/backup/rsync.log
|
2006-09-26 06:01:56 +02:00
|
|
|
getconf partition
|
2006-11-01 22:50:26 +01:00
|
|
|
getconf fscheck
|
2006-09-26 06:01:56 +02:00
|
|
|
getconf read_only
|
|
|
|
getconf mountpoint
|
|
|
|
getconf backupdir
|
|
|
|
getconf rotate
|
|
|
|
getconf days
|
|
|
|
getconf lockfile
|
2006-10-06 20:26:42 +02:00
|
|
|
getconf nicelevel 0
|
|
|
|
getconf enable_mv_timestamp_bug no
|
|
|
|
getconf tmp /tmp
|
2006-09-26 06:01:56 +02:00
|
|
|
|
|
|
|
setsection source
|
|
|
|
getconf from local
|
2006-10-06 20:26:42 +02:00
|
|
|
getconf testconnect no
|
|
|
|
getconf rsync $RSYNC
|
|
|
|
getconf rsync_options "-av --delete"
|
2006-09-26 06:01:56 +02:00
|
|
|
getconf ssh ssh
|
|
|
|
getconf user
|
|
|
|
getconf host
|
|
|
|
getconf include
|
|
|
|
getconf exclude
|
|
|
|
getconf exclude_vserver
|
2006-10-06 20:26:42 +02:00
|
|
|
getconf numericids 0
|
|
|
|
getconf compress 0
|
|
|
|
getconf bandwidthlimit
|
|
|
|
getconf remote_rsync rsync
|
2006-09-26 06:01:56 +02:00
|
|
|
|
|
|
|
setsection services
|
|
|
|
getconf initscripts
|
|
|
|
getconf service
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# function definitions
|
|
|
|
|
|
|
|
function rotate {
|
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
if [[ "$2" < 4 ]]; then
|
|
|
|
error "Rotate: minimum of 4 rotations"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d $1.$2 ]; then
|
|
|
|
$nice $mv /$1.$2 /$1.tmp
|
|
|
|
fi
|
|
|
|
|
|
|
|
for ((n=`echo "$2 - 1" | bc`; n >= 0; n--)); do
|
|
|
|
if [ -d $1.$n ]; then
|
|
|
|
dest=`echo "$n + 1" | bc`
|
|
|
|
$nice $mv /$1.$n /$1.$dest
|
|
|
|
$touch /$1.$dest
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -d $1.tmp ]; then
|
|
|
|
$nice $mv /$1.tmp /$1.0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d $1.1 ]; then
|
|
|
|
$nice $cp -alf /$1.1/. /$1.0
|
|
|
|
fi
|
2006-10-06 20:26:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function move_files {
|
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
ref=$tmp/makesnapshot-mymv-$$;
|
|
|
|
$touch -r $1 $ref;
|
|
|
|
$mv $1 $2;
|
|
|
|
$touch -r $ref $2;
|
|
|
|
$rm $ref;
|
2006-10-06 20:26:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-26 06:01:56 +02:00
|
|
|
backupdir="$mountpoint/$backupdir"
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# does $backupdir exists?
|
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
if [ ! -d "$backupdir" ]; then
|
|
|
|
error "Backupdir $backupdir does not exist"
|
|
|
|
exit 1
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# setup number of increments
|
|
|
|
|
2006-09-26 06:01:56 +02:00
|
|
|
if [ -z "$days" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
keep="4"
|
2006-09-26 06:01:56 +02:00
|
|
|
else
|
2009-05-22 14:50:52 +02:00
|
|
|
keep="`echo $days - 1 | bc -l`"
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# lockfile setup
|
|
|
|
|
2006-09-26 06:01:56 +02:00
|
|
|
if [ ! -z "$lockfile" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
$touch $lockfile || warning "Could not create lockfile $lockfile"
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# nicelevel setup
|
2006-09-26 06:01:56 +02:00
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
if [ ! -z "$nicelevel" ]; then
|
|
|
|
nice="nice -n $nicelevel"
|
|
|
|
else
|
|
|
|
nice=""
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# connection test
|
2006-09-26 06:01:56 +02:00
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
if [ "$from" == "remote" ] && [ "$testconnect" == "yes" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
debug "$ssh -o PasswordAuthentication=no $user@$host 'echo -n 1'"
|
|
|
|
result=`ssh -o PasswordAuthentication=no $user@$host 'echo -n 1'`
|
|
|
|
if [ "$result" != "1" ]; then
|
|
|
|
fatal "Can't connect to $host as $user."
|
|
|
|
else
|
|
|
|
debug "Connected to $srchost successfully"
|
|
|
|
fi
|
2006-10-06 20:26:42 +02:00
|
|
|
fi
|
2006-09-26 06:01:56 +02:00
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# rsync options for local sources
|
|
|
|
|
|
|
|
if [ "$from" == "local" ]; then
|
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
rsync_local_options="$rsync_options"
|
2006-10-06 20:26:42 +02:00
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
if [ ! -z "$numericids" ]; then
|
|
|
|
rsync_local_options="$rsync_local_options --numeric-ids "
|
|
|
|
fi
|
2006-09-26 06:01:56 +02:00
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
fi
|
2006-09-26 06:01:56 +02:00
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# rsync options for remote sources
|
|
|
|
|
|
|
|
if [ "$from" == "remote" ]; then
|
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
rsync_remote_options="$rsync_options --rsync-path=$remote_rsync"
|
2006-10-06 20:26:42 +02:00
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
if [ "$compress" == "1" ]; then
|
|
|
|
rsync_remote_options="$rsync_remote_options --compress"
|
|
|
|
fi
|
2006-09-26 06:01:56 +02:00
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
if [ ! -z "$bandwidthlimit" ]; then
|
|
|
|
rsync_remote_options="$rsync_remote_options --bwlimit=$bandwidthlimit"
|
|
|
|
fi
|
2006-09-26 06:01:56 +02:00
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
if [ ! -z "$numericids" ]; then
|
|
|
|
rsync_remote_options="$rsync_remote_options --numeric-ids"
|
|
|
|
fi
|
2006-10-06 20:26:42 +02:00
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
# set mv procedure
|
|
|
|
|
|
|
|
if [ $enable_mv_timestamp_bug == "yes" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
mv=move_files
|
2006-10-06 20:26:42 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# set excludes
|
|
|
|
|
|
|
|
for path in $exclude; do
|
2009-05-22 14:50:52 +02:00
|
|
|
EXCLUDES="$EXCLUDES --exclude=$path"
|
2006-10-06 20:26:42 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
# stop services
|
|
|
|
|
|
|
|
if [ ! -z "$service" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
for daemon in $service; do
|
|
|
|
info "Stopping service $daemon..."
|
|
|
|
$initscripts/$daemon stop
|
|
|
|
done
|
2006-10-06 20:26:42 +02:00
|
|
|
fi
|
2006-09-26 06:01:56 +02:00
|
|
|
|
|
|
|
echo "Starting backup at `date`" >> $log
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# mount backup destination folder as read-write
|
|
|
|
|
2006-09-26 06:01:56 +02:00
|
|
|
if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
if [ -d "$mountpoint" ]; then
|
|
|
|
mount -o remount,rw $mountpoint
|
|
|
|
if (($?)); then
|
|
|
|
error "Could not mount $mountpoint"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# add vservers to included folders
|
|
|
|
|
2006-09-26 06:01:56 +02:00
|
|
|
if [ "$vservers_are_available" == "yes" ]; then
|
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
# sane permission on backup
|
|
|
|
mkdir -p $backupdir/$VROOTDIR
|
|
|
|
chmod 000 $backupdir/$VROOTDIR
|
|
|
|
|
|
|
|
for candidate in $found_vservers; do
|
|
|
|
candidate="`basename $candidate`"
|
|
|
|
found_excluded_vserver="0"
|
|
|
|
for excluded_vserver in $exclude_vserver; do
|
|
|
|
if [ "$excluded_vserver" == "$candidate" ]; then
|
|
|
|
found_excluded_vserver="1"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ "$found_excluded_vserver" == "0" ]; then
|
|
|
|
include="$include $VROOTDIR/$candidate"
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
2009-05-22 14:50:52 +02:00
|
|
|
done
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# the backup procedure
|
|
|
|
|
2006-09-26 06:01:56 +02:00
|
|
|
for SECTION in $include; do
|
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
section="`basename $SECTION`"
|
|
|
|
|
|
|
|
if [ ! -d "$backupdir/$SECTION/$section.0" ]; then
|
|
|
|
mkdir -p $backupdir/$SECTION/$section.0
|
|
|
|
fi
|
|
|
|
|
|
|
|
info "Rotating $backupdir/$SECTION/$section..."
|
|
|
|
echo "Rotating $backupdir/$SECTION/$section..." >> $log
|
|
|
|
rotate $backupdir/$SECTION/$section $keep
|
|
|
|
info "Syncing $SECTION on $backupdir/$SECTION/$section.0..."
|
|
|
|
|
|
|
|
if [ "$from" == "local" ]; then
|
|
|
|
debug $rsync $rsync_local_options $EXCLUDES /$SECTION/ $backupdir/$SECTION/$section.0/
|
|
|
|
$nice $rsync $rsync_local_options $EXCLUDES /$SECTION/ $backupdir/$SECTION/$section.0/ >> $log
|
2006-09-26 06:01:56 +02:00
|
|
|
if [ "$?" != "0" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
warning "Rsync error when trying to transfer $SECTION"
|
|
|
|
fi
|
|
|
|
elif [ "$from" == "remote" ]; then
|
|
|
|
if [ -z "$user" ] || [ -z "$host" ]; then
|
|
|
|
error "Config file error: either user or host was not specified"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
debug $nice $rsync $rsync_remote_options $EXCLUDES -e "$ssh" $user@$host:/$SECTION/ $backupdir/$SECTION/$section.0
|
|
|
|
$nice $rsync $rsync_remote_options $EXCLUDES -e "$ssh" $user@$host:/$SECTION/ $backupdir/$SECTION/$section.0 >> $log
|
|
|
|
if [ "$?" != "0" ]; then
|
|
|
|
warning "Rsync error when trying to transfer $SECTION"
|
|
|
|
fi
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
2009-05-22 14:50:52 +02:00
|
|
|
else
|
|
|
|
error "Invalid source $from"
|
|
|
|
exit 1
|
|
|
|
fi
|
2006-09-26 06:01:56 +02:00
|
|
|
|
2009-05-22 14:50:52 +02:00
|
|
|
$touch $backupdir/$SECTION/$section.0
|
2006-09-26 06:01:56 +02:00
|
|
|
|
|
|
|
done
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# remount backup destination as read-only
|
|
|
|
|
2006-09-26 06:01:56 +02:00
|
|
|
if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
mount -o remount,ro $mountpoint
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# check partition for errors
|
|
|
|
|
2006-11-01 22:50:26 +01:00
|
|
|
if [ "$fscheck" == "1" ] || [ "$fscheck" == "yes" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
umount $mountpoint
|
|
|
|
if (($?)); then
|
|
|
|
warning "Could not umount $mountpoint to run fsck"
|
|
|
|
else
|
|
|
|
$nice $fsck -v -y $partition >> $log
|
|
|
|
mount $mountpoint
|
|
|
|
fi
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# restart services
|
|
|
|
|
2006-09-26 06:01:56 +02:00
|
|
|
if [ ! -z "$service" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
for daemon in $service; do
|
|
|
|
info "Starting service $daemon..."
|
|
|
|
$initscripts/$daemon start
|
|
|
|
done
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
|
|
|
|
2006-10-06 20:26:42 +02:00
|
|
|
# removes the lockfile
|
|
|
|
|
2006-09-26 06:01:56 +02:00
|
|
|
if [ ! -z "$lockfile" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
$rm $lockfile || warning "Could not remove lockfile $lockfile"
|
2006-09-26 06:01:56 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Finnishing backup at `date`" >> $log
|
|
|
|
|