-
Notifications
You must be signed in to change notification settings - Fork 20
/
tarsnap-generations.sh
executable file
·237 lines (212 loc) · 7.61 KB
/
tarsnap-generations.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env bash
#See README @ http://github.com/Gestas/Tarsnap-generations/blob/master/README
#########################################################################################
#What day of the week do you want to take the weekly snapshot? Default = Friday(5) #
WEEKLY_DOW=5 #
#What hour of the day to you want to take the daily snapshot? Default = 11PM (23) #
DAILY_TIME=23 #
#Do you want to use UTC time? (1 = Yes) Default = 0, use local time. #
USE_UTC=0 #
#Path to GNU date binary (e.g. /bin/date on Linux, /usr/local/bin/gdate on FreeBSD) #
DATE_BIN=`which date` #
#########################################################################################
usage ()
{
cat << EOF
usage: $0 arguments
This script manages Tarsnap backups
ARGUMENTS:
? Display this help.
-f Path to a file with a list of folders to be backed up. List should be \n delimited.
-h Number of hourly backups to retain.
-d Number of daily backups to retain.
-w Number of weekly backups to retain.
-m Number of monthly backups to retain.
-q Be quiet - only output if something goes wrong
For more information - http://github.com/Gestas/Tarsnap-generations/blob/master/README
EOF
}
#Declaring helps check for errors in the user-provided arguments. See line #69.
declare -i HOURLY_CNT
declare -i DAILY_CNT
declare -i WEEKLY_CNT
declare -i MONTHLY_CNT
declare -i QUIET
QUIET=0
#Get the command line arguments. Much nicer this way than $1, $2, etc.
while getopts ":f:h:d:w:m:q" opt ; do
case $opt in
f ) PATHS=$OPTARG ;;
h ) HOURLY_CNT=$(($OPTARG+1)) ;;
d ) DAILY_CNT=$(($OPTARG+1)) ;;
w ) WEEKLY_CNT=$(($OPTARG+1)) ;;
m ) MONTHLY_CNT=$(($OPTARG+1)) ;;
q ) QUIET=1 ;;
\?) echo \n $usage
exit 1 ;;
*) echo \n $usage
exit 1 ;;
esac
done
#Check arguments
if ( [ -z "$PATHS" ] || [ -z "$HOURLY_CNT" ] || [ -z "$DAILY_CNT" ] || [ -z "$WEEKLY_CNT" ] || [ -z "$MONTHLY_CNT" ] )
then
echo "-f, -h, -d, -w, -m are not optional."
usage
exit 1
fi
if [ ! -f $PATHS ]
then
echo "Couldn't find file $PATHS"
usage
exit 1
fi
#Check that $HOURLY_CNT, $DAILY_CNT, $WEEKLY_CNT, $MONTLY_CNT are numbers.
if ( [ $HOURLY_CNT = 1 ] || [ $DAILY_CNT = 1 ] || [ $WEEKLY_CNT = 1 ] || [ $MONTHLY_CNT = 1 ] )
then
echo "-h, -d, -w, -m must all be numbers greater than 0."
usage
exit 1
fi
#Set some constants
#The day of the week (Monday = 1, Sunday = 7)
DOW=$($DATE_BIN +%u)
#The calendar day of the month
DOM=$($DATE_BIN +%d)
#The last day of the current month. I wish there was a better way to do this, but this seems to work everywhere.
LDOM=$(echo $(cal) | awk '{print $NF}')
#We need 'NOW' to be constant during execution, we set it here.
NOW=$($DATE_BIN +%Y%m%d-%H)
CUR_HOUR=$($DATE_BIN +%H)
if [ "$USE_UTC" = "1" ] ; then
NOW=$($DATE_BIN -u +%Y%m%d-%H)
CUR_HOUR=$($DATE_BIN -u +%H)
fi
#Find the backup type (HOURLY|DAILY|WEEKLY|MONTHY)
BK_TYPE=HOURLY #Default to HOURLY
if ( [ "$DOM" = "$LDOM" ] && [ "$CUR_HOUR" = "$DAILY_TIME" ] ) ; then
BK_TYPE=MONTHLY
else
if ( [ "$DOW" = "$WEEKLY_DOW" ] && [ "$CUR_HOUR" = "$DAILY_TIME" ] ) ; then
BK_TYPE=WEEKLY
else
if [ "$CUR_HOUR" = "$DAILY_TIME" ] ; then
BK_TYPE=DAILY
fi
fi
fi
#Take the backup with the right name
if [ $QUIET != "1" ] ; then
echo "Starting $BK_TYPE backups..."
fi
for dir in $(cat $PATHS) ; do
tarsnap -c -f $NOW-$BK_TYPE-$(hostname -s)-$(echo $dir) --one-file-system -C / $dir
if [ $? = 0 ] ; then
if [ $QUIET != "1" ] ; then
echo "$NOW-$BK_TYPE-$(hostname -s)-$(echo $dir) backup done."
fi
else
echo "$NOW-$BK_TYPE-$(hostname -s)-$(echo $dir) backup error. Exiting" ; exit $?
fi
done
#Check to make sure the last set of backups are OK.
if [ $QUIET != "1" ] ; then
echo "Verifying backups, please wait."
fi
archive_list=$(tarsnap --list-archives)
for dir in $(cat $PATHS) ; do
case "$archive_list" in
*"$NOW-$BK_TYPE-$(hostname -s)-$(echo $dir)"* )
if [ $QUIET != "1" ] ; then
echo "$NOW-$BK_TYPE-$(hostname -s)-$(echo $dir) backup OK."
fi ;;
* ) echo "$NOW-$BK_TYPE-$(hostname -s)-$(echo $dir) backup NOT OK. Check --archive-list."; exit 3 ;;
esac
done
#Delete old backups
HOURLY_DELETE_TIME=$($DATE_BIN -d"-$HOURLY_CNT hour" +%Y%m%d-%H)
DAILY_DELETE_TIME=$($DATE_BIN -d"-$DAILY_CNT day" +%Y%m%d-%H)
WEEKLY_DELETE_TIME=$($DATE_BIN -d"-$WEEKLY_CNT week" +%Y%m%d-%H)
MONTHLY_DELETE_TIME=$($DATE_BIN -d"-$MONTHLY_CNT month" +%Y%m%d-%H)
if [ $QUIET != "1" ] ; then
echo "Finding backups to be deleted."
fi
if [ $BK_TYPE = "HOURLY" ] ; then
for backup in $archive_list ; do
case "$backup" in
"$HOURLY_DELETE_TIME-$BK_TYPE"* )
case "$backup" in #this case added to make sure the script doesn't delete the backup it just took. Case: '-h x' and backup takes > x hours.
*"$NOW"* ) echo "Skipped $backup" ;;
* ) tarsnap -d -f $backup
if [ $? = 0 ] ; then
if [ $QUIET != "1" ] ; then
echo "$backup snapshot deleted."
fi
else
echo "Unable to delete $backup. Exiting" ; exit $?
fi ;;
esac ;;
* ) ;;
esac
done
fi
if [ $BK_TYPE = "DAILY" ] ; then
for backup in $archive_list ; do
case "$backup" in
"$DAILY_DELETE_TIME-$BK_TYPE"* )
case "$backup" in
*"$NOW"* ) echo "Skipped $backup" ;;
* ) tarsnap -d -f $backup
if [ $? = 0 ] ; then
if [ $QUIET != "1" ] ; then
echo "$backup snapshot deleted."
fi
else
echo "Unable to delete $backup. Exiting" ; exit $?
fi ;;
esac ;;
* ) ;;
esac
done
fi
if [ $BK_TYPE = "WEEKLY" ] ; then
for backup in $archive_list ; do
case "$backup" in
"$WEEKLY_DELETE_TIME-$BK_TYPE"* )
case "$backup" in
*"$NOW"* ) echo "Skipped $backup" ;;
* ) tarsnap -d -f $backup
if [ $? = 0 ] ; then
if [ $QUIET != "1" ] ; then
echo "$backup snapshot deleted."
fi
else
echo "Unable to delete $backup. Exiting" ; exit $?
fi ;;
esac ;;
* ) ;;
esac
done
fi
if [ $BK_TYPE = "MONTHLY" ] ; then
for backup in $archive_list ; do
case "$backup" in
"$MONTHLY_DELETE_TIME-$BK_TYPE"* )
case "$backup" in
*"$NOW"* ) echo "Skipped $backup" ;;
* ) tarsnap -d -f $backup
if [ $? = 0 ] ; then
if [ $QUIET != "1" ] ; then
echo "$backup snapshot deleted."
fi
else
echo "Unable to delete $backup. Exiting" ; exit $?
fi ;;
esac ;;
* ) ;;
esac
done
fi
if [ $QUIET != "1" ] ; then
echo "$0 done"
fi