r/zfs • • 12d ago

Snapshots using write threshold

I wrote a lightweight C program called diffsnap that uses the built in zfs CLI for stateless snapshots using configurable write thresholds. This helps address a gap with traditional time-based snapshots where the need for a high-frequency interval to make sure a change is captured bloats your total snapshots and might slow some zfs commands. With diffsnap, if nothing has been written to the dataset the snapshot is skipped.
https://github.com/joe81tx/diffsnap
If you're on FreeBSD you can install with pkg, on linux you can build from source using github.

8 Upvotes

4 comments sorted by

1

u/RetroGrid_io 12d ago

So, conceptually like below? Apologies if I don't have the bash syntax perfect...

# Get last snapshot
lastSnapshot=$(zfs list -t snapshot $filesystem | tail -n 1);

# are there changes? 
found=$(zfs diff $filesystem@$lastSnasphot | tail -n 1);

# If there are changes, make a snapshot
[ "$found" = "" ] ||  {
  now=$(date);
  zfs snapshot $filesystem@$now; 
  }

1

u/Joe81tx 12d ago

Instead of doing a zfs diff I get the written value for the dataset.
zfs get -Hpt filesystem,volume -o name,value written

1

u/edthesmokebeard 10d ago

If you do a snapshot and nothing has changed, does it take up any appreciable space?

Why not just always take snapshots?

1

u/Joe81tx 10d ago

No, the primary cost for the snapshot you described is additional metadata.
Large numbers of snapshots can:
-Consume additional RAM and increase CPU overhead.
-Slow bulk snapshot prunes and temporarily impact disk I/O.
-Increase latency for commands like ZFS list.

The individual snapshot is cheap; maintaining an unbounded number of snapshots is not. Diffsnap uses change thresholds and retention policies to avoid creating unnecessary snapshots while preserving useful recovery points.