home | projects | about

Snapfilter
Copyright (c) 2009 Scott Ludwig
http://www.scottlu.com

Download snapfilter

Snapfilter Copyright (c) 2009 Scott Ludwig

Snapfilter is a filtering tool which given a list of zfs snapshots, determines which to retain, and which to remove, according to a user specified retention policy. The default output is the list of snapshots to remove.

The retention policy is specified with command line arguments, and can by specified with yearly, quarterly, monthly, weekly, and daily periods. Each setting, if not specified on the command line, has a default:

2 Yearly backups will be kept by default
4 Quarterly backups will be kept by default
6 Monthly backups will be kept by default
12 Weekly backups will be kept by default
28 Daily backups will be kept by default

The snapshot list is read from stdin. Items in the list are assumed to use the '%Y.%m.H.%M.%S' strptime format at the end of each line. For example:

tank/data@daily-2008.12.14-01.02.03
tank/data@daily-2007.01.22-01.02.03
tank/data@daily-2006.04.19-01.02.03

Command Line Arguments:
 
python snapfilter.py [-t] [-s] [-v] [-y #] [-q #] [-m #] [-w #] [-d #]
-t Run unit tests, and exit
-s Output snapshot list sample, and exit
-v Verbose output categorizing every snapshot
-y # number of yearly backups to keep
-q # number of quarterly backups to keep
-m # number of monthly backups to keep
-w # number of weekly backups to keep
-d # number of daily backups to keep

Examples:
See sample verbose output, with a 60 day, 52 week, and 5 year retention policy (-s just provides sample input):

python snapfilter.py -s | python snapfilter.py -v -d 60 -w 52 -y 5 | less

See just the removes from the sample input, with the default retention policy:

python snapfilter.py -s | python snapfilter.py

Example script to be run once a day via cron job. This script makes a snapshot, then deletes snapshots that are no longer in the default retention policy.

zfs snapshot tank/data@daily-`date +%Y.%m.H.%M.%S`
for snapshot in $(zfs list -r -t snapshot -H -o name tank/data | grep "tank/data@daily" | python snapfilter.py); do zfs destroy $snapshot; done

History:

v 0.1 04/21/2009 scottlu
- first version

License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
last modified April 24, 2009, at 11:03 PM