Rotera ZFS snapshots i FreeBSD: Difference between revisions
Jump to navigation
Jump to search
m Skapade sidan med ' Category:FreeBSD' |
mNo edit summary |
||
Line 1: | Line 1: | ||
{{Box Fil|/root/bin/zfs-autosnap.sh| | |||
<pre> | |||
#!/bin/sh | |||
usage() { | |||
cat <<EOF | |||
usage: ${0##*/} <label> | |||
Rotates filesystem snapshots. If filesystem has the property | |||
org.freebsd:snap:<label>=<count>, increments all existing snapshots labeled | |||
<label>.<num> and creates a new snapshot called <label>.0. Destroys snapshot | |||
numbered <label>.<count>. | |||
EOF | |||
exit | |||
} | |||
# Default values | |||
: ${PROP:=org.freebsd:snap} | |||
# Parse command line | |||
[ ${#} -ne 1 ] && usage | |||
label=${1} | |||
issnap() { | |||
[ "$(/sbin/zfs get -Ho value type "$1" 2>/dev/null)" = "snapshot" ] | |||
} | |||
# Iterate over all filesystems with ${PROP}:${label} > 0 | |||
/sbin/zfs list -Ho name,${PROP}:${label} -t filesystem | \ | |||
while read name value; do | |||
[ $value -gt 0 ] 2>/dev/null || continue | |||
# Destroy the oldest snapshot | |||
value=$((${value} - 1)) | |||
snap="${name}@${label}" | |||
issnap ${snap}.${value} && /sbin/zfs destroy ${snap}.${value} | |||
# Increment existing snapshots | |||
while [ ${value} -gt 0 ]; do | |||
next=$((${value} - 1)) | |||
issnap ${snap}.${next} && /sbin/zfs rename "${snap}.${next}" "${snap}.${value}" | |||
value=$next | |||
done | |||
# Make new snapshot | |||
/sbin/zfs snapshot ${snap}.0 | |||
done | |||
</pre> | |||
}} | |||
[[Category:FreeBSD]] | [[Category:FreeBSD]] |