Skip to content

Instantly share code, notes, and snippets.

@bmarwell
Forked from gbrks/btrfs-scrub
Last active December 20, 2019 23:08
Show Gist options
  • Save bmarwell/fdf799bdb61f70a2dce089dba94f838c to your computer and use it in GitHub Desktop.
Save bmarwell/fdf799bdb61f70a2dce089dba94f838c to your computer and use it in GitHub Desktop.
btrfs scrub: script and systemd timers to run weekly and send mails using gmail via curl + netrc
machine smtp.gmail.com
login user@gmail.com
password my-app-password

installing systemd services + timers

systemctl daemon-reload systemctl enable btrfs-scrub.service systemctl enable btrfs-scrub.timer systemctl start btrfs-scrub.timer

chmod 600 /root/.netrc

apt-get install curl (if you are inside a in container)

#! /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>"
LOG_FILE="/var/log/btrfs-scrub.log"
TMP_OUTPUT="/tmp/btrfs-scrub.$$.out"
# redirect all stdout to log gile
exec >> $LOG_FILE
echo "[`date -Iseconds`] btrfs scrub job started."
# mail header to the file
echo "From: "My Name" <$EMAIL_ADDRESS>" > $TMP_OUTPUT
echo "To: "My Name" <$EMAIL_ADDRESS>" >> $TMP_OUTPUT
echo "Subject: $EMAIL_SUBJECT_PREFIX Scrub Job Completed" >> $TMP_OUTPUT
echo "" >> "$TMP_OUTPUT"
# timestamp the job
echo "[`date -Iseconds`] btrfs scrub job started."
echo "btrfs scrub job started on `date -Iseconds`" >> $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 -Iseconds`] 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 -Iseconds`" >> $TMP_OUTPUT
curl \
--netrc --ssl-reqd \
--mail-from "<$EMAIL_ADDRESS>" \
--mail-rcpt "<$EMAIL_ADDRESS>" \
--url smtps://smtp.gmail.com:465 \
-T "$TMP_OUTPUT" \
&& rm "$TMP_OUTPUT" || true
echo "[`date -Iseconds`] Scrub job ended."
exit 0;
[Unit]
Description=btrfs scrub
[Service]
User=root
Type=simple
ExecStart=/usr/local/bin/btrfs-scrub
[Unit]
Description=Runs btrfs scrub on all discs monthly
[Timer]
OnCalendar=weekly
[Install]
WantedBy=timers.target
@bmarwell
Copy link
Author

bmarwell commented Oct 30, 2017

Change to use:

  • iso timestamps
  • send mails using gmail via curl + netrc
  • use root account for that matter.
  • weekly checks

@ceremcem
Copy link

My improved version is here: https://github.com/ceremcem/monitor-btrfs-disk

@bmarwell
Copy link
Author

Thanks for letting me know!

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