Make all indentation consistent.
[matthijs/upstream/backupninja.git] / handlers / wget
1 #
2 # backupninja handler to do incremental backups using
3 # wget and hardlinks, based on rsync handler
4 #
5 # feedback: rhatto at riseup.net | gpl
6 #
7 # Config file options
8 # -------------------
9 #
10 #   [general]
11 #   log = wget log file
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
21 #   tmp = temp folder
22 #
23 #   [source]
24 #   wget = wget program
25 #   wget_options = wget command options
26 #   url = remote data url
27 #   bandwidthlimit = set a badnwidth limit in kbps (remote source only)
28 #
29 #   [destination]
30 #   folder = local folder
31 #
32 # You can also specify some system comands if you don't want the default system values:
33 #
34 #   [system]
35 #   rm = rm command
36 #   cp = cp command
37 #   touch = touch command
38 #   mv = mv command
39 #   fsck = fsck command
40 #
41 # TODO: Daily, weekly and monthly snapshot rotation (like the one present on maildir handler).
42 #
43
44 # config file evaluation
45
46 setsection system
47 getconf rm rm
48 getconf cp cp
49 getconf touch touch
50 getconf mv mv
51 getconf fsck fsck
52
53 setsection general
54 getconf log /var/log/backup/wget.log
55 getconf partition
56 getconf fscheck
57 getconf read_only
58 getconf mountpoint
59 getconf backupdir
60 getconf rotate
61 getconf days
62 getconf lockfile
63 getconf nicelevel 0
64 getconf enable_mv_timestamp_bug no
65 getconf tmp /tmp
66
67 setsection source
68 getconf wget wget
69 getconf wget_options
70 getconf url
71 getconf bandwidthlimit
72
73 setsection destination
74 getconf folder
75
76 # function definitions
77
78 function rotate {
79
80    if [[ "$2" < 4 ]]; then
81       error "Rotate: minimum of 4 rotations"
82       exit 1
83    fi
84
85    if [ -d $1.$2 ]; then
86       $nice $mv /$1.$2 /$1.tmp
87    fi
88
89    for ((n=`echo "$2 - 1" | bc`; n >= 0; n--)); do
90       if [ -d $1.$n ]; then
91          dest=`echo "$n + 1" | bc`
92          $nice $mv /$1.$n /$1.$dest
93          $touch /$1.$dest
94       fi
95    done
96
97    if [ -d $1.tmp ]; then
98       $nice $mv /$1.tmp /$1.0
99    fi
100
101    if [ -d $1.1 ]; then
102       $nice $cp -alf /$1.1/. /$1.0
103    fi
104
105 }
106
107 function move_files {
108    ref=$tmp/makesnapshot-mymv-$$;
109    $touch -r $1 $ref;
110    $mv $1 $2;
111    $touch -r $ref $2;
112    $rm $ref;
113 }
114
115 backupdir="$mountpoint/$backupdir"
116
117 # does $backupdir exists?
118
119 if [ ! -d "$backupdir" ]; then
120    error "Backupdir $backupdir does not exist"
121    exit 1
122 fi
123
124 # setup number of increments
125
126 if [ -z "$days" ]; then
127    keep="4"
128 else
129    keep="`echo $days - 1 | bc -l`"
130 fi
131
132 # lockfile setup
133
134 if [ ! -z "$lockfile" ]; then
135    $touch $lockfile || warning "Could not create lockfile $lockfile"
136 fi
137
138 # nicelevel setup
139
140 if [ ! -z "$nicelevel" ]; then
141    nice="nice -n $nicelevel"
142 else
143    nice=""
144 fi
145
146 # set mv procedure
147
148 if [ $enable_mv_timestamp_bug == "yes" ]; then
149    mv=move_files
150 fi
151
152 # set excludes
153
154 for path in $exclude; do
155    EXCLUDES="$EXCLUDES --exclude=$path"
156 done
157
158 echo "Starting backup at `date`" >> $log
159
160 # mount backup destination folder as read-write
161
162 if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
163    if [ -d "$mountpoint" ]; then
164       mount -o remount,rw $mountpoint
165       if (($?)); then
166          error "Could not mount $mountpoint"
167          exit 1
168       fi
169    fi
170 fi
171
172 # the backup procedure
173
174 if [ ! -d "$backupdir/$folder/$folder.0" ]; then
175    mkdir -p $backupdir/$folder/$folder.0
176 fi
177
178 info "Rotating $backupdir/$folder/$folder..."
179 echo "Rotating $backupdir/$folder/$folder..." >> $log
180 rotate $backupdir/$folder/$folder $keep
181 info "Wget'ing $SECTION on $backupdir/$folder/$folder.0..."
182
183 if [ ! -z "$badnwidth" ]; then
184    limit_rate="--limit-rate=$badnwidth""k"
185 fi
186
187 cd $backupdir/$folder/$folder.0
188 wget $wget_options $limit-rate -r -c -N -e robots=off $url
189 cd -
190
191 $touch $backupdir/$folder/$folder.0
192
193 # remount backup destination as read-only
194
195 if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
196    mount -o remount,ro $mountpoint
197 fi
198
199 # check partition for errors
200
201 if [ "$fscheck" == "1" ] || [ "$fscheck" == "yes" ]; then
202    umount $mountpoint
203    if (($?)); then
204       warning "Could not umount $mountpoint to run fsck"
205    else
206       $nice $fsck -v -y $partition >> $log
207       mount $mountpoint
208    fi
209 fi
210
211 # removes the lockfile
212
213 if [ ! -z "$lockfile" ]; then
214    $rm $lockfile || warning "Could not remove lockfile $lockfile"
215 fi
216
217 echo "Finnishing backup at `date`" >> $log
218