Skip to content

Instantly share code, notes, and snippets.

@gbrks
Last active September 10, 2021 21:39
Show Gist options
  • Save gbrks/11b9d68d19394a265d70 to your computer and use it in GitHub Desktop.
Save gbrks/11b9d68d19394a265d70 to your computer and use it in GitHub Desktop.
btrfs scrub: script and systemd timers to run monthly
#! /bin/bash
#######################################################
# This is a helper script to be used in a systemd timer
# or cron job to scrub all mounted btrfs filessytems
#
# $Author: gbrks
# $Revision 0.1
# $Date: 2015.05.15
#
# Update email address below
EMAIL_SUBJECT_PREFIX="$HOSTNAME BTRFS - "
EMAIL_ADDRESS="<email@address>"
#Mailgun API
MG_API="api:key-<apikey>"
MG_DOMAIN="<domain>"
MG_FROM="<email_from>"
LOG_FILE="/var/log/btrfs-scrub.log"
TMP_OUTPUT="/tmp/btrfs-scrub.out"
# redirect all stdout to log gile
exec >> $LOG_FILE
# timestamp the job
echo "[`date`] btrfs scrub job started."
echo "btrfs scrub job started on `date`" > $TMP_OUTPUT
echo "----------------------------------------" >> $TMP_OUTPUT
# for each btrfs type system mounted, scrub and record output
while read d m t x
do
[[ $t != "btrfs" ]] && continue
echo "scrubbing $m" >> $TMP_OUTPUT
echo "[`date`] scrubbing $m"
btrfs scrub start -Bd $m >> $TMP_OUTPUT
echo "" >> $TMP_OUTPUT
done </proc/mounts
echo "----------------------------------------" >> $TMP_OUTPUT
echo "btrfs scrub job finished on `date`" >> $TMP_OUTPUT
curl -s --user "$MG_API" \
https://api.mailgun.net/v3/$MG_DOMAIN/messages \
-F from="$MG_FROM" \
-F to="$EMAIL_ADDRESS" \
-F subject="$EMAIL_SUBJECT_PREFIX Scrub Job Completed" \
-F text="$(cat $TMP_OUTPUT)" \
echo "[`date`] Scrub job ended."
exit 0;
[Unit]
Description=btrfs scrub
[Service]
Type=simple
ExecStart=/usr/local/bin/btrfs-scrub
[Unit]
Description=Runs btrfs scrub on all discs monthly
[Timer]
OnCalendar=monthly
[Install]
WantedBy=timers.target
@bmarwell
Copy link

Shouldn't the service be "oneshot"? Other than that: Thanks a lot!

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