Automated snapshots?

All your general support questions for OpenZFS on OS X.

Automated snapshots?

Postby Sharko » Wed Sep 07, 2016 12:09 pm

So, is anyone doing automated snapshots? Care to share your technique or wizardry? Allan Jude's ZFS book mentions a FreeBSD package called "zfstools," which is apparently a collection of cron-based scripts in Ruby (https://github.com/bdrewery/zfstools). I haven't used Ruby before, and the scripts would probably need to be modified to work with launchd instead of cron. Has anyone done this? The scripts are quite elegant, and make use of ZFS properties to control how they work.

I also came across some scripts written in Bash shell: https://bitbucket.org/mmichele/zfssnap . These look a little more universal, a little more primitive, but definitely portable to OS X. Has anyone worked with this one, either?

If no one has done it, I'll probably work with the Bash scripts to implement something eventually.

Thanks,

Kurt
Sharko
 
Posts: 230
Joined: Thu May 12, 2016 12:19 pm

Re: Automated snapshots?

Postby stumble » Wed Sep 07, 2016 3:04 pm

I use this:

Code: Select all
#!/bin/bash

##
# original code: http://andyleonard.com/2010/04/07/automatic-zfs-snapshot-rotation-on-freebsd/
# 07/17/2011 - ertug: made it compatible with zfs-fuse which doesn't have .zfs directories
##

# Path to ZFS executable:
ZFS=/usr/sbin/zfs
 
# Parse arguments:
TARGET=$1
SNAP=$2
COUNT=$3
 
# Function to display usage:
usage() {
    scriptname=`/usr/bin/basename $0`
    echo "$scriptname: Take and rotate snapshots on a ZFS file system"
    echo
    echo "  Usage:"
    echo "  $scriptname target snap_name count"
    echo
    echo "  target:    ZFS file system to act on"
    echo "  snap_name: Base name for snapshots, to be followed by a '.' and"
    echo "             an integer indicating relative age of the snapshot"
    echo "  count:     Number of snapshots in the snap_name.number format to"
    echo "             keep at one time.  Newest snapshot ends in '.0'."
    echo
    exit
}
 
# Basic argument checks:
if [ -z $COUNT ] ; then
    usage
fi
 
if [ ! -z $4 ] ; then
    usage
fi
 
# Snapshots are numbered starting at 0; $max_snap is the highest numbered
# snapshot that will be kept.
max_snap=$(($COUNT -1))
 
# Clean up oldest snapshot:
$ZFS list -t snapshot | grep -q ^${TARGET}@${SNAP}\.${max_snap}
if [ $? -eq 0 ] ; then
    $ZFS destroy -r ${TARGET}@${SNAP}.${max_snap}
fi
 
# Rename existing snapshots:
dest=$max_snap
while [ $dest -gt 0 ] ; do
    src=$(($dest - 1))
    $ZFS list -t snapshot | grep -q ^${TARGET}@${SNAP}\.${src}
    if [ $? -eq 0 ] ; then
    $ZFS rename -r ${TARGET}@${SNAP}.${src} ${TARGET}@${SNAP}.${dest}
    fi
    dest=$(($dest - 1))
done
 
# Create new snapshot:
$ZFS snapshot -r ${TARGET}@${SNAP}.0
stumble
 
Posts: 37
Joined: Thu May 15, 2014 7:05 pm

Re: Automated snapshots?

Postby Brendon » Wed Sep 07, 2016 5:00 pm

I use znapzend. Its available in brew (http://brew.sh/)

- Brendon
Brendon
 
Posts: 286
Joined: Thu Mar 06, 2014 12:51 pm

Re: Automated snapshots?

Postby Sharko » Wed Sep 07, 2016 7:33 pm

Thank you both for your suggestions!
Sharko
 
Posts: 230
Joined: Thu May 12, 2016 12:19 pm

Re: Automated snapshots?

Postby haer22 » Thu Sep 08, 2016 1:48 am

For the snapshots I use zfsnap.sh.

Here are som lines from my crontab:
Code: Select all
#
# ZFS snappies
#
ZF=/Users/hans/bin/zfsnap-master/sbin/zfsnap.sh
#
0,30 * * * *    $ZF snapshot -v -r -z -a 5d zeus
1,31 0 * * *    $ZF snapshot -v -r -z -a 5w zeus
2,32 0 * * Mon  $ZF snapshot -v -r -z -a 3m zeus
3,33 0 1 * *    $ZF snapshot -v -r -z -a 3y zeus

The above lines gives me twice hourly snapshot that are saved for 5d, dailies are saved for 5w, etc.

The cleanup part is done by:
Code: Select all
25,55 * * * * $ZF destroy -vr zeus


zeus is my alive pool which I copy to my backup pool (gaia) by using zxfer:
Code: Select all
#
#  ZFS mirroring
#
ZX=/usr/local/bin/zxfer
# we run it after the new snapshots, and after the cleanup of old snapshots
5,35 * * * *    $ZX -V -dFy -R zeus gaia

zxfer will add and remove snapshots so gaia is always a copy of the current zeus.

Used them for 2-3 years, i.e. since I started using ZFS.
haer22
 
Posts: 123
Joined: Sun Mar 23, 2014 2:13 am

Re: Automated snapshots?

Postby tim.rohrer » Thu Sep 06, 2018 9:51 am

@haer22

I know this post is almost two years old, but I was curious as to which zxfer you were/are using? I found the version on GitHub that Allan Jude took over. However, I'm having trouble finding explanations for some of the options you're using.

-F is shown as an option in the man page, but I have not yet found an explanation of what it does. However, in your example am I correct in assuming that is being passed "forced, yes" as an option for deleting snapshots on the destination?

-V is not shown at all.

I assume -R is recursive?
tim.rohrer
 
Posts: 29
Joined: Tue Jul 24, 2018 6:49 pm

Re: Automated snapshots?

Postby tgunr » Sun Mar 03, 2019 8:44 am

For stumblers, https://github.com/jimsalterjrs/sanoid works just fine thank you very much
tgunr
 
Posts: 1
Joined: Sat Mar 02, 2019 7:17 am

Re: Automated snapshots?

Postby haer22 » Fri Mar 08, 2019 10:10 am

@tim_rohrer

Sorry for the very delayed reply :-)

There is no version info in the source apart from the following Copyright lines:
Code: Select all
#!/bin/sh
# BSD HEADER START
# Copyright (c) 2010,2011 Ivan Nash Dreckman
# Copyright (c) 2007, 2008 Constantin Gonzalez


-F means delete newer snapshot if needed on the destination. like in zfs recv....
-V is my own extension where I ger some statistics from zfs send -P and -v

Works flawlessly
haer22
 
Posts: 123
Joined: Sun Mar 23, 2014 2:13 am

Re: Automated snapshots?

Postby Sharko » Fri May 10, 2019 10:28 pm

I've updated the wiki with a guide to setting up automated snapshots using zfstools (the Ruby script that implements zfs-auto-snapshot). You can find it here:

https://openzfsonosx.org/wiki/Auto_snapshots

Kurt
Sharko
 
Posts: 230
Joined: Thu May 12, 2016 12:19 pm

Re: Automated snapshots?

Postby jordancurve » Mon Jun 21, 2021 8:06 pm

https://openzfsonosx.org/wiki/Auto_snapshots says:

In the Linux or FreeBSD environment this would be accomplished with the utility ‘cron’, but in Mac OS X we have to work with the launchd daemon.


Why can't we use cron on OS X? I use it for some things on OS X and it seems to work OK.
User avatar
jordancurve
 
Posts: 19
Joined: Sun Jun 06, 2021 4:19 pm

Next

Return to General Help

Who is online

Users browsing this forum: No registered users and 20 guests