Skip to content

Instantly share code, notes, and snippets.

@bmarwell
Forked from gbrks/btrfs-scrub
Last active December 20, 2019 23:08

Revisions

  1. bmarwell revised this gist Oct 30, 2017. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    ## 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)
  2. bmarwell revised this gist Oct 30, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions .netrc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    machine smtp.gmail.com
    login user@gmail.com
    password my-app-password
  3. bmarwell revised this gist Oct 30, 2017. 3 changed files with 22 additions and 18 deletions.
    37 changes: 20 additions & 17 deletions btrfs-scrub
    Original file line number Diff line number Diff line change
    @@ -12,41 +12,44 @@
    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"
    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`] btrfs scrub job started."
    echo "btrfs scrub job started on `date`" > $TMP_OUTPUT
    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`] scrubbing $m"
    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`" >> $TMP_OUTPUT
    echo "btrfs scrub job finished on `date -Iseconds`" >> $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)" \
    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`] Scrub job ended."
    echo "[`date -Iseconds`] Scrub job ended."
    exit 0;
    1 change: 1 addition & 0 deletions btrfs-scrub.service
    Original file line number Diff line number Diff line change
    @@ -2,5 +2,6 @@
    Description=btrfs scrub

    [Service]
    User=root
    Type=simple
    ExecStart=/usr/local/bin/btrfs-scrub
    2 changes: 1 addition & 1 deletion btrfs-scrub.timer
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    Description=Runs btrfs scrub on all discs monthly

    [Timer]
    OnCalendar=monthly
    OnCalendar=weekly

    [Install]
    WantedBy=timers.target
  4. @gbrks gbrks revised this gist Aug 15, 2015. 2 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion btrfs-scrub.service
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,4 @@ Description=btrfs scrub

    [Service]
    Type=simple
    ExecStart=/root/scripts/btrfs-scrub.sh
    ExecStart=/usr/local/bin/btrfs-scrub
  5. @gbrks gbrks revised this gist Aug 1, 2015. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion btrfs-scrub.sh
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,12 @@

    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"
    @@ -33,7 +39,14 @@ done </proc/mounts

    echo "----------------------------------------" >> $TMP_OUTPUT
    echo "btrfs scrub job finished on `date`" >> $TMP_OUTPUT
    /usr/bin/mail -s "$EMAIL_SUBJECT_PREFIX Scrub Job Completed" "$EMAIL_ADDRESS" < $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;
  6. @gbrks gbrks revised this gist May 16, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions btrfs-scrub.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    [Unit]
    Description=btrfs scrub

    [Service]
    Type=simple
    ExecStart=/root/scripts/btrfs-scrub.sh
  7. @gbrks gbrks revised this gist May 16, 2015. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions btrfs-scrub.service
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    [Unit]
    Description=btrfs scrub

    [Service]
    Type=simple
    ExecStart=/root/scripts/btrfs-scrub.sh
  8. @gbrks gbrks revised this gist May 16, 2015. 2 changed files with 14 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions btrfs-scrub.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    [Unit]
    Description=btrfs scrub

    [Service]
    Type=simple
    ExecStart=/root/scripts/btrfs-scrub.sh
    8 changes: 8 additions & 0 deletions btrfs-scrub.timer
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    [Unit]
    Description=Runs btrfs scrub on all discs monthly

    [Timer]
    OnCalendar=monthly

    [Install]
    WantedBy=timers.target
  9. @gbrks gbrks created this gist May 16, 2015.
    39 changes: 39 additions & 0 deletions btrfs-scrub.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #! /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
    # 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
    /usr/bin/mail -s "$EMAIL_SUBJECT_PREFIX Scrub Job Completed" "$EMAIL_ADDRESS" < $TMP_OUTPUT

    echo "[`date`] Scrub job ended."
    exit 0;