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