f99aq8ove (owner)

Revisions

gist: 58114 Download_button fork
public
Description:
zfs snapshot auto-rotation script
Public Clone URL: git://gist.github.com/58114.git
Embed All Files: show embed
zfs_snapshot.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
#!/bin/sh
#
# zfs snapshot auto-rotation
#
# usage: zfs_snapshot.sh <mountpoint> <snapshot name> <number of keeping snapshots> [recursive]
#
# recursive option: Recursively create snapshot (zfs snapshot -r)
#
# for crontab
#
# @daily /root/bin/zfs_snapshot.sh tank days_later 7 recursive
# @weekly /root/bin/zfs_snapshot.sh tank weeks_later 5 recursive
# @monthly /root/bin/zfs_snapshot.sh tank months_later 12 recursive
# @yearly /root/bin/zfs_snapshot.sh tank years_later 10 recursive
 
 
PATH=/sbin:$PATH
 
mountpoint=$1
snapshot_name=$2
level=$3
option=$4
 
zfs_opt=
if [ "$option" = "recursive" ]; then
zfs_opt=-r
fi
 
## destroy outdated snapshot
zfs destroy $zfs_opt "$mountpoint@`expr $level - 1`$snapshot_name"
 
## rolling snapshot
j=`expr $level - 2`
for i in `jot - $j 0`; do
i_plus1=`expr $i + 1`
    zfs rename $zfs_opt "$mountpoint@$i$snapshot_name" "$mountpoint@$i_plus1$snapshot_name"
done
 
## take snapshot
zfs snapshot $zfs_opt "$mountpoint@0$snapshot_name"