#!/bin/sh ############################################################################### # name: zfsdumpd.sh # # Script for dumping the filesystem for backup. Used in conjunection with # zfsrestored.sh (available on BigAdmin) # Read full description: # http://www.sun.com/bigadmin/content/submitted/zfsdumpd_zfsrestored.jsp ############################################################################### # You may change the default backup DIR in both zfsdumpd.sh and zfsrestored.sh DIR=/var/zfsbk Usage() { cat <<-EOF Usage: $1 [-d backup_dir] [-r] [-z zfs] -d = where to back up -r = used with -z; back up the descendents of a file system -z = file system to back up in format of tank/home EOF } GetBackupfileprefixFromSnapshot () { c=0 for str in `echo $1 | awk -F@ '{print $1}' | awk -F/ '{ for( i=1 ; i <= NF ; i++ ){print $i}}` do c=`expr $c + 1` if [ $c -eq 1 ] then file_prefix="$str" else file_prefix="${file_prefix}_$str" fi done echo $file_prefix } BackupSnapshots () { if [ "$1" = all ] then SNAPSHOTS=`zfs list -t snapshot -o name | grep -vw NAME` else if [ $recursive -eq 0 ] then SNAPSHOTS=`zfs list -t snapshot -o name -r "$1" | grep -vw NAME` else SNAPSHOTS=`zfs list -t snapshot -o name | grep "^${1}@"` fi fi for snapshot in $SNAPSHOTS do file_prefix=`GetBackupfileprefixFromSnapshot "$snapshot"` if find "$DIR" -name tmp -prune -o -print | grep "${file_prefix}\." >/dev/null 2>&1 then rm ${DIR}/${file_prefix}.* fi if zfs send "$snapshot" > ${DIR}/${file_prefix}.$mydate; then myzfs=`echo $snapshot | awk -F@ '{print $1}'` echo "$myzfs is backed up as ${DIR}/${file_prefix}.$mydate" fi done } Create_BackupSnapshots () { if [ $recursive -eq 0 ] then for snapshot in `zfs list -t snapshot -o name -r $zfs | grep -vw NAME` do zfs destroy $snapshot done zfs snapshot -r ${zfs}@$mydate else for snapshot in `zfs list -t snapshot -o name | grep "^${zfs}@"` do zfs destroy $snapshot done zfs snapshot ${zfs}@$mydate fi BackupSnapshots "$zfs" } Create_BackupSnapshotsAll () { for snapshot in `zfs list -t snapshot -o name | grep -vw NAME` do zfs destroy $snapshot done for pool in `zpool list -o name | grep -vw NAME` do zfs snapshot -r ${pool}@$mydate done BackupSnapshots all } ############################################################################### # main() ############################################################################### recursive=1 mydate=`date '+%Y%m%d'` while getopts d:rz: opt do case "$opt" in d) DIR="$OPTARG";; r) recursive=0;; z) zfs="$OPTARG";; \?) Usage $0 >&2; exit 2;; esac done [ $# -ne `expr $OPTIND - 1` ] && Usage $0 && exit 2 [ -d $DIR ] || mkdir -p $DIR if [ "X$zfs" != X ] then if zfs list -o name | grep -v @ | grep -w "$zfs" > /dev/null then Create_BackupSnapshots else echo File system $zfs does not exist. exit fi else Create_BackupSnapshotsAll fi ############################################################################## ### This script is submitted to BigAdmin by a user of the BigAdmin community. ### Sun Microsystems, Inc. is not responsible for the ### contents or the code enclosed. ### ### ### Copyright 2008 Sun Microsystems, Inc. ALL RIGHTS RESERVED ### Use of this software is authorized pursuant to the ### terms of the license found at ### http://www.sun.com/bigadmin/common/berkeley_license.html ##############################################################################