|
@@ -0,0 +1,91 @@
|
|
1
|
+#!/bin/sh
|
|
2
|
+
|
|
3
|
+# Tarsnap backup script
|
|
4
|
+# Written by Tim Bishop, 2009.
|
|
5
|
+
|
|
6
|
+# Directories to backup
|
|
7
|
+DIRS="/home /root /decrypted /var/www /var/lib/mysql"
|
|
8
|
+
|
|
9
|
+# Number of daily backups to keep
|
|
10
|
+DAILY=7
|
|
11
|
+
|
|
12
|
+# Number of weekly backups to keep
|
|
13
|
+WEEKLY=3
|
|
14
|
+# Which day to do weekly backups on
|
|
15
|
+# 1-7, Monday = 1
|
|
16
|
+WEEKLY_DAY=5
|
|
17
|
+
|
|
18
|
+# Number of monthly backups to keep
|
|
19
|
+MONTHLY=1
|
|
20
|
+# Which day to do monthly backups on
|
|
21
|
+# 01-31 (leading 0 is important)
|
|
22
|
+MONTHLY_DAY=01
|
|
23
|
+
|
|
24
|
+# Path to tarsnap
|
|
25
|
+#TARSNAP="/home/tdb/tarsnap/tarsnap.pl"
|
|
26
|
+TARSNAP="/usr/local/bin/tarsnap"
|
|
27
|
+
|
|
28
|
+# end of config
|
|
29
|
+
|
|
30
|
+# day of week: 1-7, monday = 1
|
|
31
|
+DOW=`date +%u`
|
|
32
|
+# day of month: 01-31
|
|
33
|
+DOM=`date +%d`
|
|
34
|
+# month of year: 01-12
|
|
35
|
+MOY=`date +%m`
|
|
36
|
+# year
|
|
37
|
+YEAR=`date +%Y`
|
|
38
|
+# time
|
|
39
|
+TIME=`date +%H%M%S`
|
|
40
|
+
|
|
41
|
+# Backup name
|
|
42
|
+if [ X"$DOM" = X"$MONTHLY_DAY" ]; then
|
|
43
|
+ # monthly backup
|
|
44
|
+ BACKUP="$YEAR$MOY$DOM-$TIME-monthly"
|
|
45
|
+elif [ X"$DOW" = X"$WEEKLY_DAY" ]; then
|
|
46
|
+ # weekly backup
|
|
47
|
+ BACKUP="$YEAR$MOY$DOM-$TIME-weekly"
|
|
48
|
+else
|
|
49
|
+ # daily backup
|
|
50
|
+ BACKUP="$YEAR$MOY$DOM-$TIME-daily"
|
|
51
|
+fi
|
|
52
|
+
|
|
53
|
+# Do backups
|
|
54
|
+for dir in $DIRS; do
|
|
55
|
+ # nasty bodge for my large /home :-)
|
|
56
|
+ EXTRA_FLAGS=
|
|
57
|
+ if [ X"$dir" = X"/home" ]; then
|
|
58
|
+ EXTRA_FLAGS="--lowmem"
|
|
59
|
+ fi
|
|
60
|
+
|
|
61
|
+ echo "==> create $BACKUP-$dir"
|
|
62
|
+ $TARSNAP $EXTRA_FLAGS -c -f $BACKUP-$dir $dir
|
|
63
|
+done
|
|
64
|
+
|
|
65
|
+# Backups done, time for cleaning up old archives
|
|
66
|
+
|
|
67
|
+# using tail to find archives to delete, but its
|
|
68
|
+# +n syntax is out by one from what we want to do
|
|
69
|
+# (also +0 == +1, so we're safe :-)
|
|
70
|
+DAILY=`expr $DAILY + 1`
|
|
71
|
+WEEKLY=`expr $WEEKLY + 1`
|
|
72
|
+MONTHLY=`expr $MONTHLY + 1`
|
|
73
|
+
|
|
74
|
+# Do deletes
|
|
75
|
+TMPFILE=/tmp/tarsnap.archives.$$
|
|
76
|
+$TARSNAP --list-archives > $TMPFILE
|
|
77
|
+for dir in $DIRS; do
|
|
78
|
+ for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-daily-$dir" $TMPFILE | sort -rn | tail -n +$DAILY`; do
|
|
79
|
+ echo "==> delete $i"
|
|
80
|
+ $TARSNAP -d -f $i
|
|
81
|
+ done
|
|
82
|
+ for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-weekly-$dir" $TMPFILE | sort -rn | tail -n +$WEEKLY`; do
|
|
83
|
+ echo "==> delete $i"
|
|
84
|
+ $TARSNAP -d -f $i
|
|
85
|
+ done
|
|
86
|
+ for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-monthly-$dir" $TMPFILE | sort -rn | tail -n +$MONTHLY`; do
|
|
87
|
+ echo "==> delete $i"
|
|
88
|
+ $TARSNAP -d -f $i
|
|
89
|
+ done
|
|
90
|
+done
|
|
91
|
+rm $TMPFILE
|