2 # backupninja handler to do incremental backups using
3 # wget and hardlinks, based on rsync handler
5 # feedback: rhatto at riseup.net | gpl
12 # partition = partition where the backup lives
13 # fscheck = set to 1 if fsck should run on $partition after the backup is made
14 # read_only = set to 1 if $partition is mounted read-only
15 # mountpoint = backup partition mountpoint or backup main folder
16 # backupdir = folder relative do $mountpoint where the backup should be stored
17 # days = number of backup increments (min = 5)
18 # lockfile = lockfile to be kept during backup execution
19 # nicelevel = wget command nice level
20 # enable_mv_timestamp_bug = set to "yes" if your system isnt handling timestamps correctly
25 # wget_options = wget command options
26 # url = remote data url
27 # bandwidthlimit = set a badnwidth limit in kbps (remote source only)
30 # folder = local folder
32 # You can also specify some system comands if you don't want the default system values:
37 # touch = touch command
41 # TODO: Daily, weekly and monthly snapshot rotation (like the one present on maildir handler).
44 # config file evaluation
54 getconf log /var/log/backup/wget.log
64 getconf enable_mv_timestamp_bug no
71 getconf bandwidthlimit
73 setsection destination
76 # function definitions
80 if [[ "$2" < 4 ]]; then
81 error "Rotate: minimum of 4 rotations"
86 $nice $mv /$1.$2 /$1.tmp
89 for ((n=`echo "$2 - 1" | bc`; n >= 0; n--)); do
91 dest=`echo "$n + 1" | bc`
92 $nice $mv /$1.$n /$1.$dest
97 if [ -d $1.tmp ]; then
98 $nice $mv /$1.tmp /$1.0
102 $nice $cp -alf /$1.1/. /$1.0
107 function move_files {
109 ref=$tmp/makesnapshot-mymv-$$;
117 backupdir="$mountpoint/$backupdir"
119 # does $backupdir exists?
121 if [ ! -d "$backupdir" ]; then
122 error "Backupdir $backupdir does not exist"
126 # setup number of increments
128 if [ -z "$days" ]; then
131 keep="`echo $days - 1 | bc -l`"
136 if [ ! -z "$lockfile" ]; then
137 $touch $lockfile || warning "Could not create lockfile $lockfile"
142 if [ ! -z "$nicelevel" ]; then
143 nice="nice -n $nicelevel"
150 if [ $enable_mv_timestamp_bug == "yes" ]; then
156 for path in $exclude; do
157 EXCLUDES="$EXCLUDES --exclude=$path"
160 echo "Starting backup at `date`" >> $log
162 # mount backup destination folder as read-write
164 if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
165 if [ -d "$mountpoint" ]; then
166 mount -o remount,rw $mountpoint
168 error "Could not mount $mountpoint"
174 # the backup procedure
176 if [ ! -d "$backupdir/$folder/$folder.0" ]; then
177 mkdir -p $backupdir/$folder/$folder.0
180 info "Rotating $backupdir/$folder/$folder..."
181 echo "Rotating $backupdir/$folder/$folder..." >> $log
182 rotate $backupdir/$folder/$folder $keep
183 info "Wget'ing $SECTION on $backupdir/$folder/$folder.0..."
185 if [ ! -z "$badnwidth" ]; then
186 limit_rate="--limit-rate=$badnwidth""k"
189 cd $backupdir/$folder/$folder.0
190 wget $wget_options $limit-rate -r -c -N -e robots=off $url
193 $touch $backupdir/$folder/$folder.0
195 # remount backup destination as read-only
197 if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
198 mount -o remount,ro $mountpoint
201 # check partition for errors
203 if [ "$fscheck" == "1" ] || [ "$fscheck" == "yes" ]; then
206 warning "Could not umount $mountpoint to run fsck"
208 $nice $fsck -v -y $partition >> $log
213 # removes the lockfile
215 if [ ! -z "$lockfile" ]; then
216 $rm $lockfile || warning "Could not remove lockfile $lockfile"
219 echo "Finnishing backup at `date`" >> $log