b82b90028a8b06accaae1e0e7dc5c5c8cb74583a
[zfs-nexenta/.git] / zfs-scrub
1 #!/bin/bash
2
3 # Author: Alan J. Pippin
4 # Description: This script will attempt to scrub each zfs pool
5 #              given to it in the for loop. This script ensures
6 #              That only 1 scrub operation is running at any given time.
7 #              This serializes the zfs scrub process for each pool.
8
9 PATH=/usr/sbin:/sbin:$PATH
10
11 pools="tank storage"
12 maxsleeptime=360
13 logfile=/var/log/zfs/zfs-scrub.log
14
15 for i in $pools
16 do
17   # Check to see if this 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   zpool scrub $i
35   sleep 60
36
37 done