Added date filter capability when parsing snapshot total logs.
[zfs-ubuntu/.git] / zfs-scrub-usb-drive
1 #!/bin/bash
2
3 # Author: Alan J. Pippin
4 # Description: This script will attempt to scrub a given USB drive
5 #              This script ensures that only 1 scrub operation is 
6 #              running at any given time. This serializes the zfs 
7 #              scrub process for any pool.
8
9 maxsleeptime=360
10 mailto=root
11
12 for i in backups
13 do
14   # Check to see if any zfs filesystem has a scrub being performed on it now.
15   # If it does, we cannot perform more than one scrub operation at a time.
16   while true; do
17     /sbin/zpool status | grep scrub: | grep "in progress" > /dev/null 2>&1
18     if [ $? == 0 ]; then
19         # Another zpool scrub operation is already running
20         # Wait until it is done before continuing
21         ransleep=$(($RANDOM % $maxsleeptime))
22         sleep $ransleep
23     else
24         # Another zpool scrub operation is not running
25         break
26     fi
27   done
28
29   echo "Scrubing zfs pool $i"
30   /usr/local/etc/bin/usb-drive-power on
31   /sbin/zpool import $i
32   /sbin/zpool scrub $i
33
34   # Wait until the scrub completes, and check for any errors
35   while true; do
36     /sbin/zpool status $i | grep scrub: | grep "in progress" > /dev/null 2>&1
37     if [ $? == 0 ]; then
38         # Our zpool scrub operation is still running
39         # Wait until it is done before continuing
40         ransleep=$(($RANDOM % $maxsleeptime))
41         sleep $ransleep
42     else
43         # Our scrub operation has completed
44         break
45     fi
46   done
47
48   # Check for any scrub errors
49   /sbin/zpool status $i | grep scrub: | grep "with 0 errors" > /dev/null 2>&1
50   if [ $? != 0 ]; then
51     # The scrub found errors
52     /sbin/zpool status $i | /usr/bin/mailx -s "zpool scrub $i found errors" $mailto 
53   fi 
54
55   # Power off the USB drive
56   /sbin/zpool export $i
57   /usr/local/etc/bin/usb-drive-power off
58
59 done
60