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. # Get current user's username
  68. CURRENT_USER=$(logname 2>/dev/null || echo $SUDO_USER)
  69. # If we still don't have a username, try to get it from /home
  70. if [ -z "$CURRENT_USER" ]; then
  71. CURRENT_USER=$(ls -la /home | grep -v "\.\." | grep -v "total" | awk '{print $9}' | head -1)
  72. fi
  73. echo "Cleaning cache for user: $CURRENT_USER"
  74. # Create a backup directory
  75. BACKUP_DIR="/home/$CURRENT_USER/.cache_backup_$(date +%Y%m%d)"
  76. mkdir -p "$BACKUP_DIR"
  77. chown $CURRENT_USER:$CURRENT_USER "$BACKUP_DIR"
  78. # User cache directory
  79. USER_CACHE="/home/$CURRENT_USER/.cache"
  80. if [ -d "$USER_CACHE" ]; then
  81. # Firefox cache
  82. if [ -d "$USER_CACHE/mozilla" ]; then
  83. print_task "Backing up Firefox cache metadata"
  84. cp -r "$USER_CACHE/mozilla" "$BACKUP_DIR/"
  85. print_task "Cleaning Firefox cache"
  86. find "$USER_CACHE/mozilla" -type f -name "*.sqlite" -exec sqlite3 {} "VACUUM;" \;
  87. fi
  88. # Chrome/Chromium cache
  89. for chrome_dir in "$USER_CACHE/google-chrome" "$USER_CACHE/chromium"; do
  90. if [ -d "$chrome_dir" ]; then
  91. print_task "Backing up Chrome/Chromium cache metadata"
  92. mkdir -p "$BACKUP_DIR/$(basename $chrome_dir)"
  93. cp -r "$chrome_dir/Default/Preferences" "$BACKUP_DIR/$(basename $chrome_dir)/" 2>/dev/null
  94. print_task "Cleaning Chrome/Chromium cache"
  95. rm -rf "$chrome_dir/Default/Cache" "$chrome_dir/Default/Code Cache" 2>/dev/null
  96. fi
  97. done
  98. # Clean thumbnail cache
  99. if [ -d "$USER_CACHE/thumbnails" ]; then
  100. print_task "Cleaning thumbnail cache"
  101. rm -rf "$USER_CACHE/thumbnails/*" 2>/dev/null
  102. fi
  103. # General cache cleanup (but preserve important files)
  104. print_task "Cleaning general cache files"
  105. find "$USER_CACHE" -type f -atime +30 -not -path "*/mozilla/*" -not -path "*/chromium/*" -not -path "*/google-chrome/*" -delete 2>/dev/null
  106. print_success "Cache cleaned for user $CURRENT_USER"
  107. else
  108. print_warning "Cache directory not found for user $CURRENT_USER"
  109. fi
  110. # Clean system journals
  111. print_section "Cleaning System Journals"
  112. print_task "Rotating and cleaning journal logs"
  113. journalctl --vacuum-time=2weeks
  114. print_success "Journal logs cleaned (kept last 2 weeks)"
  115. # Reset failed services
  116. print_section "Resetting Failed Systemd Units"
  117. print_task "Checking for failed systemd units"
  118. FAILED_UNITS=$(systemctl --failed --no-legend | awk '{print $1}')
  119. if [ -n "$FAILED_UNITS" ]; then
  120. echo "Failed units found:"
  121. echo "$FAILED_UNITS"
  122. fi
  123. print_task "Clearing failed systemd units"
  124. systemctl reset-failed
  125. print_success "Failed systemd units cleared"
  126. # Clean temporary files
  127. print_section "Cleaning Temporary Files"
  128. print_task "Removing temporary files"
  129. rm -rf /tmp/* /var/tmp/* 2>/dev/null
  130. print_success "Temporary files removed"
  131. # Update file database
  132. print_section "Updating File Database"
  133. if command -v updatedb &>/dev/null; then
  134. print_task "Updating file database for locate command"
  135. updatedb
  136. print_success "File database updated"
  137. else
  138. print_warning "updatedb not found. Skipping file database update."
  139. fi
  140. # Check for and install security updates
  141. print_section "Security Updates Check"
  142. print_task "Checking for security updates"
  143. SECURITY_UPDATES=$(pacman -Qu | grep -i "security")
  144. if [ -n "$SECURITY_UPDATES" ]; then
  145. echo "Security updates available:"
  146. echo "$SECURITY_UPDATES"
  147. print_task "Installing security updates"
  148. pacman -S --needed $(echo "$SECURITY_UPDATES" | awk '{print $1}') --noconfirm
  149. print_success "Security updates installed"
  150. else
  151. print_success "No security updates needed"
  152. fi
  153. # Check for pacman database errors
  154. print_section "Checking Pacman Database"
  155. print_task "Verifying package database integrity"
  156. if command -v paccheck &>/dev/null; then
  157. paccheck --md5sum --quiet
  158. print_success "Pacman database check complete"
  159. else
  160. print_warning "paccheck not found. Consider installing pacutils package."
  161. fi
  162. # Cleanup pacnew/pacsave files
  163. print_section "Configuration File Management"
  164. print_task "Checking for .pacnew and .pacsave files"
  165. PACFILES=$(find /etc -name "*.pacnew" -o -name "*.pacsave" 2>/dev/null)
  166. if [ -n "$PACFILES" ]; then
  167. echo "Found the following .pacnew/.pacsave files:"
  168. echo "$PACFILES"
  169. print_warning "You may want to merge these configuration files"
  170. echo "Use 'pacdiff' to help manage these files (install pacdiff from pacman-contrib)"
  171. else
  172. print_success "No .pacnew or .pacsave files found"
  173. fi
  174. # System status
  175. print_section "System Status"
  176. # Disk usage with duf
  177. print_task "Checking disk usage"
  178. if command -v duf &>/dev/null; then
  179. duf --only local
  180. else
  181. print_warning "duf not found. Using df instead. Consider installing duf for better disk usage display."
  182. df -h | grep -v "tmpfs" | grep -v "udev"
  183. fi
  184. # Memory usage
  185. echo "Memory usage:"
  186. free -h
  187. # Find large files
  188. echo "Large files (>100MB) in your home directory:"
  189. find /home/$(logname 2>/dev/null || echo $SUDO_USER) -type f -size +100M -exec ls -lh {} \; 2>/dev/null | sort -k5hr | head -n 10
  190. # Final summary
  191. print_section "Maintenance Complete"
  192. echo "System maintenance tasks completed successfully"
  193. echo "Remember to periodically run additional manual maintenance tasks:"
  194. echo "- Check for broken symlinks: find /usr /etc -xtype l -print"
  195. echo "- Run a SMART disk check: smartctl -a /dev/sdX (install smartmontools)"
  196. echo "- Check systemd boot time: systemd-analyze"
  197. exit 0