Create a gist now

Instantly share code, notes, and snippets.

@erikw /snp
Last active Mar 25, 2017

What would you like to do?
Wrap shell command around btrfs snapper pre-post snapshots and log outputs.
#!/usr/bin/env bash
# Runs a command wrapped in btrfs snapper pre-post snapshots.
date=$(date "+%Y-%m-%d-%H%M%S")
! [ -d $log_path ] && mkdir -p $log_path
# Log stdout and stderr. Reference:
exec > >(tee -a "$log_file")
exec 2> >(tee -a "$log_file" >&2)
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}."

cig0 commented Nov 3, 2014

Hi! Out of curiosity, why did you choose /var/local/log instead /var/log?


erikw commented Nov 16, 2014

Hi msx!

I use the local directory to not confuse my custom files and scripts with the systems. The very same reason people put their own system scripts under /usr/local/{bin,sbin} instead of /usr/{bin,sbin} :)

cig0 commented Jun 2, 2015

Just passing by to say TY and let you know that I'm using snp all the time for all sort of things. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment