-
-
Save indication/3801166 to your computer and use it in GitHub Desktop.
zfs snapshot auto-rotation script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
i_max=`expr $level - 1` | |
i_test=`zfs list -t snapshot | grep "$mountpoint@$i_max$snapshot_name" | wc -l` | |
if [ $i_test -gt 0 ]; then | |
zfs destroy $zfs_opt "$mountpoint@$i_max$snapshot_name" | |
fi | |
## rolling snapshot | |
j=`expr $level - 2` | |
#for i in `jot - $j 0`; do | |
for i in `seq $j -1 0`; do | |
i_plus1=`expr $i + 1` | |
i_test=`zfs list -t snapshot | grep "$mountpoint@$i$snapshot_name" | wc -l` | |
if [ $i_test -gt 0 ] ; then | |
zfs rename $zfs_opt "$mountpoint@$i$snapshot_name" "$mountpoint@$i_plus1$snapshot_name" | |
fi | |
done | |
## take snapshot | |
zfs snapshot $zfs_opt "$mountpoint@0$snapshot_name" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This may take costs when it has too many snapshots.
If you regard, you can use original.