[引用]zfs snapshot script

Amplify’d from andyleonard.com

OpenSolaris has ZFS Automatic Snapshots; FreeBSD, while it has ZFS, doesn’t have a comparable feature that I’m aware of. So I wrote my own, zfs-snapshot.sh:

#!/usr/local/bin/bash
 
# Path to ZFS executable:
ZFS=/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 number starting at 0; $max_snap is the highest numbered
# snapshot that will be kept.
max_snap=$(($COUNT -1))
 
# Clean up oldest snapshot:
if [ -d /${TARGET}/.zfs/snapshot/${SNAP}.${max_snap} ] ; then
    $ZFS destroy -r ${TARGET}@${SNAP}.${max_snap}
fi
 
# Rename existing snapshots:
dest=$max_snap
while [ $dest -gt 0 ] ; do
    src=$(($dest - 1))
    if [ -d /${TARGET}/.zfs/snapshot/${SNAP}.${src} ] ; then
    $ZFS rename -r ${TARGET}@${SNAP}.${src} ${TARGET}@${SNAP}.${dest}
    fi
    dest=$(($dest - 1))
done
 
# Create new snapshot:
$ZFS snapshot -r ${TARGET}@${SNAP}.0

From the command line, call the script something like the following:

./zfs-snapshot.sh tank weekly 5

This would take a recursive snapshot of the “tank” zpool with the basename weekly, rotating through five snapshots with names “weekly.0″ through “weekly.4″. This allows you to implement a snapshot scheme approximately similar to NetApp’s hourly-daily-weekly scheme, if you like. Because my FreeBSD workstation isn’t on 24×7, I run hourly snapshots out of /etc/crontab:

# Automated ZFS backups (hourly):
0 * * * * root /root/bin/zfs-snapshot.sh tank hourly 25

And daily/weekly/monthly snapshots out of /usr/local/etc/anacrontab (from the sysutils/anacron port):

(Of course, this isn’t as cool as the Gnome-integrated Time Slider in OpenSolaris, but it scratches my itch sufficiently.)

Read more at andyleonard.com

 
See this Amp at http://bit.ly/gVMtlW

Leave a Reply

你的電子郵件位址並不會被公開。 必要欄位標記為 *