Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
scrub all zfs on linux pools sequentially (with option to exclude pools)
#!/bin/bash
##
## --exclude takes one pool or a comma seperated list of pools
##
## Example: scrub-all.sh --exclude rpool
## Example: scrub-all.sh --exclude rpool,tank-foo,tank-bar
##
EMAIL_RECIPIENT="foo@bar.com"
SCRIPT=${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}
ARGS=$(getopt -o e: -l "exclude:" -n "$SCRIPT" -- "$@");
eval set -- "$ARGS";
while true
do
case "$1" in
-e|--exclude)
shift
if [ -n "$1" ]; then
EXCLUDE=$1
shift
fi
;;
--)
shift
break
;;
*)
error "wrong/missing parameters"
exit 1
;;
esac
done
EXCLUDE_LIST="${EXCLUDE//,/\\|}"
ZPOOLS=( $(zpool list -H -o name | sed '/\('$EXCLUDE_LIST'\)/d') )
for ZPOOL in ${ZPOOLS[@]}
do
# start scrubbing
zpool scrub $ZPOOL
# wait till scrub is finished
while zpool status $ZPOOL | grep 'scan: *scrub in progress' > /dev/null
do
sleep 10
done
# send a report
zpool status $ZPOOL | mail -s "zpool status: $ZPOOL" $EMAIL_RECIPIENT
done
@poikilotherm

This comment has been minimized.

Copy link

@poikilotherm poikilotherm commented Feb 28, 2016

Hi,
I just stumbled over your script and gave it a try.

I found two issues with it:

  1. If you do not want to exclude something, the hole zpool list will be deleted by sed. (as /()/d matches and deletes all...)
    Maybe fix it with:
EXCLUDE_LIST="${EXCLUDE/,/\$\\|^}"
ZPOOLS=( $(zpool list -H -o name | sed "/\(^$EXCLUDE_LIST$\)/d") )
  1. it doesn't do proper parameter validation - there is no safety against wrong pool names. On the other hand: wrong pool name = intended exclude won't happen.

Just my 2 cents :-)
Cheers!

@morph027

This comment has been minimized.

Copy link
Owner Author

@morph027 morph027 commented Jul 1, 2016

Hm...if i pass nothing, i get a list of all my pools...

And you're right, at the moment, a non-existing pool obviously won't get scrubbed ;)

I ported things here.... https://github.com/morph027/zfs-cron-scrub-all-pools-sequentially

Will add these

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment