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