-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshotCycle.sh
executable file
·80 lines (62 loc) · 1.54 KB
/
snapshotCycle.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
#!/bin/bash
PATH=$PATH:/usr/local/bin/:/usr/sbin/
usage() {
echo "USAGE: ./snapshotCycle.sh <DAYS> <POOLNAME> <FILESYSTEM> <SNAPSHOT BASE NAME>"
echo "DAYS must be a positive integer"
echo "eg. ./snapshotCycle.sh 14 ZFS_POOL Users snap"
}
checkStatus() {
if [ $? -gt 0 ];
then
echo "An error occured in this step: $1"
exit 125
fi
}
if [ $# -ne 4 ]
then
usage
exit
fi
# Set variables to something so we don't accidentally erase a pool
export POOLNAME=FOO
export SNAPBASE=FOO
export FILESYSTEM=FOO
export DAYS=$1
export POOLNAME=$2
export FILESYSTEM=$3
export SNAPBASE=$4
export FSBASE=${POOLNAME}/${FILESYSTEM}@${SNAPBASE}_
echo ""
echo "Snapshot cycle script starting"
echo `date`
echo ""
echo " - Snapshot basename is " $FSBASE
if ! [[ "$DAYS" =~ ^[0-9]+$ ]] ; then
usage
fi
echo " - Cycling ZFS snapshots"
echo " - Keeping $DAYS days of snapshots"
# Remove oldest
echo " - Removing oldest snapshot"
zfs destroy ${FSBASE}${DAYS}
checkStatus DestroyOldest
# echo " - Faking it: zfs destroy ${FSBASE}${DAYS}"
# Iterate and increment the rest
echo " - Incrementing previous days"
let COUNTDAY=${DAYS}-1
while [ "$COUNTDAY" -ge 0 ]; do
let NEWDAY=COUNTDAY+1
echo " - Renaming $COUNTDAY"
zfs rename ${FSBASE}${COUNTDAY} ${FSBASE}${NEWDAY}
zfs get name,creation,used,referenced,compressratio ${FSBASE}${NEWDAY}
checkStatus IncrementingExisting
let COUNTDAY=COUNTDAY-1
done
# Create new one
echo " - Creating today's snapshot"
zfs snapshot ${FSBASE}0
checkStatus TodaysSnapshot
echo ""
echo "Script complete"
echo `date`
exit 0