Just wanted to make sure and ask if this is good enough.
set -e
echo "=== fstrim ==="
fstrim -av
echo "=== BTRFS scrub status before ==="
btrfs scrub status /
echo "=== BTRFS scrub start ==="
btrfs scrub start -B /
echo "=== BTRFS scrub status after ==="
btrfs scrub status /
echo "=== BTRFS balance (50%) ==="
btrfs balance start -dusage=50 /
echo "=== Done ==="```


Trim can be handled by the system without a dedicated script, either as contunues trim (by just mounting it with the
discardoption) or periodically via already provided SystemD services and timers (both supported by btrfs). And I think the most common defaults would be doing it once a week or so.Scrubbing on the other hand once a week feels excessive to me. And I would definitely not look at the result only at the moment I start the next scrub. Also I like having such stuff happen in the background without noticing much, so again SystemD is your friend by providing you ways to do it on a timer while also limiting the amount of ressources used via slices and ways to only notify you on a failure.
And then there is balancing which I really do very rarely. If you don’t have the habit of littering your disk with dozens and dozens of snapshots until it’s getting full, there is not much need.
(And then there’re the SMART self-tests I would definitely not skip… a simple short one daily doesn’t hurt, a more extended full one once a week.)
Lonf story short. Even if I wanted to do exactly what you plan, I would not put it into one script, because of different intervals (also to spread things out over time).
btrfs is fantastic for being low-maintenance.
Thanks for taking the time to write this! Now I understand my system a bit better and now kinda don’t understand why would someone scare people with btrfs maintenance when it’s “automated” by default. Thanks again!
Btr is one of those things you kinda don’t need to be using unless you know exactly why you’re doing it for this reason. Because best practices and received wisdom are actively shifting (although the internet search change between COVID and now has given It the short end of the stick) it may be better to do something different instead.
Also as another user pointed out it’s likely not necessary to do anything to limit your ssd writes. Nand write failure manifests itself in the form of lack of recall when unpowered. More writes means a shorter time unpowered will surface a lack of recall. If the system is a server or other type of always on computer then the write failure will likely never happen, since the drive isn’t unpowered enough to matter.
Do be aware of read fatigue though, blocks which are read many times will return stranger values that the ssd’s controller recognizes with crc and retries. This manifests as slowness to the user. If something seems laggy or slow it’s a good idea to run a program called spinrite on its “lvl 3” mode, which reads every block on the drive and rewrites it. You can do similar things with programs like dd or crystal but I tend to point people at spinrite because it’s hard to fuck up and very consistent.
Probably because people were more scared when they started. Just like you still find a lot of information how to reduce write/read accesses to your SSD to not kill it quickly while in reality an average user nowadays wouldn’t be able to kill an SSD even if he tried.