-
Notifications
You must be signed in to change notification settings - Fork 13
/
tarsnap-prune.sh
executable file
·88 lines (71 loc) · 2.06 KB
/
tarsnap-prune.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bash
# Tarsnap backup script
# Written by Tim Bishop, 2009.
# Modified by Pronoiac, 2014.
# Directories to backup - set in
CONFIG=/etc/tarsnap-cron.conf
#CONFIG=tarsnap-cron.conf
# note: this evals the config file, which can present a security issue
# unless it's locked with the right permissions - e.g. not world-writable
if [ ! -r "$CONFIG" ] ; then
echo "ERROR: Couldn't read config file $CONFIG"
echo Exiting now!
exit 1
fi
source $CONFIG
# end of config
# using tail to find archives to delete, but its
# +n syntax is out by one from what we want to do
# (also +0 == +1, so we're safe :-)
DAILY=`expr $DAILY + 1`
WEEKLY=`expr $WEEKLY + 1`
MONTHLY=`expr $MONTHLY + 1`
HOURLY=`expr $HOURLY + 1`
# Do deletes
if [ "$DEBUG" = "true" ]
then
TMPFILE=/tmp/tarsnap.archives
if [ ! -f $TMPFILE ]; then
$TARSNAP --configfile /etc/tarsnap.conf --list-archives > $TMPFILE
fi
else
TMPFILE=/tmp/tarsnap.archives.$$
$TARSNAP --configfile /etc/tarsnap.conf --list-archives > $TMPFILE
fi
DELARCHIVES=""
for BACKUP in "${BACKUP_ARRAY[@]}"; do
SPLIT=(${BACKUP//=/ })
# typical label & path: www and /var/www
label=${SPLIT[0]}
path=${SPLIT[1]}
# hourly
for i in `grep -E "^$PREFIX-$label-.+-hourly$" $TMPFILE | sort -rn | tail -n +$HOURLY`; do
echo "==> delete $i"
DELARCHIVES="$DELARCHIVES -f $i"
done
# daily
for i in `grep -E "^$PREFIX-$label-.+-daily$" $TMPFILE | sort -rn | tail -n +$DAILY`; do
echo "==> delete $i"
DELARCHIVES="$DELARCHIVES -f $i"
done
# weekly
for i in `grep -E "^$PREFIX-$label-.+-weekly$" $TMPFILE | sort -rn | tail -n +$WEEKLY`; do
echo "==> delete $i"
DELARCHIVES="$DELARCHIVES -f $i"
done
# monthly
for i in `grep -E "^$PREFIX-$label-.+-monthly$" $TMPFILE | sort -rn | tail -n +$MONTHLY`; do
echo "==> delete $i"
DELARCHIVES="$DELARCHIVES -f $i"
done
done
if [ X"$DELARCHIVES" != X ]; then
echo "==> delete $DELARCHIVES"
if [ "$DEBUG" = "true" ]
then
echo $TARSNAP -d $DELARCHIVES
else
$TARSNAP -d $DELARCHIVES
rm $TMPFILE
fi
fi