#!/bin/bash
# Author: Alan J. Pippin
-# Description: This script will attempt to scrub each zfs pool
-# given to it in the for loop. This script ensures
-# That only 1 scrub operation is running at any given time.
-# This serializes the zfs scrub process for each pool.
+# Description: This script will attempt to scrub a given pool.
+# This script ensures that only 1 scrub operation is
+# running at any given time. This serializes the zfs
+# scrub process for any pool.
-PATH=/usr/sbin:/sbin:$PATH
+# source our configuration
+config="${0%/*}/zfs-scripts.conf"
+[ -e "${config}.dist" ] && . ${config}.dist
+[ -e "${config}" ] && . ${config}
+exec >> $logdir/zfs-scrub.log 2>&1
+
+pools="$*"
maxsleeptime=360
-logfile=/var/log/zfs/zfs-scrub.log
-for i in tank storage
+if [ -z "$pools" ]; then
+ echo "-E- Usage: $0 <pools>"
+ exit 1
+fi
+
+fatal_and_exit() {
+ # echo message to terminal
+ echo -e 2>&1 "$1"
+ # send email notification
+ echo -e "$1" | $mailx -s "zfs scrub on $hostname failed" "$mailto"
+ exit 1
+}
+
+for i in $pools
do
- # Check to see if this zfs filesystem has a scrub being performed on it now.
+ # Import the local pool if needed and option was given to do so, else error out
+ zpool list -H "$i" >/dev/null 2>&1
+ if [ $? != 0 ]; then
+ if [[ $import_export_backup_pool == 1 ]] && [[ "$backup_pool" =~ "$i" ]]; then
+ zpool import $i
+ if [ $? != 0 ]; then
+ fatal_and_exit "-E- unable to import the pool $i"
+ fi
+ else
+ fatal_and_exit "-E- The local pool, '$i' doesn't seem to exist."
+ fi
+ fi
+
+ # Check to see if any zfs filesystem has a scrub being performed on it now.
# If it does, we cannot perform more than one scrub operation at a time.
while true; do
- zpool status | grep scrub: | grep "in progress" > /dev/null 2>&1
+ zpool status | grep scan: | grep "in progress" > /dev/null 2>&1
if [ $? == 0 ]; then
# Another zpool scrub operation is already running
# Wait until it is done before continuing
done
date=`date`
- echo "$date: Scrub started for zfs pool $i" >> $logfile
+ echo "$date: Scrub started for zfs pool $i"
zpool scrub $i
- sleep 60
+
+ # Wait until the scrub completes, and check for any errors
+ while true; do
+ zpool status $i 2>/dev/null | grep scan: | grep "in progress" > /dev/null 2>&1
+ if [ $? == 0 ]; then
+ # Our zpool scrub operation is still running
+ # Wait until it is done before continuing
+ ransleep=$(($RANDOM % $maxsleeptime))
+ sleep $ransleep
+ else
+ # Our scrub operation has completed
+ break
+ fi
+ done
+
+ date=`date`
+ echo "$date: Scrub completed for zfs pool $i"
+
+ # Import the local pool if needed and option was given to do so, else error out
+ # do this so we can check its status to see if it repaired any errors or not
+ zpool list -H "$i" >/dev/null 2>&1
+ if [ $? != 0 ]; then
+ if [[ $import_export_backup_pool == 1 ]] && [[ "$backup_pool" =~ "$i" ]]; then
+ zpool import $i
+ if [ $? != 0 ]; then
+ fatal_and_exit "-E- unable to import the pool $i"
+ fi
+ else
+ fatal_and_exit "-E- The local pool, '$i' doesn't seem to exist."
+ fi
+ fi
+
+ # Check for any scrub errors
+ zpool status $i | grep scan:
+ zpool status $i | grep scan: | grep -e "with 0 errors" -e "in progress" > /dev/null 2>&1
+ if [ $? != 0 ]; then
+ # The scrub found errors
+ zpool status $i | $mailx -s "zpool scrub on $hostname $i found errors" "$mailto"
+ fi
+
+ # Export the local pool if told to do so
+ if [[ $import_export_backup_pool == 1 ]] && [[ "$backup_pool" =~ "$i" ]]; then
+ # Don't export the pool if there is a currently running zfs-replicate operation
+ ps -ef | grep -q "zfs-replicate" | grep -v grep
+ if [ $? != 0 ]; then
+ # Only export the pool if it is still imported
+ zpool list -H "$i" >/dev/null 2>&1
+ if [ $? == 0 ]; then
+ zpool export $i
+ if [ $? != 0 ]; then
+ echo "-E- unable to export the local pool $i"
+ zpool status $i | $mailx -s "zpool scrub on $hostname unable to export the local pool $i" "$mailto"
+ fi
+ fi
+ fi
+ fi
done
+