btrfs-maintenance/usr/local/bin/btrfs-maintenance

39 lines
878 B
Bash
Executable file

#!/bin/bash
set -e
# Find the mountpoint for all BTRFS formatted partitions
PARTITIONS=()
for i in $(lsblk -nrpo "name" -e 1,7,11); do
if [[ "$(lsblk -no FSTYPE "$i")" == "btrfs" ]]; then
PARTITIONS+=("$(findmnt -v -y --real -f -o TARGET -n $i)")
fi
done
# Remove duplicates, semi-jankily
PARTITIONS=($(echo ${PARTITIONS[@]} | tr ' ' '\n' | sort -u | tr '\n' ' '))
# Reallocate blocks that are less than 95% used
for i in ${PARTITIONS[@]}; do
{
echo "Starting balance on $i"
btrfs balance start -dusage=95 "$i"
echo "Finished balance on $i"
} &
done
# Wait for balance operations to finish
wait
# Scrub all partitions
for i in ${PARTITIONS[@]}; do
{
echo "Starting scrub on $i"
btrfs scrub start -B "$i"
echo "Finished scrub on $i"
} &
done
# Wait for scrub operations to finish
wait
exit 0