public
Last active

Wrap shell command around btrfs snapper pre-post snapshots and log outputs.

  • Download Gist
snp
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/env bash
# Runs a command wrapped with btrfs pre-post snapshots.
log_path="/var/local/log/snp"
date=$(date "+%Y-%m-%d-%H%M%S")
log_file="${log_path}/snp_${date}.log"
 
# 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="$@"
 
snapshot_nbr=$(snapper create --type=pre --cleanup-algorithm=number --print-number --description="${cmd}")
echo -e "> New pre snapshot with number ${snapshot_nbr}."
echo -e "> Running command \"${cmd}\"."
 
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}."

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.