]> piware.de Git - bin.git/blob - backup
rewrite backup script in shell and with fixed weekly/monthly logic
[bin.git] / backup
1 #!/bin/sh -e
2
3 # call rsnapshot daily/weekly/monthly regularly
4 # this should be called from cron very often (several times a day) to not miss
5 # a cycle when the machine is powered down for extended times.
6 #
7 # Author: Martin Pitt <martin@piware.de>
8 # License: Public Domain
9
10 backupdir=`grep ^snapshot_root ~/.rsnapshotrc | awk '{print $2}'`
11
12 now=`date +%s`
13
14 get_age() {
15     if [ -e $backupdir/$1 ]; then
16         age=$(($now - `stat -c %Y $backupdir/$1`))
17     else
18         unset age
19     fi
20 }
21
22 # abort if last backup was less than a day ago
23 get_age daily.0
24 [ -n "$age" ] && [ $age -lt 86300 ] && exit 0
25
26 print '------------1---------------'
27 # monthly backup if last monthly is older than a month (if it doesn't exist,
28 # check oldest weekly)
29 get_age monthly.0
30 [ -n "$age" ] || get_age weekly.3
31 [ -n "$age" ] && [ $age -ge 2591900 ] && mode=monthly
32
33 # weekly backup if last weekly is older than a week (if it doesn't exist,
34 # check oldest daily)
35 get_age weekly.0
36 [ -n "$age" ] || get_age daily.6
37 [ -n "$age" ] && [ $age -ge 604700 ] && mode=weekly
38
39 # default to daily
40 [ -n "$mode" ] || mode=daily
41
42 # call rsnapshot
43 echo mode: $mode
44 rsnapshot -c ~/.rsnapshotrc $mode