Skip to content

Instantly share code, notes, and snippets.

@cmlsharp
Forked from erikw/snp
Last active August 29, 2015 14:06
Show Gist options
  • Save cmlsharp/58dece80a2b6a81206b3 to your computer and use it in GitHub Desktop.
Save cmlsharp/58dece80a2b6a81206b3 to your computer and use it in GitHub Desktop.
#!/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"
if (( EUID != 0 )); then
echo "This script must be run as root" 1>&2
exit 1
fi
if [ ! -d "$log_path" ]; then
mkdir -p $log_path
fi
# 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}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment