-
Notifications
You must be signed in to change notification settings - Fork 0
/
openmediavault_zfs.sh
84 lines (67 loc) · 1.77 KB
/
openmediavault_zfs.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
#!/usr/bin/bash
# Reference
# cron: https://www.runoob.com/linux/linux-comm-crontab.html
# log
## 2022-01-13:
## 1. generalize pool names to any string instead of `nas` only.
# Introduction
## Create or destory snapshots of ZFS pools in Openmediavault
# Usage
USAGE_NOTRUN()
{
chmod +777 ./openmediavault_zfs.sh && cp ./openmediavault_zfs.sh /sbin
# 1.Basic usage
bash openmediavault_zfs.sh snapshot
bash openmediavault_zfs.sh destroy
bash openmediavault_zfs.sh test
# 2.Cron
echo "0 3 * * * root openmediavault_zfs.sh snapshot" >> /etc/crontab
echo "0 5 * * * root openmediavault_zfs.sh destroy" >> /etc/crontab
# 3.Check crontab
vim /etc/crontab
}
# Parameter
type=$1 # 执行类型: snapshot ; destroy; test
every=$2 # 每多少天进行快照删除。
# Programe
## check the name of pools
pool=`/sbin/zpool list | cut -d' ' -f1`
## How many pools
i=1
while((1==1))
do
splitchar=`echo $pool|cut -d " " -f$i`
if [ "$splitchar" != "" ]
then
((i++))
else
break
fi
done
num=$[ `expr $i - 1` ]
## Manipulate snapshots
if [ $num -ge 2 ]; then
for ((i=2;i<=$num;i++));
do
# Get one pool
p=`echo ${pool}|cut -d' ' -f${i}`
echo "Pool: $p"
# Snapshot
if [ ${type} == "snapshot" ]; then
/sbin/zfs snapshot -r ${p}@AutoD-`date +"%F"`
elif [ ${type} == "destroy" ]; then
dataset=`/sbin/zfs list -t snapshot -o name | /bin/grep ${p}@AutoD- | /usr/bin/sort -r | /usr/bin/tail -n +${every}`
if [ -z ${dataset} ]; then
echo "no datasets available"
else
echo "destroy ${dataset}"
/sbin/zfs destroy -r ${dataset}
fi
elif [ ${type} == "test" ]; then
ls /sbin/openmediavault_zfs.sh -hl | cut -d' ' -f1 && cat /etc/crontab | grep openmediavault_zfs.sh
else
echo "Please input right type: One of 'snapshot', 'destory' or 'test'."
fi
# End
done
fi