Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- vol="Library" #zfs ZVOL name - I only have one. If you have multiple ZVOLs, you will have to modify this script.
- mount="/Library/Snapshot" #mountpoint for snapshots
- case $1 in
- list)
- if [ "$2" == "time" ] ; then
- zfs list -t snapshot -o name | grep "$vol"@Auto-H-
- elif [ "$2" == "day" ] ; then
- zfs list -t snapshot -o name | grep "$vol"@Auto-D-
- else
- zfs list -t snapshot -o name
- fi
- ;;
- mount)
- if [ "$2" == "time" ] ; then
- if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-H-$3` ] ; then
- mount -t zfs "$vol"@Auto-H-$3 "$mount"
- else
- echo "ZFS snapshot Auto-H-$3 on ZVOL $vol does not exist. Exiting."
- exit 2
- fi
- elif [ "$2" == "day" ] ; then
- if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-D-$3` ] ; then
- mount -t zfs "$vol"@Auto-D-$3 "$mount"
- else
- echo "ZFS snapshot Auto-H-$3 on ZVOL $vol does not exist. Exiting."
- exit 2
- fi
- elif [ "$2" == "latest" ; then
- l=`zfs list -t snapshot -o name | grep "$vol"@Auto-H- | wc -l`
- snap=`zfs list -t snapshot -o name | grep "$vol"@Auto-H- | sort | tail -n +$l`
- echo "No snapshot given, mounting most recent snapshot ($snap) at $mount."
- mount -t zfs "$snap" "$mount"
- else
- if [ `zfs list -t snapshot -o name | grep "$vol"@$2` ] ; then
- mount -t zfs "$vol"@$2 "$mount"
- else
- echo "ZFS snapshot $2 on ZVOL $vol does not exist. Exiting."
- exit 2
- fi
- fi
- ;;
- unmount)
- umount "$mount"
- ;;
- delete)
- if [ "$2" == "time" ] ; then
- if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-H-$3` ] ; then
- read -p "Are you sure you want to delete ZFS snapshot Auto-H-$3 from ZVOL $vol? (y/n) " -n 1 -r
- echo # (optional) move to a new line
- if [[ ! $REPLY =~ ^[Yy]$ ]]
- then
- echo "Operation canceled at user request. Exiting."
- exit 1
- fi
- zfs destroy -r "$vol"@Auto-H-$3
- else
- echo "ZFS snapshot Auto-H-$3 on ZVOL $vol does not exist. Exiting."
- exit 2
- fi
- elif [ "$2" == "day" ] ; then
- if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-D-$3` ] ; then
- read -p "Are you sure you want to delete ZFS snapshot Auto-D-$3 from ZVOL $vol? (y/n) " -n 1 -r
- echo # (optional) move to a new line
- if [[ ! $REPLY =~ ^[Yy]$ ]]
- then
- echo "Operation canceled at user request. Exiting."
- exit 1
- fi
- zfs destroy -r "$vol"@Auto-D-$3
- else
- echo "ZFS snapshot Auto-D-$3 on ZVOL $vol does not exist. Exiting."
- exit 2
- fi
- else
- if [ -n "$2" ] ; then
- if [ `zfs list -t snapshot -o name | grep "$vol"@$2` ] ; then
- read -p "Are you sure you want to delete ZFS snapshot $2 from ZVOL $vol? (y/n) " -n 1 -r
- echo # (optional) move to a new line
- if [[ ! $REPLY =~ ^[Yy]$ ]] ; then
- echo "Operation canceled at user request. Exiting."
- exit 1
- fi
- zfs destroy -r "$vol"@$2
- else
- echo "ZFS snapshot $2 on ZVOL $vol does not exist. Exiting."
- exit 2
- fi
- else
- echo "Invalid Syntax. Exiting."
- fi
- fi
- ;;
- rollback)
- if [ "$2" == "time" ] ; then
- if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-H-$3` ] ; then
- echo "Are you sure you want to roll back ZVOL $vol to ZFS snapshot Auto-H-$3?"
- read -p "WARNING: This will also delete all snapshots newer than Auto-H-$3 (y/n) " -n 1 -r
- echo # (optional) move to a new line
- if [[ ! $REPLY =~ ^[Yy]$ ]]
- then
- echo "Operation canceled at user request. Exiting."
- exit 1
- fi
- zfs rollback -r "$vol"@Auto-H-$3
- else
- echo "ZFS snapshot Auto-H-$3 on ZVOL $vol does not exist. Exiting."
- exit 2
- fi
- elif [ "$2" == "day" ] ; then
- if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-D-$3` ] ; then
- echo "Are you sure you want to roll back ZVOL $vol to ZFS snapshot Auto-D-$3?"
- read -p "WARNING: This will also delete all snapshots newer than Auto-D-$3 (y/n) " -n 1 -r
- echo # (optional) move to a new line
- if [[ ! $REPLY =~ ^[Yy]$ ]]
- then
- echo "Operation canceled at user request. Exiting."
- exit 1
- fi
- zfs rollback -r "$vol"@Auto-D-$3
- else
- echo "ZFS snapshot Auto-D-$3 on ZVOL $vol does not exist. Exiting."
- exit 2
- fi
- else
- if [ -n "$2" ] ; then
- if [ `zfs list -t snapshot -o name | grep "$vol"@$2` ] ; then
- echo "Are you sure you want to roll back ZVOL $vol to ZFS snapshot $2?"
- read -p "WARNING: This will also delete all snapshots newer than $2 (y/n) " -n 1 -r
- echo # (optional) move to a new line
- if [[ ! $REPLY =~ ^[Yy]$ ]] ; then
- echo "Operation canceled at user request. Exiting."
- exit 1
- fi
- zfs rollback -r "$vol"@$2
- else
- echo "ZFS snapshot $2 on ZVOL $vol does not exist. Exiting."
- exit 2
- fi
- else
- echo "Invalid Syntax. Exiting."
- fi
- fi
- ;;
- *)
- zfs list -t snapshot -o name
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement