Skip to content

Instantly share code, notes, and snippets.

@morph027
Last active March 2, 2022 13:44
Show Gist options
  • Save morph027/b86d8bc374a104766dcf to your computer and use it in GitHub Desktop.
Save morph027/b86d8bc374a104766dcf to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

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