fant0men

Reinstall broken packages (Fedora)

Oct 30th, 2019
139
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script either reinstalls every RPM package on the system, or verifies all RPMs to see which ones are broken, and reinstalls those.
  3. # Run with either '-full' or '-verify' as an argument.
  4.  
  5. date=$(date "+%F")
  6. dir='/home/lucifer'
  7. txt1="${dir}/dnf_all_rpms_${date}.txt"
  8. txt2="${dir}/rpm_verify_${date}.txt"
  9. txt3="${dir}/dnf_installed_rpms_done_${date}.txt"
  10.  
  11. # If the script isn't run with sudo / root privileges, then ask the user to type
  12. # his / her password, so we can run the script with full root privileges.
  13. # 'exec' is used in conjunction with 'sudo bash', thereby replacing the current
  14. # shell, and current instance of the script, with the new one that has full
  15. # privileges.
  16. if [[ $(whoami) != root ]]; then
  17.     exec sudo bash -c "$0"
  18. fi
  19.  
  20. function full {
  21.     dnf list --installed | sed -e "s/[[:space:]]\+/ /g" | cut -d' ' -f1 > "$txt1"
  22.     dnf_install "$txt1"
  23. }
  24.  
  25. function verify {
  26.     rpm -qf $(rpm -Va 2>&1 | grep -vE '^$|prelink:' | grep -vE '^$|error:' | sed 's|.* /|/|') | sort -u > "$txt2"
  27.     dnf_install "$txt2"
  28. }
  29.  
  30. function dnf_install {
  31.     regex='kernel|grub'
  32.  
  33.     txt="$1"
  34.  
  35.     touch "$txt3"
  36.  
  37.     cat "$txt" | while read rpm; do
  38.         if [[ ! $rpm =~ $regex ]]; then
  39.             dnf -y reinstall "$rpm"
  40.         fi
  41.  
  42.         if [[ $? -eq 0 ]]; then
  43.             echo "$rpm" >> "$txt3"
  44.         fi
  45.     done
  46. }
  47.  
  48. function usage {
  49.     bname=$(basename "$0")
  50.     echo -e "Usage: ${bname} -full | -verify\n"
  51.     exit
  52. }
  53.  
  54. if [[ -z $1 ]]; then
  55.     usage
  56. elif [[ $1 == '-full' ]]; then
  57.     full
  58. elif [[ $1 == '-verify' ]]; then
  59.     verify
  60. else
  61.     usage
  62. fi
RAW Paste Data