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.
9 # source our configuration
10 config="${0%/*}/zfs-scripts.conf"
11 [ -e "${config}.dist" ] && . ${config}.dist
12 [ -e "${config}" ] && . ${config}
14 exec >> $logdir/zfs-scrub.log 2>&1
19 if [ -z "$pools" ]; then
20 echo "-E- Usage: $0 <pools>"
25 # echo message to terminal
27 # send email notification
28 echo -e "$1" | $mailx -s "zfs scrub on $hostname failed" "$mailto"
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
37 if [[ $import_export_backup_pool == 1 ]] && [[ "$i" =~ "$backup_pool" ]]; then
40 fatal_and_exit "-E- unable to import the pool $i"
43 fatal_and_exit "-E- The local pool, '$i' doesn't seem to exist."
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.
50 zpool status | grep scan: | grep "in progress" > /dev/null 2>&1
52 # Another zpool scrub operation is already running
53 # Wait until it is done before continuing
54 ransleep=$(($RANDOM % $maxsleeptime))
57 # Another zpool scrub operation is not running
63 echo "$date: Scrub started for zfs pool $i"
66 # Wait until the scrub completes, and check for any errors
68 zpool status $i 2>/dev/null | grep scan: | grep "in progress" > /dev/null 2>&1
70 # Our zpool scrub operation is still running
71 # Wait until it is done before continuing
72 ransleep=$(($RANDOM % $maxsleeptime))
75 # Our scrub operation has completed
81 echo "$date: Scrub completed for zfs pool $i"
83 # Check for any scrub errors
84 zpool status $i | grep scan:
85 zpool status $i | grep scan: | grep "with 0 errors" > /dev/null 2>&1
87 # The scrub found errors
88 zpool status $i | $mailx -s "zpool scrub on $hostname $i found errors" "$mailto"
91 # Export the local pool if told to do so
92 if [[ $import_export_backup_pool == 1 ]] && [[ "$i" =~ "$backup_pool" ]]; then
95 echo "-E- unable to export the local pool $i"
96 zpool status $i | $mailx -s "zpool scrub on $hostname unable to export the local pool $i" "$mailto"