Last active
September 10, 2021 21:39
-
-
Save gbrks/11b9d68d19394a265d70 to your computer and use it in GitHub Desktop.
btrfs scrub: script and systemd timers to run monthly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=btrfs scrub | |
[Service] | |
Type=simple | |
ExecStart=/usr/local/bin/btrfs-scrub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Runs btrfs scrub on all discs monthly | |
[Timer] | |
OnCalendar=monthly | |
[Install] | |
WantedBy=timers.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't the service be "oneshot"? Other than that: Thanks a lot!