Skip to content

Instantly share code, notes, and snippets.

@erikw
Last active March 4, 2026 21:56
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}."
@cig0

cig0 commented Nov 3, 2014

Copy link
Copy Markdown

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

@erikw

erikw commented Nov 16, 2014

Copy link
Copy Markdown
Author

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

cig0 commented Jun 2, 2015

Copy link
Copy Markdown

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

@erikw

erikw commented May 29, 2018

Copy link
Copy Markdown
Author

ZFS users!

Check out znp

=)

@Fire100265

Copy link
Copy Markdown

Hi,
May I submit a PKGBUILD for this script to the AUR? I believe it will help out a lot of people.

@erikw

erikw commented Dec 20, 2020

Copy link
Copy Markdown
Author

@Fire100265 Hey, you're very welcome to make a PKGBUILD for this! I too find this little utility immensely useful for many semi- to dangerous Linux operational tasks.

I don't know how well PKGBUILDs work with gists. If it's not working out well, I can move this script to a proper Github git repo. Let me know if the Gist works first though

@Fire100265

Fire100265 commented Dec 21, 2020

Copy link
Copy Markdown

@erikw Thank you! I have written a PKGBUILD and I will be submitting it to the AUR soon. As you have predicted, gists do not work very well with PKGBUILDs. Due to this, I copied all the text into a file and used it as the source.
Edit: Here is the link to the AUR page. Please let me know of any changes I should make. I can add you as a maintainer if you wish.

@erikw

erikw commented Dec 30, 2020

Copy link
Copy Markdown
Author

@Fire100265 The PKGBUILD looks great!

There can only be 1 maintainer in AUR right? To distribute comments and future maintenance questions you could tell people to reach out to me as well of your unavailable, here at Github or my AUR profile https://aur.archlinux.org/account/erikw :)

@Fire100265

Fire100265 commented Dec 30, 2020

Copy link
Copy Markdown

Thank you for your reply! I added you as a maintainer. Please let me know if you can access the package.
Edit: please edit the PKGBUILD to add your email.

@erikw

erikw commented Dec 30, 2020

Copy link
Copy Markdown
Author

@cristi-neagu

Copy link
Copy Markdown

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

erikw commented Feb 2, 2026

Copy link
Copy Markdown
Author

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

@cristi-neagu

Copy link
Copy Markdown

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

erikw commented Feb 2, 2026

Copy link
Copy Markdown
Author

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

cristi-neagu commented Feb 2, 2026

Copy link
Copy Markdown

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