Skip to content

Instantly share code, notes, and snippets.

@yyolk
Forked from erikw/snp
Created April 28, 2021 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yyolk/cb9f0c9a200c71908f4a221edc32dee9 to your computer and use it in GitHub Desktop.
Save yyolk/cb9f0c9a200c71908f4a221edc32dee9 to your computer and use it in GitHub Desktop.
snp: Wrap shell command in BTRFS snapper pre-post snapshots and log outputs.
#!/usr/bin/env bash
# Runs a command wrapped in btrfs snapper pre-post snapshots.
# Usage: $ snp <commands>
# e.g.: $ snp pacman -Syyu
# The latest version of this script is hosted at https://gist.github.com/erikw/5229436
log_path="/var/local/log/snp"
date=$(date "+%Y-%m-%d-%H%M%S")
log_file="${log_path}/snp_${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}"
snapshot_nbr=$(snapper create --type=pre --cleanup-algorithm=number --print-number --description="${cmd}")
echo "> New pre snapshot with number ${snapshot_nbr}."
echo -e "> Running command \"${cmd}\".\n"
eval "$cmd"
snapshot_nbr=$(snapper create --type=post --cleanup-algorithm=number --print-number --pre-number="$snapshot_nbr")
echo -e "\n> New post snapshot with number ${snapshot_nbr}."
# Snapper has a --command option nowadays. But it works worse, the output from the command is not printed separately from the snaptshot number, just becomes a mess.
#echo "> Running command \"${cmd}\"."
#snapshot_nbr=$(snapper create --command "${cmd}" --print-number --cleanup-algorithm=number --description="${cmd}" | tail -1)
#echo -e "\n> New pre-post snapshot with numbers ${snapshot_nbr}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment