-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_zfs_cleanup.sh
executable file
·119 lines (103 loc) · 2.64 KB
/
backup_zfs_cleanup.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
#!/bin/bash
#
# global vars
#
if [ -f `dirname $0`/backup.vars ]
then
. `dirname $0`/backup.vars
else
echo "backup.vars missing"
exit 1
fi
#
# args
#
if [ $# -ne 2 ]
then
echo "We need 2 arguments <data_pool> <backup_pool>"
fatal "We need 2 arguments <data_pool> <backup_pool>"
else
DATA_POOL=$1
BACKUP_POOL=$2
fi
#
# local vars
#
# Overriding global EXECLOGFILE
EXECLOGFILE=${LOGDIR}/cleanup.`date +%Y%m%d.%H%M`.log
TEMPFILE=${LOGDIR}/temp_`basename $0`_$$
#
# reusable functions
#
clean_logs()
{
log "Cleaning log files older than ${LOG_RETENTION} days"
find $LOGDIR -mtime +${LOG_RETENTION} -name "*.log*" -exec rm {} \;
}
##########
# MAIN
##########
# First cleanup the log files
clean_logs
# Second cleanup the snapshots in pools
log "Pools : BACKUP - $BACKUP_POOL / DATA - $DATA_POOL"
for POOL in $BACKUP_POOL $DATA_POOL
do
log " "
log "************************ POOL ${POOL} *************************"
log "Starting cleanup of snapshots in ${POOL} on `date '+%Y%m%d-%H%M'`"
# look for last SNAPS_RETENTION snapshot name(s)/date(s)
PREVIOUS=`${ZFS} list -H -r -o name -t snapshot ${POOL} | sort -r | head -$SNAPS_RETENTION | cut -f2 -d\@ | sort -u`
if [ -z "${PREVIOUS}" ]
then
log "We don't have a backup => don't cleanup, skipping"
continue
else
log "last backup(s) : ${PREVIOUS}"
fi
log "${POOL} - Retrieving list of ALL snapshots (of the top zfs) and keeping the PREVIOUS backups"
TOP_SNAPS=`${ZFS} list -H -r -o name -t snapshot ${POOL} | grep -v \/ | sort -r `
###TOP_SNAPS=`echo $SNAPS | sed -e "s,/[a-z]*\@,\@,g"`
if [ -z "$TOP_SNAPS" ]
then
log "No top zfs snapshots, retrieveing all other snapshots"
SNAPS=`${ZFS} list -H -r -o name -t snapshot ${POOL} | sort -r `
else
SNAPS=$TOP_SNAPS
fi
# Filtering out previous backups (as selected above)
# by removing them from SNAPS list
log "Exclude backups to be kept"
for previous in $PREVIOUS
do
if [ ! -z "$TOP_SNAPS" ]
then
SNAPS=`echo $SNAPS | sed -e "s,${POOL}\@${previous},,g"`
else
FILTER_SNAPS=
for snap in $SNAPS
do
FILTER_SNAPS="$FILTER_SNAPS `echo $snap | grep -v $previous `"
done
SNAPS=$FILTER_SNAPS
fi
done
log "Deleting extra snapshots"
# to avoid useless error message
touch $TEMPFILE
# Deleting snaps listed in $SNAPS
for snap in $SNAPS
do
NB=`grep $snap $TEMPFILE | wc -l`
if [ $NB -eq 0 ]
then
log "Deleting $snap"
${ZFS} destroy -r $snap || fatal "Cannot destroy snapshot $snap"
echo $snap >> $TEMPFILE
fi
done
# displaying the deleted snapshot
cat $TEMPFILE
rm $TEMPFILE
log "End of cleanup"
done