Categories
Filesystem Linux ZFS

ZFS and snapshots

One nice feature of filesystems like btrfs of zfs is snapshots. They preserve a certain state of a filesystem without wasting too much space (as long as the modification rate is low).

In some cases it is fine to create a snapshot manually from time to time, however on several occasions I wished I had taken one before changing some configuration – but I didn’t. So in some cases it makes total sense to create snapshots automatically and keep them for a certain amount of time.

That’s exactly what the package zfs-auto-snapshot is used for: create snapshots on a regular basis (and clean them up after some time).

Configure snapshots

com.sun:auto-snapshot=true|false
com.sun:auto-snapshot:hourly=true|false
com.sun:auto-snapshot:daily=true|false
com.sun:auto-snapshot:weekly=true|false
com.sun:auto-snapshot:monthly=true|false
com.sun:auto-snapshot:yearly=true|false
linux # zfs set com.sun:auto-snapshot=true zpool/subdir1
linux # zfs inherit com.sun:auto-snapshot zpool/subdir1

Overview of settings

As settings are inherited it sometimes can be hard to keep an overview. To just list the really set (not inherited) values you can use this command:

linux # zfs get -r all  | grep com.sun  | grep -v com.sun:auto-snapshot-desc | grep -v inherited

List snapshots

linux # zfs list -H -o name -t snapshot zpool/subdir1
<...>
zpool/subdir1@zfs-auto-snap_monthly-2024-11-24-0646
<...>
zpool/subdir1@zfs-auto-snap_weekly-2024-12-15-0642
<...>
zpool/subdir1@zfs-auto-snap_daily-2024-12-18-0638
zpool/subdir1@zfs-auto-snap_daily-2024-12-19-0637
zpool/subdir1@zfs-auto-snap_daily-2024-12-20-0637

Manually removing snapshots

Sometimes you may decide that snapshots are no longer necessary for a certain filesystem. With automated snapshot you may easily have a few dozen snapshots that you’d like to get rid of. Based on the above command to list snapshots you can use something like this to get rid of them:

linux # fs list -H -o name -t snapshot zpool/subdir1 | xargs -n1 zfs destroy

Leave a Reply

Your email address will not be published. Required fields are marked *