Guide - How to check BTRFS volume integrity using Monit

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Guide - How to check BTRFS volume integrity using Monit

Post by ben53252642 »

A Monit rule and script that does a read only BTRFS file system volume scrub (in this case the root volume / ) at a set time each day and generates a Monit alert if errors were detected.

Rule for /etc/monit/monitrc:

Code: Select all

# BTRFS File System Integrity
check program btrfsintegritycheck with path "/etc/monit/scripts/btrfsintegritycheck.sh"
 with timeout 1800 seconds
 # Run at 4am each day
 every "0 4 * * *"
 if status != 0 then alert
Script that goes in:
/etc/monit/scripts/btrfsintegritycheck.sh

Code: Select all

#!/bin/bash
btrfs scrub start -Br / | grep -e 'no errors found' -e 'with 0 errors'
This is an extremely useful tool for detecting for example corruption of an SD card.
Last edited by ben53252642 on Thursday 07 November 2019 13:06, edited 1 time in total.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Guide - How to check BTRFS volume integrity using Monit

Post by ben53252642 »

Wow! Monit / Mmonit just picked up a server with a corrupt BTRFS volume!

I investigated and sure enough, it was corrupt with unfixable errors, now restoring from a backup. 8-)

monit_btrfs.JPG
monit_btrfs.JPG (342.39 KiB) Viewed 884 times

Checking the server manually (output below):

btrfs2.JPG
btrfs2.JPG (111.72 KiB) Viewed 882 times
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Guide - How to check BTRFS volume integrity using Monit

Post by ben53252642 »

I really, really like BTRFS! :)

Using the below command (will probably vary a bit for different systems), I was able to get a list of files that had become corrupt on the BTRFS file system, delete them (which fixed the file system corruption), then restore just those files that had become corrupted from a backup.

journalctl --output cat | grep 'BTRFS' | grep '(path:' | sed -n -e 's/^.*\(path:\)/\1/p' | cut -f2- -d: | tr -d ")"

Note that I ran this command before the one above: btrfs scrub start -Bd /dev/sda2

Upon deleting the list of corrupt files....

btrfs5.JPG
btrfs5.JPG (57.81 KiB) Viewed 864 times
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest