Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ############################################################ CONFIGURATION
- #
- # Create snapshot if delta becomes at least this many bytes
- #
- MIN_SIZE=$(( 1024 * 1024 * 10 )) # 10MB
- #
- # Create snapshot if previous is at least this old
- #
- MIN_AGE=$(( 3600 * 24 )) # 24 hours
- #
- # Snapshot name
- #
- SNAPNAME_FORMAT="zsnap-%Y-%m-%d_%H.%M.%S"
- ############################################################ MAIN
- set -e # make any/all errors fatal
- #
- # Get current epoch and pre-create snapname
- #
- now=$( date +%s )
- newsnapname=$( date -r $now +"$SNAPNAME_FORMAT" )
- #
- # Operate on the datasets
- #
- for dataset in $( zfs list -Ht filesystem | awk '$0=$1' ); do
- #
- # Get the date/time and name of the most recent snapshot for dataset
- #
- read -r creation snapname <<-EOF
- $( zfs get -pHrt snapshot creation "$dataset" 2> /dev/null |
- awk -v dataset="$dataset" '
- BEGIN { last_epoch = 0 }
- (ds = $1) sub(/@.*/, "", ds) && ds == dataset {
- if ((epoch = $--NF) < last_epoch) next
- last_epoch = epoch
- last_snapshot = $1
- sub(/^[^@]*@/, "", last_snapshot)
- }
- END { print last_epoch, last_snapshot }
- ' | sort -nr )
- EOF
- #
- # Skip filesystem if still young and few bytes written
- #
- age=$(( $now - $creation ))
- written=$( zfs get -Hp written "$dataset" | awk '{print $--NF}' )
- [ $age -lt $MIN_AGE -a $written -lt $MIN_SIZE ] && continue
- #
- # Create new snapshot
- #
- echo "zfs snapshot $dataset@$newsnapname"
- done
- ################################################################################
- # END
- ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement