Advertisement
crabbypup

ZFS snapshot management script

Apr 12th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.10 KB | None | 0 0
  1. #!/bin/bash
  2. vol="Library" #zfs ZVOL name - I only have one. If you have multiple ZVOLs, you will have to modify this script.
  3. mount="/Library/Snapshot" #mountpoint for snapshots
  4. case $1 in
  5.     list)
  6.         if [ "$2" == "time" ] ; then
  7.             zfs list -t snapshot -o name | grep "$vol"@Auto-H-
  8.  
  9.         elif [ "$2" == "day" ] ; then
  10.             zfs list -t snapshot -o name | grep "$vol"@Auto-D-
  11.  
  12.         else
  13.             zfs list -t snapshot -o name
  14.  
  15.         fi
  16.         ;;
  17.  
  18.     mount)
  19.         if [ "$2" == "time" ] ; then
  20.             if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-H-$3` ] ; then
  21.                 mount -t zfs "$vol"@Auto-H-$3 "$mount"
  22.             else
  23.                 echo "ZFS snapshot Auto-H-$3 on ZVOL $vol does not exist. Exiting."
  24.                             exit 2
  25.             fi
  26.  
  27.         elif [ "$2" == "day" ] ; then
  28.             if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-D-$3` ] ; then
  29.                 mount -t zfs "$vol"@Auto-D-$3 "$mount"
  30.             else
  31.                 echo "ZFS snapshot Auto-H-$3 on ZVOL $vol does not exist. Exiting."
  32.                                 exit 2
  33.                         fi
  34.  
  35.          elif [ "$2" == "latest"    ; then
  36.                         l=`zfs list -t snapshot -o name | grep "$vol"@Auto-H- | wc -l`
  37.                         snap=`zfs list -t snapshot -o name | grep "$vol"@Auto-H- | sort | tail -n +$l`
  38.                         echo "No snapshot given, mounting most recent snapshot ($snap) at $mount."
  39.                         mount -t zfs "$snap" "$mount"
  40.  
  41.                 else
  42.                         if [ `zfs list -t snapshot -o name | grep "$vol"@$2` ] ; then
  43.                                 mount -t zfs "$vol"@$2 "$mount"
  44.                         else
  45.                                 echo "ZFS snapshot $2 on ZVOL $vol does not exist. Exiting."
  46.                                 exit 2
  47.                         fi
  48.  
  49.         fi
  50.         ;;
  51.  
  52.     unmount)
  53.         umount "$mount"
  54.        
  55.         ;;
  56.  
  57.     delete)
  58.         if [ "$2" == "time" ] ; then
  59.             if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-H-$3` ] ; then
  60.                 read -p "Are you sure you want to delete ZFS snapshot Auto-H-$3 from ZVOL $vol? (y/n) " -n 1 -r
  61.                             echo    # (optional) move to a new line
  62.                             if [[ ! $REPLY =~ ^[Yy]$ ]]
  63.                             then
  64.                                     echo "Operation canceled at user request. Exiting."
  65.                                     exit 1
  66.                             fi
  67.                 zfs destroy -r "$vol"@Auto-H-$3
  68.             else
  69.                 echo "ZFS snapshot Auto-H-$3 on ZVOL $vol does not exist. Exiting."
  70.                 exit 2
  71.             fi
  72.  
  73.                 elif [ "$2" == "day" ] ; then
  74.             if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-D-$3` ] ; then
  75.                                 read -p "Are you sure you want to delete ZFS snapshot Auto-D-$3 from ZVOL $vol? (y/n) " -n 1 -r
  76.                                 echo    # (optional) move to a new line
  77.                                 if [[ ! $REPLY =~ ^[Yy]$ ]]
  78.                                 then
  79.                                         echo "Operation canceled at user request. Exiting."
  80.                                         exit 1
  81.                                 fi
  82.                                 zfs destroy -r "$vol"@Auto-D-$3
  83.                         else
  84.                                 echo "ZFS snapshot Auto-D-$3 on ZVOL $vol does not exist. Exiting."
  85.                                 exit 2
  86.                         fi
  87.  
  88.                 else
  89.             if [ -n "$2" ] ; then
  90.                 if [ `zfs list -t snapshot -o name | grep "$vol"@$2` ] ; then
  91.                                     read -p "Are you sure you want to delete ZFS snapshot $2 from ZVOL $vol? (y/n) " -n 1 -r
  92.                                     echo    # (optional) move to a new line
  93.                                     if [[ ! $REPLY =~ ^[Yy]$ ]] ; then
  94.                                             echo "Operation canceled at user request. Exiting."
  95.                                             exit 1
  96.                                     fi
  97.                                     zfs destroy -r "$vol"@$2
  98.                             else
  99.                                     echo "ZFS snapshot $2 on ZVOL $vol does not exist. Exiting."
  100.                                     exit 2
  101.                             fi
  102.             else
  103.                 echo "Invalid Syntax. Exiting."
  104.             fi
  105.  
  106.                 fi
  107.         ;;
  108.  
  109.         rollback)
  110.                 if [ "$2" == "time" ] ; then
  111.             if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-H-$3` ] ; then
  112.                 echo "Are you sure you want to roll back ZVOL $vol to ZFS snapshot Auto-H-$3?"
  113.                             read -p "WARNING: This will also delete all snapshots newer than Auto-H-$3 (y/n) " -n 1 -r
  114.                             echo    # (optional) move to a new line
  115.                             if [[ ! $REPLY =~ ^[Yy]$ ]]
  116.                             then
  117.                                     echo "Operation canceled at user request. Exiting."
  118.                                     exit 1
  119.                             fi
  120.                             zfs rollback -r "$vol"@Auto-H-$3
  121.             else
  122.                 echo "ZFS snapshot Auto-H-$3 on ZVOL $vol does not exist. Exiting."
  123.                                 exit 2
  124.             fi
  125.  
  126.                 elif [ "$2" == "day" ] ; then
  127.             if [ `zfs list -t snapshot -o name | grep "$vol"@Auto-D-$3` ] ; then
  128.                                 echo "Are you sure you want to roll back ZVOL $vol to ZFS snapshot Auto-D-$3?"
  129.                                 read -p "WARNING: This will also delete all snapshots newer than Auto-D-$3 (y/n) " -n 1 -r
  130.                                 echo    # (optional) move to a new line
  131.                                 if [[ ! $REPLY =~ ^[Yy]$ ]]
  132.                                 then
  133.                                         echo "Operation canceled at user request. Exiting."
  134.                                         exit 1
  135.                                 fi
  136.                                 zfs rollback -r "$vol"@Auto-D-$3
  137.                         else
  138.                                 echo "ZFS snapshot Auto-D-$3 on ZVOL $vol does not exist. Exiting."
  139.                                 exit 2
  140.                         fi
  141.  
  142.                 else
  143.                         if [ -n "$2" ] ; then
  144.                                 if [ `zfs list -t snapshot -o name | grep "$vol"@$2` ] ; then
  145.                     echo "Are you sure you want to roll back ZVOL $vol to  ZFS snapshot $2?"
  146.                                         read -p "WARNING: This will also delete all snapshots newer than $2 (y/n) " -n 1 -r
  147.                                         echo    # (optional) move to a new line
  148.                                         if [[ ! $REPLY =~ ^[Yy]$ ]] ; then
  149.                                                 echo "Operation canceled at user request. Exiting."
  150.                                                 exit 1
  151.                                         fi
  152.                                         zfs rollback -r "$vol"@$2
  153.                                 else
  154.                                         echo "ZFS snapshot $2 on ZVOL $vol does not exist. Exiting."
  155.                                         exit 2
  156.                                 fi
  157.                         else
  158.                                 echo "Invalid Syntax. Exiting."
  159.                         fi
  160.  
  161.                 fi
  162.                 ;;
  163.  
  164.  
  165.     *)
  166.         zfs list -t snapshot -o name
  167.         ;;
  168. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement