Automatic Sanoid ZFS snapshots & pruning

Add your Tips and tricks to configure and tweak your GhostBSD System here.
Post Reply
q-pa
Posts: 7
Joined: Mon Nov 22, 2021 5:35 am

Automatic Sanoid ZFS snapshots & pruning

Post by q-pa »

I installed sanoid and set it up to automatically make a snapshot of my home dataset every 15 minutes but keep only the following numbers of snapshots, the rest is pruned in the same run:
frequently = 2
hourly = 5
daily = 7
monthly = 1
yearly = 0
That's 15 snapshots at any given time.

I did not set up syncoid to send and sync my snapshots on a remote system. For syncoid to work you need to follow some additional advise here
https://github.com/jimsalterjrs/sanoid/ ... md#freebsd and here
https://github.com/jimsalterjrs/sanoid/ ... BSD.readme

According to the info on https://github.com/jimsalterjrs/sanoid you need to install the following package:
sanoid

I had to to drop a symlink on my system (alternatively FreeBSD users need to change the Perl shebangs at the top of the executables from #!/usr/bin/perl
to #!/usr/local/bin/perl in most cases) (https://github.com/jimsalterjrs/sanoid/ ... BSD.readme):

Code: Select all

# ln -s /usr/local/bin/perl /usr/bin/perl
The sanoid .conf files can be found here: /usr/local/etc/sanoid

The sanoid.defaults.conf file should not be edited. Instead, create a file named sanoid.conf at the same location.

These are the contents of my sanoid.conf file:

Code: Select all

######################################
# This is a sample sanoid.conf file. #
# It should go in /etc/sanoid.       #
######################################

# name your backup modules with the path to their ZFS dataset - no leading slash.
[zroot/usr/home]
	use_template = production
	recursive = yes

#############################
# templates below this line #
#############################

# name your templates template_templatename. you can create your own, and use them in your module definitions above.

[template_demo]
	daily = 60

[template_production]
	frequently = 2
	hourly = 5
	daily = 7
	monthly = 1
	yearly = 0
	autosnap = yes
	autoprune = yes

[template_backup]
	autoprune = yes
	frequently = 0
	hourly = 30
	daily = 90
	monthly = 12
	yearly = 0

	### don't take new snapshots - snapshots on backup
	### datasets are replicated in from source, not
	### generated locally
	autosnap = no

	### monitor hourlies and dailies, but don't warn or
	### crit until they're over 48h old, since replication
	### is typically daily only
	hourly_warn = 2880
	hourly_crit = 3600
	daily_warn = 48
	daily_crit = 60

[template_hotspare]
	autoprune = yes
	frequently = 0
	hourly = 30
	daily = 90
	monthly = 3
	yearly = 0

	### don't take new snapshots - snapshots on backup
	### datasets are replicated in from source, not
	### generated locally
	autosnap = no

	### monitor hourlies and dailies, but don't warn or
	### crit until they're over 4h old, since replication
	### is typically hourly only
	hourly_warn = 4h
	hourly_crit = 6h
	daily_warn = 2d
	daily_crit = 4d

[template_scripts]
	### dataset and snapshot name will be supplied as environment variables
	### for all pre/post/prune scripts ($SANOID_TARGET, $SANOID_SNAPNAME)
	### run script before snapshot
	pre_snapshot_script = /path/to/script.sh
	### run script after snapshot
	post_snapshot_script = /path/to/script.sh
	### run script after pruning snapshot
	pruning_script = /path/to/script.sh
	### don't take an inconsistent snapshot (skip if pre script fails)
	#no_inconsistent_snapshot = yes
	### run post_snapshot_script when pre_snapshot_script is failing
	#force_post_snapshot_script = yes
	### limit allowed execution time of scripts before continuing (<= 0: infinite)
	script_timeout = 5

[template_ignore]
	autoprune = no
	autosnap = no
	monitor = no
Last thing to do is adding this line in the root crontab:

Code: Select all

* * * * * /usr/local/bin/sanoid --cron
nevets
Posts: 149
Joined: Tue Jun 23, 2020 3:54 am

Re: Automatic Sanoid ZFS snapshots & pruning

Post by nevets »

Are there pro and cons with sanoid compared to the zfs man page example that can be dropped into a script for cron?
Example - Performing a Rolling Snapshot
The following example shows how to maintain a history of snapshots with a consistent naming scheme. To keep a week's worth of snapshots, the user destroys the oldest snapshot, renames the remaining snapshots, and then creates a new snapshot, as follows:
# zfs destroy -r pool/users@7daysago
# zfs rename -r pool/users@6daysago @7daysago
# zfs rename -r pool/users@5daysago @6daysago
# zfs rename -r pool/users@4daysago @5daysago
# zfs rename -r pool/users@3daysago @4daysago
# zfs rename -r pool/users@2daysago @3daysago
# zfs rename -r pool/users@yesterday @2daysago
# zfs rename -r pool/users@today @yesterday
# zfs snapshot -r pool/users@today
q-pa
Posts: 7
Joined: Mon Nov 22, 2021 5:35 am

Re: Automatic Sanoid ZFS snapshots & pruning

Post by q-pa »

For me it was just the most convenient solution. Maybe in combination with syncoid but then... you can write a script for that too, of course.

Share your script, if you like, so other readers have alternatives to chose from.
Post Reply