Browse Source

Merge pull request #60 from bcachet/tarsnap

add(tarsnap): Script to rotate backups
Alex Payne 11 years ago
parent
commit
8c0b6c4e3f
3 changed files with 104 additions and 2 deletions
  1. 91
    0
      roles/tarsnap/files/tarsnap.sh
  2. 5
    0
      roles/tarsnap/files/tarsnaprc
  3. 8
    2
      roles/tarsnap/tasks/tarsnap.yml

+ 91
- 0
roles/tarsnap/files/tarsnap.sh View File

@@ -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

+ 5
- 0
roles/tarsnap/files/tarsnaprc View File

@@ -0,0 +1,5 @@
1
+keyfile /root/tarsnap.key
2
+cachedir /usr/tarsnap-cache
3
+exclude /usr/tarsnap-cache
4
+humanize-numbers
5
+

+ 8
- 2
roles/tarsnap/tasks/tarsnap.yml View File

@@ -24,5 +24,11 @@
24 24
 - name: Create Tarsnap cache directory
25 25
   file: state=directory path=/usr/tarsnap-cache
26 26
 
27
-- name: Install nightly Tarsnap cronjob
28
-  cron: name="Tarsnap backup" hour="3" minute="0" job="/usr/local/bin/tarsnap --cachedir /usr/tarsnap-cache --keyfile /root/tarsnap.key -c -f backup-`date +\%Y\%m\%d` -C / home root decrypted var/www var/log var/lib/mysql > /dev/null"
27
+- name: Install Tarsnap configuration file
28
+  copy: src=tarsnaprc dest=/root/.tarsnaprc mode="644"
29
+
30
+- name: Install Tarsnap backup handler script
31
+  copy: src=tarsnap.sh dest=/root/tarsnap.sh mode="755"
32
+
33
+- name: Install nightly Tarsnap-generations cronjob
34
+  cron: name="Tarsnap backup" hour="23" minute="0" job="sh /root/tarsnap.sh >> /var/log/tarsnap.log"

Loading…
Cancel
Save