maintenance.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/bin/bash
  2. # Make sure we're running as root or with sudo
  3. if [ "$(id -u)" -ne 0 ]; then
  4. gum style --foreground 196 --bold "This script must be run as root or with sudo"
  5. exit 1
  6. fi
  7. # Check if gum is installed
  8. if ! command -v gum &>/dev/null; then
  9. echo "This script uses gum for a nice interface."
  10. echo "Please install gum first: pacman -S gum"
  11. exit 1
  12. fi
  13. # Function to print section headers
  14. print_section() {
  15. gum style --border normal --border-foreground 39 --margin "1" --padding "0 2" --bold "$1"
  16. }
  17. # Function to print tasks
  18. print_task() {
  19. echo "→ $1..."
  20. }
  21. # Function to print success messages
  22. print_success() {
  23. gum style --foreground 46 "✓ $1"
  24. }
  25. # Function to print warning messages
  26. print_warning() {
  27. gum style --foreground 226 "⚠ $1"
  28. }
  29. # Starting message
  30. # Starting message
  31. echo "=== Arch Linux System Maintenance ==="
  32. # Update the system
  33. print_section "Updating System"
  34. # Sync package database
  35. print_task "Synchronizing package databases"
  36. pacman -Sy
  37. print_success "Package databases synchronized"
  38. # Clean package caches
  39. print_section "Cleaning Package Caches"
  40. # Clean pacman cache
  41. print_task "Cleaning pacman cache"
  42. pacman -Sc --noconfirm
  43. print_success "Pacman cache cleaned"
  44. # Clean AUR cache if paru is installed
  45. if command -v paru &>/dev/null; then
  46. print_task "Cleaning paru cache"
  47. paru -Sc --noconfirm
  48. print_success "Paru cache cleaned"
  49. else
  50. print_warning "paru not found. Skipping AUR cache cleanup."
  51. fi
  52. # Remove orphaned packages
  53. print_section "Removing Orphaned Packages"
  54. print_task "Finding orphaned packages"
  55. ORPHANS=$(pacman -Qtdq)
  56. if [ -n "$ORPHANS" ]; then
  57. echo "Found orphaned packages:"
  58. echo "$ORPHANS"
  59. print_task "Removing orphaned packages"
  60. pacman -Rns $(pacman -Qtdq) --noconfirm
  61. print_success "Orphaned packages removed"
  62. else
  63. print_success "No orphaned packages found"
  64. fi
  65. # # Clean user cache directory
  66. # print_section "Cleaning User Cache Directory"
  67. #
  68. # # Get current user's username
  69. # CURRENT_USER=$(logname 2>/dev/null || echo $SUDO_USER)
  70. #
  71. # # If we still don't have a username, try to get it from /home
  72. # if [ -z "$CURRENT_USER" ]; then
  73. # CURRENT_USER=$(ls -la /home | grep -v "\.\." | grep -v "total" | awk '{print $9}' | head -1)
  74. # fi
  75. #
  76. # echo "Cleaning cache for user: $CURRENT_USER"
  77. #
  78. # # Create a backup directory
  79. # BACKUP_DIR="/home/$CURRENT_USER/.cache_backup_$(date +%Y%m%d)"
  80. # mkdir -p "$BACKUP_DIR"
  81. # chown $CURRENT_USER:$CURRENT_USER "$BACKUP_DIR"
  82. #
  83. # # User cache directory
  84. # USER_CACHE="/home/$CURRENT_USER/.cache"
  85. #
  86. # if [ -d "$USER_CACHE" ]; then
  87. # # Firefox cache
  88. # if [ -d "$USER_CACHE/mozilla" ]; then
  89. # print_task "Backing up Firefox cache metadata"
  90. # cp -r "$USER_CACHE/mozilla" "$BACKUP_DIR/"
  91. # print_task "Cleaning Firefox cache"
  92. # find "$USER_CACHE/mozilla" -type f -name "*.sqlite" -exec sqlite3 {} "VACUUM;" \;
  93. # fi
  94. #
  95. # # Chrome/Chromium cache
  96. # for chrome_dir in "$USER_CACHE/google-chrome" "$USER_CACHE/chromium"; do
  97. # if [ -d "$chrome_dir" ]; then
  98. # print_task "Backing up Chrome/Chromium cache metadata"
  99. # mkdir -p "$BACKUP_DIR/$(basename $chrome_dir)"
  100. # cp -r "$chrome_dir/Default/Preferences" "$BACKUP_DIR/$(basename $chrome_dir)/" 2>/dev/null
  101. # print_task "Cleaning Chrome/Chromium cache"
  102. # rm -rf "$chrome_dir/Default/Cache" "$chrome_dir/Default/Code Cache" 2>/dev/null
  103. # fi
  104. # done
  105. #
  106. # # Clean thumbnail cache
  107. # if [ -d "$USER_CACHE/thumbnails" ]; then
  108. # print_task "Cleaning thumbnail cache"
  109. # rm -rf "$USER_CACHE/thumbnails/*" 2>/dev/null
  110. # fi
  111. #
  112. # # General cache cleanup (but preserve important files)
  113. # print_task "Cleaning general cache files"
  114. # find "$USER_CACHE" -type f -atime +30 -not -path "*/mozilla/*" -not -path "*/chromium/*" -not -path "*/google-chrome/*" -delete 2>/dev/null
  115. #
  116. # print_success "Cache cleaned for user $CURRENT_USER"
  117. # else
  118. # print_warning "Cache directory not found for user $CURRENT_USER"
  119. # fi
  120. # Clean system journals
  121. print_section "Cleaning System Journals"
  122. print_task "Rotating and cleaning journal logs"
  123. journalctl --vacuum-time=2weeks
  124. print_success "Journal logs cleaned (kept last 2 weeks)"
  125. # Reset failed services
  126. print_section "Resetting Failed Systemd Units"
  127. print_task "Checking for failed systemd units"
  128. FAILED_UNITS=$(systemctl --failed --no-legend | awk '{print $1}')
  129. if [ -n "$FAILED_UNITS" ]; then
  130. echo "Failed units found:"
  131. echo "$FAILED_UNITS"
  132. fi
  133. print_task "Clearing failed systemd units"
  134. systemctl reset-failed
  135. print_success "Failed systemd units cleared"
  136. # Clean temporary files
  137. print_section "Cleaning Temporary Files"
  138. print_task "Removing temporary files"
  139. rm -rf /tmp/* /var/tmp/* 2>/dev/null
  140. print_success "Temporary files removed"
  141. # Update file database
  142. print_section "Updating File Database"
  143. if command -v updatedb &>/dev/null; then
  144. print_task "Updating file database for locate command"
  145. updatedb
  146. print_success "File database updated"
  147. else
  148. print_warning "updatedb not found. Skipping file database update."
  149. fi
  150. # Check for and install security updates
  151. print_section "Security Updates Check"
  152. print_task "Checking for security updates"
  153. SECURITY_UPDATES=$(pacman -Qu | grep -i "security")
  154. if [ -n "$SECURITY_UPDATES" ]; then
  155. echo "Security updates available:"
  156. echo "$SECURITY_UPDATES"
  157. print_task "Installing security updates"
  158. pacman -S --needed $(echo "$SECURITY_UPDATES" | awk '{print $1}') --noconfirm
  159. print_success "Security updates installed"
  160. else
  161. print_success "No security updates needed"
  162. fi
  163. # Check for pacman database errors
  164. print_section "Checking Pacman Database"
  165. print_task "Verifying package database integrity"
  166. if command -v paccheck &>/dev/null; then
  167. paccheck --md5sum --quiet
  168. print_success "Pacman database check complete"
  169. else
  170. print_warning "paccheck not found. Consider installing pacutils package."
  171. fi
  172. # Cleanup pacnew/pacsave files
  173. print_section "Configuration File Management"
  174. print_task "Checking for .pacnew and .pacsave files"
  175. PACFILES=$(find /etc -name "*.pacnew" -o -name "*.pacsave" 2>/dev/null)
  176. if [ -n "$PACFILES" ]; then
  177. echo "Found the following .pacnew/.pacsave files:"
  178. echo "$PACFILES"
  179. print_warning "You may want to merge these configuration files"
  180. echo "Use 'pacdiff' to help manage these files (install pacdiff from pacman-contrib)"
  181. else
  182. print_success "No .pacnew or .pacsave files found"
  183. fi
  184. # System status
  185. print_section "System Status"
  186. # Disk usage with duf
  187. print_task "Checking disk usage"
  188. if command -v dysk &>/dev/null; then
  189. dysk
  190. else
  191. print_warning "duf not found. Using df instead. Consider installing duf for better disk usage display."
  192. df -h | grep -v "tmpfs" | grep -v "udev"
  193. fi
  194. # Memory usage
  195. echo "Memory usage:"
  196. free -h
  197. # Find large files
  198. echo "Large files (>100MB) in your home directory:"
  199. find /home/$(logname 2>/dev/null || echo $SUDO_USER) -type f -size +100M -exec ls -lh {} \; 2>/dev/null | sort -k5hr | head -n 10
  200. # Final summary
  201. print_section "Maintenance Complete"
  202. echo "System maintenance tasks completed successfully"
  203. echo "Remember to periodically run additional manual maintenance tasks:"
  204. echo "- Check for broken symlinks: find /usr /etc -xtype l -print"
  205. echo "- Run a SMART disk check: smartctl -a /dev/sdX (install smartmontools)"
  206. echo "- Check systemd boot time: systemd-analyze"
  207. exit 0