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 # fscheck = 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 # nicelevel = rsync command nice level
23 # enable_mv_timestamp_bug = set to "yes" if your system isnt handling timestamps correctly
27 # from = local or remote
28 # testconnect = when "yes", test the connection for a remote source before backup
29 # include = include folder on backup
30 # exclude = exclude folder on backup
31 # ssh = ssh command line (remote only)
32 # rsync = rsync program
33 # rsync_options = rsync command options
34 # exclude_vserver = vserver-name (valid only if vservers = yes on backupninja.conf)
35 # numericids = when set to 1, use numeric ids instead of user/group mappings on rsync
36 # compress = if set to 1, compress data on rsync (remote source only)
37 # bandwidthlimit = set a badnwidth limit in kbps (remote source only)
38 # remote_rsync = remote rsync program (remote source only)
41 # initscripts = absolute path where scripts are located
42 # service = script name to be stoped at the begining of the backup and started at its end
44 # You can also specify some system comands if you don't want the default system values:
49 # touch = touch command
53 # You dont need to manually specify vservers using "include = /vservers".
54 # They are automatically backuped if vserver is set to "yes" on you backupninja.conf.
57 # config file evaluation
67 getconf log /var/log/backup/rsync.log
77 getconf enable_mv_timestamp_bug no
82 getconf testconnect no
84 getconf rsync_options "-av --delete"
90 getconf exclude_vserver
93 getconf bandwidthlimit
94 getconf remote_rsync rsync
100 # function definitions
104 if [[ "$2" < 4 ]]; then
105 error "Rotate: minimum of 4 rotations"
109 if [ -d $1.$2 ]; then
110 $nice $mv /$1.$2 /$1.tmp
113 for ((n=`echo "$2 - 1" | bc`; n >= 0; n--)); do
114 if [ -d $1.$n ]; then
115 dest=`echo "$n + 1" | bc`
116 $nice $mv /$1.$n /$1.$dest
121 if [ -d $1.tmp ]; then
122 $nice $mv /$1.tmp /$1.0
126 $nice $cp -alf /$1.1/. /$1.0
131 function move_files {
133 ref=$tmp/makesnapshot-mymv-$$;
141 backupdir="$mountpoint/$backupdir"
143 # does $backupdir exists?
145 if [ ! -d "$backupdir" ]; then
146 error "Backupdir $backupdir does not exist"
150 # setup number of increments
152 if [ -z "$days" ]; then
155 keep="`echo $days - 1 | bc -l`"
160 if [ ! -z "$lockfile" ]; then
161 $touch $lockfile || warning "Could not create lockfile $lockfile"
166 if [ ! -z "$nicelevel" ]; then
167 nice="nice -n $nicelevel"
174 if [ "$from" == "remote" ] && [ "$testconnect" == "yes" ]; then
175 debug "$ssh -o PasswordAuthentication=no $user@$host 'echo -n 1'"
176 result=`ssh -o PasswordAuthentication=no $user@$host 'echo -n 1'`
177 if [ "$result" != "1" ]; then
178 fatal "Can't connect to $host as $user."
180 debug "Connected to $srchost successfully"
184 # rsync options for local sources
186 if [ "$from" == "local" ]; then
188 rsync_local_options="$rsync_options"
190 if [ ! -z "$numericids" ]; then
191 rsync_local_options="$rsync_local_options --numeric-ids "
196 # rsync options for remote sources
198 if [ "$from" == "remote" ]; then
200 rsync_remote_options="$rsync_options --rsync-path=$remote_rsync"
202 if [ "$compress" == "1" ]; then
203 rsync_remote_options="$rsync_remote_options --compress"
206 if [ ! -z "$bandwidthlimit" ]; then
207 rsync_remote_options="$rsync_remote_options --bwlimit=$bandwidthlimit"
210 if [ ! -z "$numericids" ]; then
211 rsync_remote_options="$rsync_remote_options --numeric-ids"
218 if [ $enable_mv_timestamp_bug == "yes" ]; then
224 for path in $exclude; do
225 EXCLUDES="$EXCLUDES --exclude=$path"
230 if [ ! -z "$service" ]; then
231 for daemon in $service; do
232 info "Stopping service $daemon..."
233 $initscripts/$daemon stop
237 echo "Starting backup at `date`" >> $log
239 # mount backup destination folder as read-write
241 if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
242 if [ -d "$mountpoint" ]; then
243 mount -o remount,rw $mountpoint
245 error "Could not mount $mountpoint"
251 # add vservers to included folders
253 if [ "$vservers_are_available" == "yes" ]; then
255 # sane permission on backup
256 mkdir -p $backupdir/$VROOTDIR
257 chmod 000 $backupdir/$VROOTDIR
259 for candidate in $found_vservers; do
260 candidate="`basename $candidate`"
261 found_excluded_vserver="0"
262 for excluded_vserver in $exclude_vserver; do
263 if [ "$excluded_vserver" == "$candidate" ]; then
264 found_excluded_vserver="1"
268 if [ "$found_excluded_vserver" == "0" ]; then
269 include="$include $VROOTDIR/$candidate"
274 # the backup procedure
276 for SECTION in $include; do
278 section="`basename $SECTION`"
280 if [ ! -d "$backupdir/$SECTION/$section.0" ]; then
281 mkdir -p $backupdir/$SECTION/$section.0
284 info "Rotating $backupdir/$SECTION/$section..."
285 echo "Rotating $backupdir/$SECTION/$section..." >> $log
286 rotate $backupdir/$SECTION/$section $keep
287 info "Syncing $SECTION on $backupdir/$SECTION/$section.0..."
289 if [ "$from" == "local" ]; then
290 debug $rsync $rsync_local_options $EXCLUDES /$SECTION/ $backupdir/$SECTION/$section.0/
291 $nice $rsync $rsync_local_options $EXCLUDES /$SECTION/ $backupdir/$SECTION/$section.0/ >> $log
292 if [ "$?" != "0" ]; then
293 warning "Rsync error when trying to transfer $SECTION"
295 elif [ "$from" == "remote" ]; then
296 if [ -z "$user" ] || [ -z "$host" ]; then
297 error "Config file error: either user or host was not specified"
300 debug $nice $rsync $rsync_remote_options $EXCLUDES -e "$ssh" $user@$host:/$SECTION/ $backupdir/$SECTION/$section.0
301 $nice $rsync $rsync_remote_options $EXCLUDES -e "$ssh" $user@$host:/$SECTION/ $backupdir/$SECTION/$section.0 >> $log
302 if [ "$?" != "0" ]; then
303 warning "Rsync error when trying to transfer $SECTION"
307 error "Invalid source $from"
311 $touch $backupdir/$SECTION/$section.0
315 # remount backup destination as read-only
317 if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
318 mount -o remount,ro $mountpoint
321 # check partition for errors
323 if [ "$fscheck" == "1" ] || [ "$fscheck" == "yes" ]; then
326 warning "Could not umount $mountpoint to run fsck"
328 $nice $fsck -v -y $partition >> $log
335 if [ ! -z "$service" ]; then
336 for daemon in $service; do
337 info "Starting service $daemon..."
338 $initscripts/$daemon start
342 # removes the lockfile
344 if [ ! -z "$lockfile" ]; then
345 $rm $lockfile || warning "Could not remove lockfile $lockfile"
348 echo "Finnishing backup at `date`" >> $log