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 ==="```
you are viewing a single comment's thread
view the rest of the comments
[–] 6 points 3 hours ago* (last edited 3 hours ago) (4 children)

Trim can be handled by the system without a dedicated script, either as contunues trim (by just mounting it with the discard option) 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.)

Long 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).

Edit: And to keep this whole thing more constructive, here my setup for scrubbing as an example

background.slice:
[Slice]
CPUQuote=20%
IOReadBandwithMax=10M

notification_mail@.sevice:
[Unit] Description=Send Systemd Notification to eMail

[Service]
Type=oneshot
ExecStart=/sbin/sh -c 'echo "Subject: Service %i failed\n\nService %i failed.\n$(systemctl status %i)" | /sbin/msmtp <mail.address>'

[Install]
WantedBy=multi-user.target

monthly_scrub.service:
[Unit]
Description=Monthly BTRFS Scrub
OnFailure=notification_mail@%i.service

[Service]
Slice=background.slice
Type=simple
ExecStart=/sbin/btrfs scrub start -RBd / >> /var/log/monthly_scrub.log

monthly_scrub.timer;
[Unit]
Description=Monthly BTRFS scrub timer

[Timer]
OnCalendar=monthly
RandomizedDelaySec=1day
Persistent=true

[Install]
WantedBy=timers.target

  • source
  • hideshow 4 child comments
  • [–] [S] 2 points 3 hours ago (2 children)

    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!

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 hour ago

    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.

  • source
  • parent
  • [–] 3 points 3 hours ago*

    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.

  • source
  • parent