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