Scrub then shutdown script

Hello curious reader. With the help of ChatGPT, I have put together a script to run a scrub then shutdown.

My backup server lives as cold storage – spends most of its life unplugged (I even unplug the network cable). This is my last-chance backup locally before I need to get my offsite hard drive or fire up S3 Glacier (credit card at the ready).

At times, I want to run a scrub but I would need to check in and see what’s happening. This script will run a scrub, monitor it, then shutdown after outputting a zpool status to a network share as a log file.

#!/bin/bash

# Start scrub
sudo zpool scrub rpool

# Check every 10 minutes (600 seconds)
while sudo zpool status rpool | grep -q "scrub in progress"; do
    sleep 600
done

# Save final status
sudo zpool status > "/mnt/XXXX/$(date '+%Y-%m-%d-%H%M')_status.log"

# Shut down after scrub is done
sudo shutdown -h now

Make file execuable:

sudo chmod +x ~/bin/scrubshutdown.sh

I use at command to run in the background. In this example, at 9am. Make sure you run as “sudo su” to allow scrub.

at 09:00 -f /home/karl/bin/shutdownscrub.sh

Note that zpool scrub has an option to wait until complete. Then your script gets much simpler, no polling required, just

zpool scrub -w pool && shutdown -h now

4 Likes

Fair point, which I have used before, but does not always work with scripting. It skipped and shutdown PC straight away. This method you know what will happen, plus it can be built upon to add additional features.