disk-maintenance 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/bash
  2. # search and destroy garbage files in root
  3. find / -type f -iname "thumbs.db" -exec rm {} \; 2> /dev/null &
  4. find / -type f -iname ".ds_store" -exec rm {} \; 2> /dev/null &
  5. find / -type f -name '._*' -exec rm -rf {} \; 2> /dev/null &
  6. # functions for asynchronous operations
  7. cleanup_ext4() {
  8. fstrim "$1"
  9. e4defrag "$1"
  10. }
  11. cleanup_btrfs() {
  12. fstrim "$1"
  13. btrfs balance start -dusage=1 "$1"
  14. btrfs balance start -musage=1 "$1"
  15. btrfs balance start -dusage=5 "$1"
  16. btrfs balance start -musage=5 "$1"
  17. btrfs balance start -dusage=10 "$1"
  18. btrfs balance start -musage=10 "$1"
  19. btrfs balance start -dusage=25 "$1"
  20. btrfs balance start -musage=25 "$1"
  21. btrfs balance start -dusage=50 "$1"
  22. btrfs balance start -musage=50 "$1"
  23. btrfs scrub start -qdB "$1"
  24. }
  25. # defragment and fstrim ext4 weekly
  26. for filesystem in $(mount -t ext4 | awk '{print $3}'); do
  27. cleanup_ext4 "$filesystem" &
  28. done
  29. # defragment and rebalance ssd-only (hdd are too slow) btrfs weekly
  30. for filesystem in $(mount -t btrfs | grep "ssd" | awk '{print $3}'); do
  31. cleanup_btrfs "$filesystem" &
  32. done
  33. # wait for jobs to complete
  34. for j in $(jobs -p); do
  35. wait $j
  36. done
  37. # cleanup journalctl
  38. journalctl --vacuum-time=10d