Skip to content

PrePost

DatuX edited this page Nov 13, 2026 · 4 revisions

Running custom commands before and after snapshotting

You can run commands before and after the snapshot. For example: freeze a database, so that the data on the disk is consistent.

When is this needed?

Usually it is not necessary.

ZFS snapshots are atomic per pool. Writes during the snapshot do not cause corruption. After a restore, the application sees the same state as after a crash. Most modern databases and applications can recover from this.

If you use more than one pool, the snapshots are only atomic per pool.

Method 1: Use snapshot mode

Use zfs-autobackup without a target to make only the snapshot. Put the commands around it in a script:

# freeze
some-freeze-command

# make the snapshot
zfs-autobackup backup1

# unfreeze
some-unfreeze-command

# make the backup
zfs-autobackup backup1 --no-snapshot --ssh-target backupserver backups/db1

Disadvantages:

  • You must do the error handling yourself.
  • If the source is remote, you must write the SSH commands and the escaping yourself.

Method 2: Use --pre-snapshot-cmd and --post-snapshot-cmd

zfs-autobackup runs the commands for you:

zfs-autobackup \
 --pre-snapshot-cmd 'some-freeze-command'\
 --post-snapshot-cmd 'some-unfreeze-command'\
 --ssh-target backupserver backups/db1

How it works:

  • zfs-autobackup always runs the commands on the source. It uses SSH if necessary.
  • If a pre-command fails, zfs-autobackup runs the post-commands and stops with an error code.
  • zfs-autobackup always runs all post-commands, also when a pre-command or the snapshot fails. Thus, it always unfreezes.

Clone this wiki locally