ZFS Autosnapshot

May 9, 2026

Having ZFS automatically create snapshots is one of the best features of the filesystem. Before we start, let's turn off cron momentarily.

/etc/init.d/cronie stop
 * Caching service dependencies ... [ ok ]
 * Stopping cronie ...              [ ok ]

Now let's install zfs-auto-snapshot.

emerge -av sys-fs/zfs-auto-snapshot
 * Use com.sun:auto-snapshot attribute to enable snapshots for datasets
 * the syntax is:
 *
 * zfs set com.sun:auto-snapshot=[true|false]
 *
 * or:
 *
 * zfs set com.sun:auto-snapshot:<frequent|hourly|daily|weekly|monthly>=[true|false]
 *
 * for example:
 *
 * # zfs set com.sun:auto-snapshot=false zroot
 * Will disalbe all snapshots for zroot and all nested datasets will inherit the property.
 *
 * # zfs set com.sun:auto-snapshot=true zroot/ROOT/default
 * Will enable all types of snapshots for given dataset.
 *
 * # zfs set com.sun:auto-snapshot:weekly=true pool/var
 * Will enable only weekly snapshots for given dataset.
 *
 * for details please visit:
 * https://docs.oracle.com/cd/E19120-01/open.solaris/817-2271/ghzuk/index.html
 *
 * (Note: Above message is only printed the first time package is
 * installed. Please look at /usr/share/doc/zfs-auto-snapshot-1.2.4-r1/README.gentoo*
 * for future reference)

Now let's turn snapshotting on for the root-level filesystem.

zfs set com.sun:auto-snapshot=true tank

You can get a sense for how the snapshots are named with the following

zfs-auto-snapshot --default-exclude --quiet --syslog --label=hourly --keep=24 tank

Use the following to view snapshots.

zfs list tank -t snap
NAME USED AVAIL REFER MOUNTPOINT
tank@zfs-auto-snap_hourly-2021-12-09-2241 0B - 96K -

I hate this format. I would much rather have a YYYY-MM-DD-HHMM-label. We can fix that.

Use an Existing Patch

Gentoo supports user-created patches. Let's create a directory for that.

mkdir -p /etc/portage/patches/sys-fs/zfs-auto-snapshot/

Now we can grab the patch. I also made a post if you need to create your own patch.

pushd /etc/portage/patches/sys-fs/zfs-auto-snapshot/
wget https://gist.githubusercontent.com/west17m/bdadd8abf5f7dc0fc47fc61787223200/raw/f58356213c4c853a89f7bfb3e114c84b99a19359/date-in-front.patch
popd

Now reinstall the package which will pull in the patch.

emerge -av sys-fs/zfs-auto-snapshot
 * User patches applied.

Now check the new format again

zfs-auto-snapshot --default-exclude --quiet --syslog --label=hourly --keep=24 tank
zfs list tank -t snap
NAME                                        USED  AVAIL     REFER  MOUNTPOINT
tank@zfs-auto-snap_hourly-2021-12-09-2241     0B      -       96K  -
tank@2021-12-09-2026-zfs-auto-snap_hourly     0B      -       96K  -

Woot! Next let's remove those temporary snapshots. the % is the wildcard and -r is for recursive.

zfs destroy -r tank@%

zfs list tank -t snap
no datasets available

We are now ready to take a baseline snapshot of the whole filesystem.

zfs snapshot -r tank@$(/bin/date +%F-%H%M)-BASE

zfs list -t snap
NAME                                    USED  AVAIL     REFER  MOUNTPOINT
tank@2021-12-09-2031-BASE                 0B      -       96K  -
tank/SYSTEM@2021-12-09-2031-BASE          0B      -       96K  -
tank/SYSTEM/root@2021-12-09-2031-BASE     0B      -     3.38G  -
tank/SYSTEM/tmp@2021-12-09-2031-BASE      0B      -      120K  -
tank/SYSTEM/var@2021-12-09-2031-BASE      0B      -     1.55G  -

Next we will enable the auto-snapshot property.

zfs set com.sun:auto-snapshot=true tank
zfs set com.sun:auto-snapshot=false tank/SYSTEM/tmp
zfs set com.sun:auto-snapshot=false tank/SYSTEM/var

Hopefully, that makes sense. I want the default to be everything gets autosnapshotted except /tmp and /var. With that done, the final step is to start cron back up.

/etc/init.d/cronie start