How to Configure ZFS Auto-Snapshot Retention: Weekly (52) and Monthly (Unlimited) in ZFS Filesystems


2 views

When working with ZFS auto-snapshots, the retention policy is controlled through specific dataset properties. The common mistake many administrators make is trying to pass retention parameters directly in the zfs set command, which results in the "invalid dataset name" error shown in your example.

# This is INCORRECT syntax (will fail):
sudo zfs set com.sun:auto-snapshot:weekly=true storage keep=52

For weekly snapshots (keeping 52):

sudo zfs set com.sun:auto-snapshot:weekly=true storage
sudo zfs set com.sun:auto-snapshot:weekly:keep=52 storage

For monthly snapshots (keeping indefinitely):

sudo zfs set com.sun:auto-snapshot:monthly=true storage
sudo zfs set com.sun:auto-snapshot:monthly:keep=0 storage

Note that setting keep=0 means unlimited retention for monthly snapshots.

After setting these properties, verify with:

zfs get -r com.sun:auto-snapshot:weekly:keep storage
zfs get -r com.sun:auto-snapshot:monthly:keep storage

Here's a full setup example for a dataset named "tank/data":

# Enable automatic snapshots
sudo zfs set com.sun:auto-snapshot=true tank/data

# Configure weekly (52 snapshots)
sudo zfs set com.sun:auto-snapshot:weekly=true tank/data
sudo zfs set com.sun:auto-snapshot:weekly:keep=52 tank/data

# Configure monthly (unlimited)
sudo zfs set com.sun:auto-snapshot:monthly=true tank/data
sudo zfs set com.sun:auto-snapshot:monthly:keep=0 tank/data

# Optional: Configure other intervals
sudo zfs set com.sun:auto-snapshot:daily=true tank/data
sudo zfs set com.sun:auto-snapshot:daily:keep=14 tank/data

The actual snapshot creation is handled by cron jobs installed with zfs-auto-snapshot. Typical locations:

  • /etc/cron.d/zfs-auto-snapshot
  • /etc/cron.hourly/zfs-auto-snapshot

Ensure these are configured to run at appropriate intervals for your retention policy.

If snapshots aren't being automatically removed:

  1. Verify the cron job is running
  2. Check for syntax errors in the properties
  3. Ensure no manual snapshots are interfering (auto-snapshots have specific naming patterns)

The zfs-auto-snapshot tool uses ZFS properties to control snapshot behavior. To specify retention policies, you'll need to set the appropriate numeric properties for each snapshot frequency.

To keep exactly 52 weekly snapshots (approximately one year's worth), use:

sudo zfs set com.sun:auto-snapshot:weekly=52 storage

For indefinite retention of monthly snapshots, set the value to 0:

sudo zfs set com.sun:auto-snapshot:monthly=0 storage

Check your settings with:

zfs get -r com.sun:auto-snapshot:weekly,com.sun:auto-snapshot:monthly storage

The retention count must be specified as part of the property value, not as a separate argument. These commands will fail:

# Incorrect syntax examples
sudo zfs set com.sun:auto-snapshot:weekly=true storage keep=52
sudo zfs set com.sun:auto-snapshot:weekly=true storage --keep=52

You can combine multiple retention policies:

# Keep 7 daily, 52 weekly, and unlimited monthly snapshots
sudo zfs set com.sun:auto-snapshot:daily=7 storage
sudo zfs set com.sun:auto-snapshot:weekly=52 storage
sudo zfs set com.sun:auto-snapshot:monthly=0 storage

The automatic cleanup removes the oldest snapshots when the count exceeds your specified limit. Snapshots are named with timestamps, making age-based deletion straightforward.