#!/bin/sh ## Snapshot zfs data directories and keep one ## week of snapshots. ## spk - 2012-09-17 - v1.0 - Basic script ## spk - 2012-09-17 - v1.1 - Added test for log file # Set up some variables LOG=/var/log/data_snap.log DATE=/usr/bin/date ECHO=/usr/bin/echo # Set up the log file if test ! -f $LOG; then touch $LOG else continue fi # Let people know we're starting... $ECHO "*************************************************" >> $LOG $ECHO "`$DATE` - Starting snapshot process" >> $LOG # Check if there is an existing 7-day old snapshot zfs list -H -t snapshot|grep 7daysago > /dev/null if test $? -eq 0; then $ECHO "`$DATE` - Deleting 7 day old snapshot" >> $LOG # for snap in `zfs list -H -o name -t snapshot|grep 7daysago`; do echo zfs destroy $snap >> $LOG && zfs destroy $snap; done zfs destroy -r data@7daysago if test $? -ne 0; then $ECHO "`$DATE` - Problem renaming 7 day old snapshot, exiting!" >> $LOG exit 1 else continue fi else $ECHO "`$DATE` - No 7daysago snapshot - not renaming" >> $LOG fi ## Check snapshots exist and rename them if they do... # Rename the 6 day old snapshot to 7 days old.. zfs list -H -t snapshot|grep 6daysago > /dev/null if test $? -eq 0; then $ECHO "`$DATE` - Renaming 6 day old snapshot" >> $LOG zfs rename -r data@6daysago data@7daysago if test $? -ne 0; then $ECHO "`$DATE` - Problem renaming 6 day old snapshot, exiting!" >> $LOG exit 1 else continue fi else $ECHO "`$DATE` - No 6daysago snapshot - not renaming" >> $LOG fi # Rename the 5 day old snapshot to 6 days old.. zfs list -H -t snapshot|grep 5daysago > /dev/null if test $? -eq 0; then $ECHO "`$DATE` - Renaming 5 day old snapshot" >> $LOG zfs rename -r data@5daysago data@6daysago if test $? -ne 0; then $ECHO "`$DATE` - Problem renaming 5 day old snapshot, exiting!" >> $LOG exit 1 else continue fi else $ECHO "`$DATE` - No 5daysago snapshot - not renaming" >> $LOG fi # Rename the 4 day old snapshot to 5 days old.. zfs list -H -t snapshot|grep 4daysago > /dev/null if test $? -eq 0; then $ECHO "`$DATE` - Renaming 4 day old snapshot" >> $LOG zfs rename -r data@4daysago data@5daysago if test $? -ne 0; then $ECHO "`$DATE` - Problem renaming 4 day old snapshot, exiting!" >> $LOG exit 1 else continue fi else $ECHO "`$DATE` - No 4daysago snapshot - not renaming" >> $LOG fi # Rename the 3 day old snapshot to 4 days old.. zfs list -H -t snapshot|grep 3daysago > /dev/null if test $? -eq 0; then $ECHO "`$DATE` - Renaming 4 day old snapshot" >> $LOG zfs rename -r data@3daysago data@4daysago if test $? -ne 0; then $ECHO "`$DATE` - Problem renaming 3 day old snapshot, exiting!" >> $LOG exit 1 else continue fi else $ECHO "`$DATE` - No 3daysago snapshot - not renaming" >> $LOG fi # Rename the 2 day old snapshot to 3 days old.. zfs list -H -t snapshot|grep 2daysago > /dev/null if test $? -eq 0; then $ECHO "`$DATE` - Renaming 2 day old snapshot" >> $LOG zfs rename -r data@2daysago data@3daysago if test $? -ne 0; then $ECHO "`$DATE` - Problem renaming 2 day old snapshot, exiting!" >> $LOG exit 1 else continue fi else $ECHO "`$DATE` - No 2daysago snapshot - not renaming" >> $LOG fi # Rename yesterday's snapshot to 2 days old.. zfs list -H -t snapshot|grep yesterday > /dev/null if test $? -eq 0; then $ECHO "`$DATE` - Renaming yesterday's snapshot" >> $LOG zfs rename -r data@yesterday data@2daysago if test $? -ne 0; then $ECHO "`$DATE` - Problem renaming yesterday's snapshot, exiting!" >> $LOG exit 1 else continue fi else $ECHO "`$DATE` - No snapshot from yesterday - not renaming" >> $LOG fi # Rename today's snapshot to yesterday's.. zfs list -H -t snapshot|grep today > /dev/null if test $? -eq 0; then $ECHO "`$DATE` - Renaming today's snapshot" >> $LOG zfs rename -r data@today data@yesterday if test $? -ne 0; then $ECHO "`$DATE` - Problem renaming today's snapshot, exiting!" >> $LOG exit 1 else continue fi else $ECHO "`$DATE` - No snapshot from today - not renaming" >> $LOG fi ## Finally, we take a new snapshot... zfs snapshot -r data@today if test $? -eq 0; then $ECHO "`$DATE` - New Snapshot taken successfully" >> $LOG else $ECHO "`$DATE` - Snapshot failed!" >> $LOG exit 2 fi # Let's finish the script.. $ECHO "`$DATE` - Snapshot creation finished!" >> $LOG $ECHO "****************************************************" >> $LOG