1
1
forked from extern/zsync

add snapshot-manager

This commit is contained in:
Administrator 2024-12-14 13:35:07 +00:00
parent 336f80387e
commit 5a6e0b3394

View File

@ -0,0 +1,111 @@
#!/bin/bash
# ZFS Snapshot Management Script
# Function to display usage information
usage() {
echo "Usage: $0 -d <dataset> -k <keep> -l <label> [-f <dateformat>] [-v]"
echo " -d: Name of the ZFS dataset"
echo " -k: Number of snapshots to keep"
echo " -l: Label for the snapshot"
echo " -f: (Optional) Date format to append to the snapshot name (default: '%Y-%m-%d-%H%M')"
echo " -v: (Optional) Enable verbose/debug logging"
exit 1
}
# Logging function
function log() {
echo -e "$(date +'%b %d %T') $1"
}
# Debug logging function
function debug() {
if [[ "$verbose" == true ]]; then
echo -e "$(date +'%b %d %T') [DEBUG] $1"
fi
}
# Default date format
default_dateformat="%Y-%m-%d-%H%M"
verbose=false
# Parse command-line arguments
while getopts "d:k:l:f:v" opt; do
case "$opt" in
d) dataset="$OPTARG" ;;
k) keep="$OPTARG" ;;
l) label="$OPTARG" ;;
f) dateformat="$OPTARG" ;;
v) verbose=true ;;
*) usage ;;
esac
done
# Validate required parameters
if [[ -z "$dataset" || -z "$keep" || -z "$label" ]]; then
usage
fi
# Set dateformat to default if not provided
if [[ -z "$dateformat" ]]; then
dateformat="$default_dateformat"
fi
# Validate "keep" parameter is a number
if ! [[ "$keep" =~ ^[0-9]+$ ]]; then
log "Error: 'keep' must be a positive integer."
exit 1
fi
debug "Dataset: $dataset"
debug "Keep: $keep"
debug "Label: $label"
debug "Date format: $dateformat"
debug "Verbose mode: $verbose"
# Generate the snapshot name
snapshot_name="${dataset}@${label}-$(date -u +"$dateformat")"
debug "Generated snapshot name: $snapshot_name"
# Create the ZFS snapshot
log "Creating snapshot: $snapshot_name"
zfs snapshot "$snapshot_name"
if [[ $? -ne 0 ]]; then
log "Error: Failed to create snapshot."
exit 1
fi
# List existing snapshots for the dataset
debug "Listing existing snapshots for dataset: $dataset"
existing_snapshots=$(zfs list -t snapshot -o name -s creation "$dataset" | grep "^${dataset}@${label}-")
debug "Existing snapshots:\n$existing_snapshots"
# Count the total number of snapshots matching the label
snapshot_count=$(echo "$existing_snapshots" | wc -l)
debug "Snapshot count: $snapshot_count"
# Check if pruning is necessary
if [[ "$snapshot_count" -gt "$keep" ]]; then
# Calculate the number of snapshots to delete
delete_count=$((snapshot_count - keep))
debug "Number of snapshots to delete: $delete_count"
# Get the snapshots to delete (oldest first)
snapshots_to_delete=$(echo "$existing_snapshots" | head -n "$delete_count")
debug "Snapshots to delete:\n$snapshots_to_delete"
# Delete the old snapshots
log "Deleting $delete_count old snapshot(s):"
for snapshot in $snapshots_to_delete; do
log "Deleting snapshot: $snapshot"
zfs destroy "$snapshot"
if [[ $? -ne 0 ]]; then
log "Error: Failed to delete snapshot $snapshot."
fi
done
else
log "No snapshots to delete. Total snapshots ($snapshot_count) <= keep ($keep)."
fi
log "Snapshot management completed."
exit 0