Skip to content

Instantly share code, notes, and snippets.

@erikw
Last active February 11, 2026 02:41
Show Gist options
  • Select an option

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

Select an option

Save erikw/5229436 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
# Requirements: snapper (https://wiki.archlinux.org/title/snapper)
# 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}."
@cristi-neagu
Copy link

It's been 5 years, but I have to ask this:

Why is this script logging to /var/local/log/snp given that writing to that folder requires root privileges? Is it meant to only be run with sudo?

@erikw
Copy link
Author

erikw commented Feb 2, 2026

@cristi-neagu it could be that snapper(1), which is used by snp, requires root privileges? I don't remember.

@cristi-neagu
Copy link

Fair enough, maybe it did. But it doesn't look like it needs them now. Running snp without sudo creates the pre and post snapshots just fine, but fails to write to log because of folder permissions. Which, to be fair, doesn't stop the script from working, but it is annoying.

Maybe I'm doing something wrong here?

@erikw
Copy link
Author

erikw commented Feb 2, 2026

Well, you could just change log_path="/var/local/log/snp" to a path that the user(s)/group(s) you want to run snp with has write permission to. Or change the permission of this directory. There are many ways :)

Using the /var/local/log/ path worked for my use cases, and on the system (Arch) I was on, back then, but it might not for you.

@cristi-neagu
Copy link

cristi-neagu commented Feb 2, 2026

Thanks for the suggestion. Seems like snapper doesn't require root access to create root snapshots on my system for some reason. I can't tell if this is a good thing or a bad thing.

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