diff options
| author | Benjamin Linskey | 2025-11-13 23:01:33 -0500 |
|---|---|---|
| committer | Benjamin Linskey | 2025-11-13 23:01:33 -0500 |
| commit | 74b7a1ad39fe4e77f10ae148bf91dd03d51f4862 (patch) | |
| tree | 684e9b5b1f580b77555561e4ec801f5621fd6f0c | |
| parent | 52da4bda88d915adba69af03557e5d49b0fd6e1b (diff) | |
| download | zfs-snapshots-74b7a1ad39fe4e77f10ae148bf91dd03d51f4862.tar.gz | |
Fix spacing
snapshots.sh was somehow inadvertently written with a mix of spaces and tabs for indentation. Retabbed with four-space tabs.
| -rwxr-xr-x | snapshots.sh | 226 |
1 files changed, 113 insertions, 113 deletions
diff --git a/snapshots.sh b/snapshots.sh index 8bdb161..3f60bd6 100755 --- a/snapshots.sh +++ b/snapshots.sh @@ -19,9 +19,9 @@ # At least one of the following options must be provided to specify an # operation to perform: # -# -c create snapshot(s) -# -p prune snapshots -# -l list snapshots +# -c create snapshot(s) +# -p prune snapshots +# -l list snapshots # # If the -c or -p option is specified, at least one dataset must be specified # as well. @@ -38,17 +38,17 @@ # # Usage: snapshots.sh [-cplrnvh] [-t tag] [-k num] [dataset ...]" # -# -c create snapshot(s) (requires -t) -# -p prune snapshots (requires -t and -k) -# -l list snapshots -# -r recursively create, prune, or list snapshots -# -n dry run: print commands that would be executed, but do not -# actually modify data -# -v verbose mode -# -h print usage -# -k num keep *num* snapshots for each dataset, including any newly -# created snapshots -# -t tag use *tag* as the snapshot name prefix +# -c create snapshot(s) (requires -t) +# -p prune snapshots (requires -t and -k) +# -l list snapshots +# -r recursively create, prune, or list snapshots +# -n dry run: print commands that would be executed, but do not +# actually modify data +# -v verbose mode +# -h print usage +# -k num keep *num* snapshots for each dataset, including any newly +# created snapshots +# -t tag use *tag* as the snapshot name prefix set -e @@ -62,136 +62,136 @@ prune= list= usage() { - printf "usage: %s [-cplrnvh] [-t tag] [-k num] [dataset ...]\n" "$0" + printf "usage: %s [-cplrnvh] [-t tag] [-k num] [dataset ...]\n" "$0" } check_args() { - if [ -z "$create" ] && [ -z "$prune" ] && [ -z "$list" ]; then - printf "At least one of -c, -p, and -l must be specified.\n" - usage - exit 1 - fi - - if [ "$#" -eq 0 ] && { [ -n "$create" ] || [ -n "$prune" ]; }; then - printf "At least one dataset must be specified\n" - usage - exit 1 - fi - - if { [ -n "$create" ] || [ -n "$prune" ]; } && [ -z "$tag" ]; then - printf "Missing -t option\n" - usage - exit 1 - fi - - if [ -n "$prune" ] && [ -z "$keep" ]; then - printf "Missing -k option\n" - usage - exit 1 - fi - - if [ -z "$prune" ] && [ -n "$keep" ]; then - printf "\-k option is only valid with -p\n" - usage - exit 1 - fi + if [ -z "$create" ] && [ -z "$prune" ] && [ -z "$list" ]; then + printf "At least one of -c, -p, and -l must be specified.\n" + usage + exit 1 + fi + + if [ "$#" -eq 0 ] && { [ -n "$create" ] || [ -n "$prune" ]; }; then + printf "At least one dataset must be specified\n" + usage + exit 1 + fi + + if { [ -n "$create" ] || [ -n "$prune" ]; } && [ -z "$tag" ]; then + printf "Missing -t option\n" + usage + exit 1 + fi + + if [ -n "$prune" ] && [ -z "$keep" ]; then + printf "Missing -k option\n" + usage + exit 1 + fi + + if [ -z "$prune" ] && [ -n "$keep" ]; then + printf "\-k option is only valid with -p\n" + usage + exit 1 + fi } create_snapshots() { - create_cmd='zfs snapshot' - if [ -n "$recursive" ]; then - create_cmd="$create_cmd -r" - fi - readonly create_cmd - - for dataset in "$@"; do - # FreeBSD's date -I option uses a "+00:00" suffix rather than "Z", and - # the + character is illegal in snapshot names, so we have to specify - # the format manually. - cmd="$create_cmd ${dataset}@${tag}-$(date -z utc +%Y-%m-%dT%H:%M:%SZ)" - - if [ -n "$dry_run" ] || [ -n "$verbose" ]; then - printf "%s\n" "$cmd" - fi - - if [ -z "$dry_run" ]; then - $cmd - fi - done + create_cmd='zfs snapshot' + if [ -n "$recursive" ]; then + create_cmd="$create_cmd -r" + fi + readonly create_cmd + + for dataset in "$@"; do + # FreeBSD's date -I option uses a "+00:00" suffix rather than "Z", and + # the + character is illegal in snapshot names, so we have to specify + # the format manually. + cmd="$create_cmd ${dataset}@${tag}-$(date -z utc +%Y-%m-%dT%H:%M:%SZ)" + + if [ -n "$dry_run" ] || [ -n "$verbose" ]; then + printf "%s\n" "$cmd" + fi + + if [ -z "$dry_run" ]; then + $cmd + fi + done } prune_snapshots() { - for dataset in "$@"; do - if [ -n "$recursive" ]; then - for filesystem in $(zfs list -t filesystem -o name -H -r "$dataset"); do - prune_fs "$filesystem" - done - else - prune_fs "$dataset" - fi - done + for dataset in "$@"; do + if [ -n "$recursive" ]; then + for filesystem in $(zfs list -t filesystem -o name -H -r "$dataset"); do + prune_fs "$filesystem" + done + else + prune_fs "$dataset" + fi + done } # Selectively destroys old snapshots for the filesystem specified by $1. # Behavior is controlled by the global variables $tag, $keep, $dry_run, and # $verbose. prune_fs() { - snapshots=$(zfs list -t snapshot -o name -S name -H "$1") - to_delete=$(printf "%s\n" "$snapshots" | grep "@${tag}-" | tail -n +"$((keep + 1))") - for s in $to_delete; do - cmd="zfs destroy $s" - if [ -n "$dry_run" ] || [ -n "$verbose" ]; then - printf "%s\n" "$cmd" - fi - - if [ -z "$dry_run" ]; then - $cmd - fi - done + snapshots=$(zfs list -t snapshot -o name -S name -H "$1") + to_delete=$(printf "%s\n" "$snapshots" | grep "@${tag}-" | tail -n +"$((keep + 1))") + for s in $to_delete; do + cmd="zfs destroy $s" + if [ -n "$dry_run" ] || [ -n "$verbose" ]; then + printf "%s\n" "$cmd" + fi + + if [ -z "$dry_run" ]; then + $cmd + fi + done } list_snapshots() { - cmd="zfs list" - if [ -n "$recursive" ]; then - cmd="$cmd -r" - fi - snapshots=$($cmd -t snapshot -o name -s name -H "$@") - - if [ -n "$tag" ]; then - printf "%s\n" "$snapshots" | grep "@${tag}-" - else - printf "%s\n" "$snapshots" - fi + cmd="zfs list" + if [ -n "$recursive" ]; then + cmd="$cmd -r" + fi + snapshots=$($cmd -t snapshot -o name -s name -H "$@") + + if [ -n "$tag" ]; then + printf "%s\n" "$snapshots" | grep "@${tag}-" + else + printf "%s\n" "$snapshots" + fi } while getopts t:k:cplrknvh name; do - case $name in - t) tag="$OPTARG";; - k) keep="$OPTARG";; - c) create=1;; - p) prune=1;; - l) list=1;; - r) recursive=1;; - n) dry_run=1;; - v) verbose=1;; - h) usage - exit 0;; - ?) usage - exit 2;; - esac + case $name in + t) tag="$OPTARG";; + k) keep="$OPTARG";; + c) create=1;; + p) prune=1;; + l) list=1;; + r) recursive=1;; + n) dry_run=1;; + v) verbose=1;; + h) usage + exit 0;; + ?) usage + exit 2;; + esac done shift $((OPTIND - 1)) check_args "$@" if [ -n "$create" ]; then - create_snapshots "$@" + create_snapshots "$@" fi if [ -n "$prune" ]; then - prune_snapshots "$@" + prune_snapshots "$@" fi if [ -n "$list" ]; then - list_snapshots "$@" + list_snapshots "$@" fi |