fe94f81de80c4233556e25ab770a07264ad5dc47
[zfs-ubuntu/.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 # source our configuration
10 config="${0%/*}/zfs-scripts.conf"
11 [ -e "${config}.dist" ] && . ${config}.dist
12 [ -e "${config}" ] && . ${config}
13
14 exec >> $logdir/zfs-scrub.log 2>&1
15
16 pools="$*"
17 maxsleeptime=360
18
19 if [ -z "$pools" ]; then
20    echo "-E- Usage: $0 <pools>"
21    exit 1
22 fi
23
24 fatal_and_exit() {
25   # echo message to terminal
26   echo -e 2>&1 "$1"
27   # send email notification
28   echo -e "$1" | $mailx -s "zfs scrub on $hostname failed" "$mailto"
29   exit 1
30 }
31
32 for i in $pools
33 do
34   # Import the local pool if needed and option was given to do so, else error out
35   zpool list -H "$i" >/dev/null 2>&1
36   if [ $? != 0 ]; then
37     if [[ $import_export_backup_pool == 1 ]] && [[ "$i" =~ "$backup_pool" ]]; then
38       zpool import $i
39       if [ $? != 0 ]; then
40         fatal_and_exit "-E- unable to import the pool $i"
41       fi
42     else
43       fatal_and_exit "-E- The local pool, '$i' doesn't seem to exist."
44     fi
45   fi
46
47   # Check to see if any zfs filesystem has a scrub being performed on it now.
48   # If it does, we cannot perform more than one scrub operation at a time.
49   while true; do
50     zpool status | grep scan: | grep "in progress" > /dev/null 2>&1
51     if [ $? == 0 ]; then
52         # Another zpool scrub operation is already running
53         # Wait until it is done before continuing
54         ransleep=$(($RANDOM % $maxsleeptime))
55         sleep $ransleep
56     else
57         # Another zpool scrub operation is not running
58         break
59     fi
60   done
61
62   date=`date`
63   echo "$date: Scrub started for zfs pool $i"
64   zpool scrub $i
65
66   # Wait until the scrub completes, and check for any errors
67   while true; do
68     zpool status $i 2>/dev/null | grep scan: | grep "in progress" > /dev/null 2>&1
69     if [ $? == 0 ]; then
70         # Our zpool scrub operation is still running
71         # Wait until it is done before continuing
72         ransleep=$(($RANDOM % $maxsleeptime))
73         sleep $ransleep
74     else
75         # Our scrub operation has completed
76         break
77     fi
78   done
79
80   date=`date`
81   echo "$date: Scrub completed for zfs pool $i"
82
83   # Import the local pool if needed and option was given to do so, else error out
84   # do this so we can check its status to see if it repaired any errors or not
85   zpool list -H "$i" >/dev/null 2>&1
86   if [ $? != 0 ]; then
87     if [[ $import_export_backup_pool == 1 ]] && [[ "$i" =~ "$backup_pool" ]]; then
88       zpool import $i
89       if [ $? != 0 ]; then
90         fatal_and_exit "-E- unable to import the pool $i"
91       fi
92     else
93       fatal_and_exit "-E- The local pool, '$i' doesn't seem to exist."
94     fi
95   fi
96
97   # Check for any scrub errors
98   zpool status $i | grep scan: 
99   zpool status $i | grep scan: | grep "with 0 errors" > /dev/null 2>&1
100   if [ $? != 0 ]; then
101     # The scrub found errors
102     zpool status $i | $mailx -s "zpool scrub on $hostname $i found errors" "$mailto" 
103   fi 
104
105   # Export the local pool if told to do so
106   if [[ $import_export_backup_pool == 1 ]] && [[ "$i" =~ "$backup_pool" ]]; then
107     # Don't export the pool if there is a currently running zfs-replicate operation
108     ps -ef | grep -q "zfs-replicate" | grep -v grep
109     if [ $? != 0 ]; then
110       # Only export the pool if it is still imported
111       zpool list -H "$i" >/dev/null 2>&1
112       if [ $? == 0 ]; then
113         zpool export $i
114         if [ $? != 0 ]; then
115           echo "-E- unable to export the local pool $i"
116           zpool status $i | $mailx -s "zpool scrub on $hostname unable to export the local pool $i" "$mailto"
117         fi
118       fi
119     fi
120   fi
121
122 done
123