This page contains scripts, links to other sites and "howto" docs that the staff at Open Technologies Inc. commonly use. You are free to use this material as you wish, but we take NO responsibility for your proper or improper use of this material. See Limitation of Liability below. ----------------------------------------------------------------------------- #!/bin/sh # Description: Create and deletes a rotating list of zfs snapshots # Can be daily, weekly, monthly, etc. or a combination # and depends upon what you put in the cron job. #------------------------------------------------------------------ # Author: Phillip Fiedler # Created: 1/29/07 # Revised: 2/9/07 # Revised: 10/10/07 v0.2 # Revised: 11/23/09 v0.3 # Revised: v0.4 (this version needs to move the snapshots.cur to the filesystems # so when the zpool moves to another server, the snapshot logs go also. #------------------------------------------------------------------ # Number of snapshots to save/rotate numsnaps="7" # Identifier (eg., daily, weekly, monthly, etc.) id=daily cur="/opt/scripts/snapshots/$id/snapshots.cur" new="/opt/scripts/snapshots/$id/snapshots.new" lst="/opt/scripts/snapshots/$id/zfs.list" #------------------------------------------------------------------ # Generate ZFS filesystem list if one wasn't given on the command line if [ "$1" = "" ]; then zfs list | awk '{print $1}' | grep '\/' | grep -v '@' > $lst fi # Check to make sure the list file exists if [ ! -f $lst ]; then echo "Usage: Must provide a list of zfs filesystems in '$lst' for snapshot rotation." echo " Error: Unable to use 'zfs list | awk '{print \$1}' | grep '\/' > $lst' to create list." echo "" exit 0 fi # Create the "cur" file if it doesn't exist if [ ! -f $cur ]; then touch $cur fi # Get Current Date and put in the "snapshots.new" file newsnap="$id-"`date '+%m%d%y-%H%M%S'` echo $newsnap > $new # If the current snapshot file exists and has recorded snapshot history if [ -f $cur -a "`cat $cur | wc -l | awk '{print $1}`" -ge "$numsnaps" ]; then # Put newest snapshot dates in new snapshot list file head -"`echo $numsnaps-1 | bc`" $cur >> $new # Get oldest snapshot date oldsnap=`tail -1 $cur` # Remove current snapshot list file rm $cur else cat $cur >> $new fi # Rename new to current mv $new $cur # Create new snapshots for i in `cat $lst`; do zfs snapshot $i@$newsnap done # Delete the oldest snapshots if [ "$oldsnap" != "" ]; then for i in `cat $lst`; do zfs destroy $i@$oldsnap done fi exit 0 -------------------------------------------------------------------------------------- LIMITATION OF LIABILITY TO THE FULL EXTENT PERMITTED BY LAW, HOST IS NOT LIABLE TO YOU OR ANY OTHER INDIVIDUAL OR ENTITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, PUNITIVE, SPECIAL OR CONSEQUENTIAL DAMAGES RELATED TO OR ARISING OUT OF ANY USE OF, ACCESS TO, OR INABILITY TO ACCESS THIS WEBSITE, CONTENT, SERVICES, OR OF ANY OTHER LINKED WEBSITE OR EXTERNAL RESOURCE INCLUDING, WITHOUT LIMITATION, ANY LOST PROFITS, LOST SALES, LOST REVENUE, LOSS OF GOODWILL, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA EVEN IF OPEN TECHNOLOGIES INC. IS EXPRESSLY ADVISED OR AWARE OF THE POSSIBILITY OF SUCH DAMAGES OR LOSSES. YOU ASSUME ALL RISK FOR ANY DAMAGE TO YOUR COMPUTER SYSTEM OR LOSS OF DATA THAT RESULTS FROM OBTAINING ANY CONTENT FROM THE WEBSITE, INCLUDING ANY DAMAGES RESULTING FROM COMPUTER VIRUSES, WORMS, OR OTHER ITEMS OF A DESTRUCTIVE NATURE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, SO THE ABOVE LIMITATION MAY NOT APPLY TO YOU.