Added new script modified from the FreeBSD release to avoid kernel panics.
[zfs-nexenta/.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 logfile=/var/log/zfs-scrub.log
12
13 for i in backups
14 do
15   # Check to see if any zfs filesystem has a scrub being performed on it now.
16   # If it does, we cannot perform more than one scrub operation at a time.
17   while true; do
18     /sbin/zpool status | grep scrub: | grep "in progress" > /dev/null 2>&1
19     if [ $? == 0 ]; then
20         # Another zpool scrub operation is already running
21         # Wait until it is done before continuing
22         ransleep=$(($RANDOM % $maxsleeptime))
23         sleep $ransleep
24     else
25         # Another zpool scrub operation is not running
26         break
27     fi
28   done
29
30   date=`date`
31   echo "$date: Scrub started for zfs pool $i" >> $logfile
32   /usr/local/etc/bin/usb-drive-power on >> /dev/null
33   /sbin/zpool import $i
34   /sbin/zpool scrub $i
35
36   # Wait until the scrub completes, and check for any errors
37   while true; do
38     /sbin/zpool status $i | grep scrub: | grep "in progress" > /dev/null 2>&1
39     if [ $? == 0 ]; then
40         # Our zpool scrub operation is still running
41         # Wait until it is done before continuing
42         ransleep=$(($RANDOM % $maxsleeptime))
43         sleep $ransleep
44     else
45         # Our scrub operation has completed
46         break
47     fi
48   done
49
50   date=`date`
51   echo "$date: Scrub completed for zfs pool $i" >> $logfile
52
53   # Check for any scrub errors
54   /sbin/zpool status $i | grep scrub: | grep "with 0 errors" > /dev/null 2>&1
55   if [ $? != 0 ]; then
56     # The scrub found errors
57     /sbin/zpool status $i | /usr/bin/mailx -s "zpool scrub $i found errors" $mailto 
58   fi 
59
60   # Power off the USB drive
61   /sbin/zpool export $i
62   /usr/local/etc/bin/usb-drive-power off >> /dev/null
63
64 done
65