Skip to content

Instantly share code, notes, and snippets.

@fuyuanli
Last active October 5, 2019 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuyuanli/d6b5d626c0bff4b32a2836450aaff58d to your computer and use it in GitHub Desktop.
Save fuyuanli/d6b5d626c0bff4b32a2836450aaff58d to your computer and use it in GitHub Desktop.
ZFS 自動快照
#!/bin/bash
# 目標 Dataset
DATASET="data/home"
# 要保留的 Snapshot 數量
SNAP_NUM=168
# 日期
TODAY=$(date +"%Y-%m-%d-%H%M%S")
# 進行 snapshot
/usr/sbin/zfs snapshot $DATASET@zfs-auto-snap_$TODAY
# 刪除快照超過 $SNAP_NUM 數量的舊快照,並把結果丟進 $DESTROY
DESTROY_LOG="$(/usr/sbin/zfs list -t snapshot -o name -S creation | /usr/bin/grep @zfs-auto-snap | /usr/bin/tail -n +$(($SNAP_NUM+1)) | /usr/bin/xargs -n 1 /usr/sbin/zfs destroy -vr )"
echo $DESTROY_LOG
if [ -z "$DESTROY_LOG" ]; then
DESTROY_LOG="The Snapshots of $DATASET are less then $SNAP_NUM, so it will not run the destroy command."
/usr/bin/logger -i -t "zfs-autosnap.sh" -p user.notice $DESTROY_LOG
else
/usr/bin/logger -i -t "zfs-autosnap.sh" -p user.notice $DESTROY_LOG
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment