fix 2 bugs with exclude handling
[rofl0r-filesync.git] / monthly-backup-incremental.sh
blob323362d85873e548de832a6a8abc50573a154ea8
1 #!/bin/sh
2 # Copyright (C) 2012 rofl0r
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 # this program will use 2 directories for backups
19 # month0 and month1
20 # the current month is taken and modulo 2 applied. so for example
21 # if the month is march, 3 % 2 == 1, the backup will be placed in
22 # month1 directory.
23 # inside this directory, a file "backup.month" will be checked
24 # if the content is equal to the current month, 3 in our case,
25 # a normal "daily" backup will be created. if not, the contents of the
26 # directory will be trashed, and a "full" backup initiated.
27 # the full backup is an exact copy of the source folder, and will be placed
28 # in a directory called "full"
29 # the daily backup will compare the source folder with the full backup
30 # and put the differences inside a folder "dayXX", where XX is equivalent
31 # to the number of the day. i.e. 13 if launched on march 13th.
32 # the daily backup will contain all files changed or added since the last
33 # full backup.
35 # using this strategy will lead to an incremental backup covering at least
36 # one entire month, and maximum 2 months - 1 day.
37 # given a relatively static set of data, this should take 2-3 times the size
38 # of the source directory.
40 # if you want to cover more than 2 months, just change the variable
41 # month_cycles accordingly (either 2,3,4,6 or 12 make sense).
42 # be aware that changing this variable will possibly overwrite existing
43 # backups.
45 backup_source=/srv/
46 backup_root=/mnt/backup/
47 filesync_cmd=filesync
48 month_cycles=2
50 month=`date +%m`
51 day=`date +%d`
52 month_mod=`expr $month % $month_cycles`
53 backup_dir="${backup_root}/month${month_mod}/"
54 backup_full="${backup_dir}/full/"
55 backup_day="${backup_dir}/day${day}/"
56 backup_cfg="${backup_dir}/backup.month"
57 log_dir="${backup_dir}/logs/"
59 do_backup_full() {
60 rm -rf "$backup_full"
61 rm -rf "${backup_dir}/day*"
62 rm -rf "${log_dir}/*"
63 echo $month > "$backup_cfg"
64 # we write to a logfile to prevent getting the stdout mailed by cron
65 # which would mean megabytes per run on a big number of files
66 # this way the logs will be cleared together with the data after
67 # completion of a cycle.
68 logfile="${log_dir}/full.log"
69 "$filesync_cmd" -e -d -f "$backup_source" "$backup_full" > "$logfile"
72 do_backup_daily() {
73 logfile="${log_dir}/day${day}.log"
74 "$filesync_cmd" -e -d -f "$backup_source" "$backup_full" "$backup_day" > "$logfile"
78 mkdir -p "${backup_dir}" "${log_dir}" || (echo failed to create directory structure; exit 1)
80 if [ -e "$backup_cfg" ] ; then
81 cont=`cat "$backup_cfg"`
82 if [ "$cont" = "$month" ] ; then
83 do_backup_daily
84 else
85 do_backup_full
87 else
88 do_backup_full