First version (not tested)

This commit is contained in:
Logan G 2023-02-24 21:55:28 -07:00
parent ebe620b702
commit 864fb09762
Signed by: logan
GPG key ID: E328528C921E7A7A
3 changed files with 49 additions and 0 deletions

View file

@ -0,0 +1,10 @@
[Unit]
Description=Run BTRFS maintenance (scrubs and rebalance)
[Service]
Type=oneshot
ExecStart=/usr/local/bin/btrfs-maintenance
RemainAfterExit=yes
[Install]
WantedBy=default.target

View file

@ -0,0 +1,9 @@
[Unit]
Description=Run BTRFS maintenance stuff (scrub and rebalance) monthly
[Timer]
OnCalendar=*-*-01 02:00:00
Persistent=true
[Install]
WantedBy=timers.target

30
usr/local/bin/btrfs-maintenance Executable file
View file

@ -0,0 +1,30 @@
#!/bin/bash
set -e
# Find all BTRFS formatted partitions
PARTITIONS=()
for i in $(lsblk -nrpo "name" -e 1,7,11); do
if [[ "$(lsblk -no FSTYPE "$i")" == "btrfs" ]]; then
PARTITIONS+=("$i")
fi
done
# Reallocate blocks that are less than 95% used
for i in ${PARTITIONS[*]}; do
echo "Balance: $i"
btrfs balance start -dusage=95 "$i" &
done
# Wait for balance operations to finish
wait
# Scrub all partitions
for i in ${PARTITIONS[*]}; do
echo "Scrub: $i"
btrfs scrub start -B "$i" &
done
# Wait for scrub operations to finish
wait
exit 0