Shell znp: Wrap dangerous shell command in ZFS pre-post snapshots and log outputs


New Member

Messages: 4


I just wanted to share a small but handy script with the community, as I think more people than me could find it useful!

Use Case:
You have some dangerous command which could alter your system in a bad way, e.g.:

$ pkg upgrade
$ portmaster -a
$ some_cmd --force --overwrite --scary-option /

and you want to be able to know today, but also in a few weeks:
  • what files on disk changed due to this command
  • complete log output from this command, to correlate to the changes on disk

My solution:
I created a simple wrapper that I call znp (as in "zfs snapshot"), executes a given command and make a tagged ZFS snapshot before and after the command. Furthermore it logs the output of the command to a directory.

To use it, simply prefix it to the dangerous command, e.g.

$ znp pkg upgrade
$ znp portmaster -a

and the command will output the names of the snapshots that were created and some more info. Then you can easily compare at a later point what happened during this command, or roll back, or view log output from that command:

$ zfs diff <znpID>_pre <znpID>_post
$ zfs rollback <znpID>_pre
$ less /var/local/log/znp/<znpID>

Simple but effective!
I've been using an analogous script, called snp, for my BTRFS based systems for 5 years (it looks like in the logs).

Enjoy the znpshots! :)