fant0men

xfs_defrag.sh

Mar 17th, 2022 (edited)
1,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.07 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script is just meant to defrag all my XFS drives (except the root
  4. # partition).
  5.  
  6. set -eo pipefail
  7.  
  8. if [[ $EUID -ne 0 ]]; then
  9.     printf '\n%s\n\n' 'You need to be root to run this script!'
  10.     exit
  11. fi
  12.  
  13. date=$(date "+%F")
  14.  
  15. passes='60'
  16.  
  17. xfs_log="${HOME}/xfs_defrag_log-${date}_${RANDOM}.txt"
  18.  
  19. mapfile -t lines < <(mount -t xfs)
  20.  
  21. xfs_defrag () {
  22.     declare -a partitions
  23.  
  24.     for (( i = 0; i < ${#lines[@]}; i++ )); do
  25.         line="${lines[${i}]}"
  26.  
  27.         mapfile -d' ' -t line_parts <<<"$line"
  28.         line_parts[-1]="${line_parts[-1]%$'\n'}"
  29.  
  30.         if [[ ${line_parts[2]} == '/' ]]; then
  31.             continue
  32.         fi
  33.  
  34.         if [[ -b ${line_parts[0]} ]]; then
  35.             partitions+=("${line_parts[0]}")
  36.         fi
  37.     done
  38.  
  39.     for (( i = 0; i < ${#partitions[@]}; i++ )); do
  40.         partition="${partitions[${i}]}"
  41.  
  42.         printf '\n%s\n\n' "*** ${partition}"
  43.  
  44.         xfs_fsr -v -p "$passes" "$partition"
  45.     done
  46.  
  47.     for (( i = 0; i < ${#partitions[@]}; i++ )); do
  48.         partition="${partitions[${i}]}"
  49.  
  50.         printf '\n%s\n\n' "*** ${partition}"
  51.  
  52.         xfs_db -c frag -r "$partition"
  53.     done
  54. }
  55.  
  56. xfs_defrag | tee "$xfs_log"
  57.  
Advertisement
Add Comment
Please, Sign In to add comment