Skip to content

Instantly share code, notes, and snippets.

@erikw
Last active September 11, 2024 21:03
Show Gist options
  • Select an option

  • Save erikw/eeec35be33e847c211acd886ffb145d5 to your computer and use it in GitHub Desktop.

Select an option

Save erikw/eeec35be33e847c211acd886ffb145d5 to your computer and use it in GitHub Desktop.

Revisions

  1. erikw revised this gist Mar 6, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion znp
    Original file line number Diff line number Diff line change
    @@ -30,9 +30,11 @@ echo -e "> Running command \"${cmd}\".\n"

    eval "$cmd"

    ecode="$?"
    echo -e "\n> Command exit code: ${ecode}"
    snapshot_post="${zfs_pool}@${snapshot_prefix}_post"
    zfs snapshot -r $snapshot_post
    echo -e "\n> New post snapshot created: ${snapshot_post}"
    echo "> New post snapshot created: ${snapshot_post}"


    echo "> See the diff between them using:"
  2. erikw revised this gist Jun 23, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion znp
    Original file line number Diff line number Diff line change
    @@ -43,4 +43,7 @@ echo "> Destroy these snapshots with:"
    echo "zfs destroy -r ${snapshot_pre} && zfs destroy -r ${snapshot_post}"

    echo "> Destroy all snapshots created with znp:"
    echo "zfs list -H -o name -t snapshot | grep "${zfs_pool}@znp_" | xargs -n1 zfs destroy -r"
    echo "zfs list -H -o name -t snapshot | grep "${zfs_pool}@znp_" | xargs -n1 zfs destroy -r"

    echo "> Destroy the 10 oldest snapshot pairs created with znp:"
    echo "zfs list -H -o name -t snapshot | grep "${zfs_pool}@znp_" | head -20 | xargs -n1 zfs destroy -r"
  3. erikw revised this gist Jun 5, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion znp
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ zfs snapshot -r $snapshot_post
    echo -e "\n> New post snapshot created: ${snapshot_post}"


    echo "> See the diff between them using"
    echo "> See the diff between them using:"
    # Tip: add -t to xargs to print every individual command.
    echo "zfs list -t snapshot -o name | grep ${snapshot_prefix} | cut -d@ -f1 | uniq | xargs -I{} -n1 sh -c 'zfs diff -F {}@${snapshot_prefix}_pre {}@${snapshot_prefix}_post 2>/dev/null'"

  4. erikw revised this gist Jun 5, 2018. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions znp
    Original file line number Diff line number Diff line change
    @@ -34,11 +34,13 @@ snapshot_post="${zfs_pool}@${snapshot_prefix}_post"
    zfs snapshot -r $snapshot_post
    echo -e "\n> New post snapshot created: ${snapshot_post}"

    echo -e "\n> Destroy these snapshots with:"
    echo "zfs destroy -r ${snapshot_pre} && zfs destroy -r ${snapshot_post}"

    echo "> See the diff between them using"
    echo "zfs diff ${snapshot_pre} ${snapshot_post}"
    echo "> See the diff between them using"
    # Tip: add -t to xargs to print every individual command.
    echo "zfs list -t snapshot -o name | grep ${snapshot_prefix} | cut -d@ -f1 | uniq | xargs -I{} -n1 sh -c 'zfs diff -F {}@${snapshot_prefix}_pre {}@${snapshot_prefix}_post 2>/dev/null'"

    echo "> Destroy these snapshots with:"
    echo "zfs destroy -r ${snapshot_pre} && zfs destroy -r ${snapshot_post}"

    echo "> Destroy all snapshots created with znp:"
    echo "zfs list -H -o name -t snapshot | grep "${zfs_pool}@znp_" | xargs -n1 zfs destroy -r"
  5. erikw revised this gist May 29, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions znp
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    # Analogous to my snp script for BTRFS: https://gist.github.com/erikw/5229436
    # Usage: $ znp <commands>
    # e.g.: $ znp pgk upgrade
    # e.g.: $ znp portmaster -aG
    # e.g.: $ znp freebsd-upgrade install

    zfs_pool=zroot
  6. erikw revised this gist May 29, 2018. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions znp
    Original file line number Diff line number Diff line change
    @@ -28,14 +28,12 @@ echo "> New pre snapshot created: ${snapshot_pre}"
    echo -e "> Running command \"${cmd}\".\n"

    eval "$cmd"
    echo -e "\n"

    snapshot_post="${zfs_pool}@${snapshot_prefix}_post"
    zfs snapshot -r $snapshot_post
    echo "> New post snapshot created: ${snapshot_post}"
    echo -e "\n> New post snapshot created: ${snapshot_post}"

    echo -e "\n"
    echo "> Destroy these snapshots with:"
    echo -e "\n> Destroy these snapshots with:"
    echo "zfs destroy -r ${snapshot_pre} && zfs destroy -r ${snapshot_post}"

    echo "> See the diff between them using"
  7. erikw created this gist May 29, 2018.
    45 changes: 45 additions & 0 deletions znp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/usr/bin/env bash
    # Runs a command wrapped in ZFS pre-post snapshots. The whole data pool is recursively snapshotted.
    # Analogous to my snp script for BTRFS: https://gist.github.com/erikw/5229436
    # Usage: $ znp <commands>
    # e.g.: $ znp pgk upgrade
    # e.g.: $ znp freebsd-upgrade install

    zfs_pool=zroot

    log_path="/var/local/log/znp"
    date=$(date "+%Y-%m-%d-%H%M%S")
    snapshot_prefix="znp_${date}"
    log_file="${log_path}/znp_${date}.log"

    ! [ -d $log_path ] && mkdir -p $log_path
    # Log stdout and stderr. Reference: http://stackoverflow.com/questions/3173131/redirect-copy-of-stdout-to-log-file-from-within-bash-script-itself
    exec > >(tee -a "$log_file")
    exec 2> >(tee -a "$log_file" >&2)

    cmd="$@"

    echo "> Logging to: ${log_file}"

    # snapper create --type=pre --cleanup-algorithm=number --print-number --description="${cmd}"
    snapshot_pre="${zfs_pool}@${snapshot_prefix}_pre"
    zfs snapshot -r $snapshot_pre
    echo "> New pre snapshot created: ${snapshot_pre}"
    echo -e "> Running command \"${cmd}\".\n"

    eval "$cmd"
    echo -e "\n"

    snapshot_post="${zfs_pool}@${snapshot_prefix}_post"
    zfs snapshot -r $snapshot_post
    echo "> New post snapshot created: ${snapshot_post}"

    echo -e "\n"
    echo "> Destroy these snapshots with:"
    echo "zfs destroy -r ${snapshot_pre} && zfs destroy -r ${snapshot_post}"

    echo "> See the diff between them using"
    echo "zfs diff ${snapshot_pre} ${snapshot_post}"

    echo "> Destroy all snapshots created with znp:"
    echo "zfs list -H -o name -t snapshot | grep "${zfs_pool}@znp_" | xargs -n1 zfs destroy -r"