-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfsSnap.sh
42 lines (38 loc) · 958 Bytes
/
zfsSnap.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
#!/bin/bash
# Check if atleast 2 arguments have been passed
zfsPath='/usr/local/sbin/zfs'
if [ "$#" -lt 2 ]
then
echo "$0 [create|destroy] [snapshotName] [olderThan]"
exit
fi
dateNow=`date +%s`
# Sort the command
if [[ "$1" == "create" ]]
then
# CREATE
$zfsPath snapshot $2_$dateNow
elif [[ "$1" == "destroy" ]]
then
# DESTROY
if [[ "$3" == "" ]]
then
echo "$0 [create|destroy] [snapshotName] [olderThan]"
exit
fi
snapshotName=$2
olderThan=$3
while read -r snapshot
do
snapshotDate=`echo $snapshot | awk -F '@' '{ print $2 }' | awk -F '_' '{ print $NF }'`
let snapshotAge=dateNow-snapshotDate
echo "> $snapshot|$snapshotAge"
if (( $snapshotAge > $olderThan ))
then
echo "[+] Destroying $snapshot"
$zfsPath destroy $snapshot
fi
done < <($zfsPath list -t snapshot -H | awk '{ print $1 }' | grep $snapshotName)
else
echo "$0 [create|destroy] [snapshotName] [olderThan]"
fi