-
Notifications
You must be signed in to change notification settings - Fork 1
/
baculabefore.sh
187 lines (140 loc) · 7.42 KB
/
baculabefore.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
#!/bin/bash
####
#### Assume volume label format is "client-LEVEL" i.e. INC FULL DIFF
#### Assume Only file backups i.e. no tape
#### To test, comment out rm and bconsole commands and uncomment ls line in each if
####
if [ "$#" -ne 5 ]
then
echo "Wrong number of args passed. Expected 5:"
echo "Usage: ./baculaVolumeCleaner.sh <client> <backuplevel> <IncrementalRetentionInDays> <DiffRetentionInDays> <FullRetentionInDays>"
exit
fi
# Echo the date and time into a text file to read later to work out how long the backup took
echo `date +%s` > /tmp/$1.timemetric
# set to yes to test only and not remove any data etc, testing mode just lists volumes which would have been deleted
# set no for production
## DEFAULT TO NOT DELETE STUFF ##
testingmode="yes"
# Create a temp file to build the bconsole commands into
TMPF1=`mktemp`
#start count for number of volumes
volcount=0
#pool retention in DAYS passed in via args
# should add case statement for name based args. ###TO DO###
incretention="$3"
diffretention="$4"
fullretention="$5"
# take client from arg 1
client="$1"
# convert client name to lowercase
clientlower=${client,,}
echo "[INFO] client passed: $clientlower"
# Set data path here:
volumepath="/data/$clientlower"
echo "[INFO] Path calculated as: $volumepath"
# level i.e. Incremental or Differential or Full from arg 2
level="$2"
echo "[INFO] Job level passed "$level""
#convert level to lowercase
levellower=${level,,}
if [ "$levellower" == "incremental" ]
then
levelinc="INC"
elif [ "$levellower" == "differential" ]
then
leveldiff="DIFF"
elif [ "$levellower" == "full" ]
then
levelfull="FULL"
else
echo "[FATAL] Unknown bacula backup level passed, exiting. Level passed: "$level""
exit 1
fi
if [ "$levelinc" == "INC" ]
then
echo "[INFO] Bacula is doing an incremental backup"
# get a list of volumes which have not been written to in (incremental pool retention period) days
oldVolumesinc=$(mysql --no-defaults -N -u root -e "select VolumeName from bacula.Media where lower(convert(VolumeName using LATIN1)) like '%"$clientlower"-"$levelinc"%' and LastWritten < NOW() - INTERVAL "$incretention" DAY order by LastWritten;")
# echo "$oldVolumesinc"
for a in $oldVolumesinc ; do
echo "[INFO] Will delete $a"
cp /dev/null $TMPF1
printf "prune yes volume=$a\n" >> $TMPF1
printf "delete yes volume=$a\n" >> $TMPF1
if [ "$testingmode" == "yes" ]
then
echo "[INFO] Testing mode active..."
ls -lhrt $volumepath/$a
else
echo "[INFO] Deleting... $a"
# redirect rest to bconsolereturn.log for debugging...
bconsole <$TMPF1 1>/tmp/bconsolereturn.log
if [ "$?" -gt 0 ]; then
echo "+[$a]: [FATAL]: Bconsole returned non-zero status returned: $?"
echo "See: TMPF1=$TMPF1"
exit
fi
rm -fv $volumepath/$a
fi
volcount=$((volcount+1))
done
elif [ "$leveldiff" == "DIFF" ]
then
echo "[INFO] Bacula is doing an diff backup"
# get a list of volumes which have not been written to in (diff pool retention period) days
oldVolumesinc=$(mysql --no-defaults -u root -N -e "select VolumeName from bacula.Media where lower(convert(VolumeName using LATIN1)) like '%"$clientlower"-"$leveldiff"%' and LastWritten < NOW() - INTERVAL "$diffretention" DAY order by LastWritten;")
for a in $oldVolumesinc ; do
echo "[INFO] Will delete $a"
cp /dev/null $TMPF1
printf "prune yes volume=$a\n" >> $TMPF1
printf "delete yes volume=$a\n" >> $TMPF1
if [ "$testingmode" == "yes" ]
then
echo "[INFO] Testing mode active..."
ls -lhrt $volumepath/$a
else
echo "[INFO] Deleting... $a"
bconsole <$TMPF1 1>/tmp/bconsolereturn.log
if [ "$?" -gt 0 ]; then
echo "+[$a]: [FATAL]: Bconsole returned non-zero status returned: $?"
echo "See: TMPF1=$TMPF1"
exit
fi
rm -fv $volumepath/$a
fi
volcount=$((volcount+1))
done
elif [ "$levelfull" == "FULL" ]
then
echo "[INFO] Bacula is doing an full backup"
# get a list of volumes which have not been written to in (diff pool retention period) days
oldVolumesinc=$(mysql --no-defaults -N -u root -e "select VolumeName from bacula.Media where lower(convert(VolumeName using LATIN1)) like '%"$clientlower"-"$levelfull"%' and LastWritten < NOW() - INTERVAL "$fullretention" DAY order by LastWritten;")
for a in $oldVolumesinc ; do
echo "[INFO] Will delete $a"
cp /dev/null $TMPF1
printf "prune yes volume=$a\n" >> $TMPF1
printf "delete yes volume=$a\n" >> $TMPF1
if [ "$testingmode" == "yes" ]
then
echo "[INFO] Testing mode active..."
ls -lhrt $volumepath/$a
else
echo "[INFO] Deleting... $a"
bconsole <$TMPF1 1>/tmp/bconsolereturn.log
if [ "$?" -gt 0 ]; then
echo "+[$a]: [FATAL]: Bconsole returned non-zero status returned: $?"
echo "See: TMPF1=$TMPF1"
exit
fi
rm -fv $volumepath/$a
fi
volcount=$((volcount+1))
done
else
echo "[FATAL] expected INC or DIFF or FULL"
exit 1
fi
echo "[INFO] deleting temp file $TMPF1"
rm -f $TMPF1
echo "[INFO] "$volcount" volumes deleted"