Advertisement
Guest User

ubuntu-cleanup.sh

a guest
Jan 20th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.49 KB | None | 0 0
  1. #!/bin/bash
  2. # This script requires package "apt-show-versions" that will be installed if not present.
  3. # This script asks user to remove packages that don't belong to main ubuntu repository.
  4. # In other words , seeks for packages installed from all PPA installed , and asks the user if want to remove or not.
  5. # with this script you can remove some or all packages of a given PPA , and also in later part of script will ask
  6. # user if wants to remove PPA configuration forever from system.
  7. # This script also will search for orfan packages, packages than went removed from main Ubuntu repository , but
  8. # somehow is still installed on system. the user will have the option to delete those.
  9. #don't forget to add valid values in configuration part of the script.these values can be obtained doing a
  10. #apt-cache policy <package> , being the selected package , one from the main ubuntu repositories.
  11. #the values pre-configured in the script should work well for most cases.
  12. #from my /etc/apt/sources.list , deb http://pt.archive.ubuntu.com/ubuntu/ wily universe , the default value used ;
  13. # archive.ubuntu.com will match most of the cases , using mirrors from any other country. just ubuntu.com/ubuntu
  14. #or ubuntu.com would be a safe string choice too.
  15. # feedback , bugs or new implementations , please email ubuntu-cleanup@outlook.pt
  16. #changelog
  17. # Version 1 - 15/1/2016
  18. # Version 1.1 - 17/1/2016 - added log notices - install script dependencies / delete packages/PPA
  19. # / apt-get update to /var/log/syslog
  20. # Version 1.2 - 18/1/2016 - improvement in function list_packages_and_prompt_user variable $extraction2 . was causing a bug in some cases. now fixed.
  21. # Version 1.3 - 18/1/2016 - start of the script and breaking with control-c now logs to syslog
  22. # Version 1.4 - 18/1/2016 - implemented session identification , for better control of logs. sessions id are saved ( /var/lib/ubuntu-cleanup-sessions )
  23. # and later compared against the generate_session_id function , to guarantee that sessions are unique in a universe of 2^64 values.
  24.  
  25. trap ctrl_c INT
  26.  
  27. function validate_userid_equal_0 {
  28. if [ "$(id -u)" != "0" ]; then
  29.     echo "This script uses superuser privileges therefore must be run with sudo" 1>&2
  30.     exit 1
  31. fi
  32. }
  33.  
  34. function ctrl_c() {
  35.         logger "ubuntu.cleanup.sh - Control-c pressed by user. script aborted - session id - $id"
  36.     exit 130
  37. }
  38.  
  39. function generate_session_id {
  40. if [ ! -e $ubuntucleanupsessions ] ; then
  41.     touch "$ubuntucleanupsessions"
  42. fi
  43. id=`shuf -i 1-18446744073709551615 -n1 --random-source=/dev/random`
  44. if grep -Fq $id "$ubuntucleanupsessions" ;
  45.     then
  46.         generate_session_id
  47. else
  48.         echo $id >> "$ubuntucleanupsessions"
  49. fi
  50.  
  51. }
  52.  
  53. function the_start_of_script_should_log_to_syslog {
  54. logger "ubuntu.cleanup.sh - script started - session id - $id"
  55. }
  56.  
  57. function install_script_requirements {
  58.  
  59. file1="/usr/bin/apt-show-versions"
  60.  
  61. if [ ! -f $file1 ] ; then
  62.     echo "requirements not met, installing : apt-show-versions"
  63.     sleep 2
  64.     apt-get install -y apt-show-versions && logger "ubuntu.cleanup.sh - Installed package apt-show-versions - session id - $id"
  65. fi
  66. }
  67.  
  68. function Yy {
  69.     read -p "Do you want to remove this package ? (y/n)" -n 1 -r && echo -e
  70.  
  71. if [[  $REPLY =~ ^[Yy]$ ]]
  72.     then
  73.         apt-get -y purge $i && logger "ubuntu.cleanup.sh - Remove package $i from system - belonged to $extraction2 PPA - session id - $id"
  74.     else
  75.         continue
  76. fi
  77. }
  78.  
  79. function clean_ppa {
  80. echo "the final part of the script is to clean ppa than you don't really want to use no more."
  81. repositories=(/etc/apt/sources.list.d/*.list)
  82. for f in "${repositories[@]}"; do
  83.    echo "$f"
  84.     read -p "Do you want to remove this ppa ? (y/n)" -n 1 -r && echo -e
  85.  
  86. if [[  $REPLY =~ ^[Yy]$ ]]
  87.     then
  88.         sudo rm -f $f
  89.         echo "$f removed !" && logger "ubuntu.cleanup.sh - Removed PPA $f from /etc/apt/sources.list.d/ - session id - $id"
  90.     else
  91.         continue
  92. fi
  93. done
  94. }
  95.  
  96.  
  97. function list_packages_and_prompt_user {
  98. for i in $(dpkg --get-selections | awk '{print $1}') ; do
  99.     echo $i
  100. extraction2=`grep -oHs $(apt-cache policy $i |grep -i http | awk '{print $2}' | sed -e 's/\/$//g') /etc/apt/sources.list.d/*.list |tr ' ' '\n'  |grep -n http | awk -F: '{print $2}'`
  101. z=`apt-cache policy $i |grep -i "$url" |grep "$value" |wc -l`
  102. if (( z == 0 )) ; then
  103. clear
  104. echo "###############"
  105. apt-cache policy $i
  106. echo "###############"
  107. echo "this package belongs to repository below :"
  108. if [ -z "$extraction2" ]; then
  109.     echo "Could not find related repository"
  110. else
  111.     echo $extraction2 | awk '{print $1}'
  112. fi
  113. Yy
  114. fi
  115. done
  116. }
  117.  
  118. function remove_no_longer_maintained_packages {
  119. echo "removing packages that are no longer maintained/ not exist in the repositories"
  120. number_of_not_existent_packges=`apt-show-versions | grep 'No available' | cut -d' ' -f 1 | wc -l`
  121. if (( number_of_not_existent_packges != 0 )) ; then
  122.     echo "found $number_of_not_existent_packges packages"
  123.     for i in `apt-show-versions | grep 'No available' | cut -d' ' -f 1` ; do  echo -e $i ; Yy ; done
  124. else
  125.         echo "no packages in this state found !"
  126. fi
  127. }
  128.  
  129. function exit_script {
  130. echo "finishing with apt-get update ..."
  131. sudo apt-get update > /dev/null && logger "ubuntu.cleanup.sh - script finished - session id - $id"
  132. echo "Script completed !"
  133. exit 1
  134. }
  135.  
  136. ## editable configuration ##
  137. url=archive.ubuntu.com
  138. value=500
  139. ubuntucleanupsessions=/var/lib/ubuntu-cleanup-sessions
  140. ## end of config ##
  141.  
  142. validate_userid_equal_0
  143. generate_session_id
  144. the_start_of_script_should_log_to_syslog
  145. install_script_requirements
  146. list_packages_and_prompt_user
  147. remove_no_longer_maintained_packages
  148. clean_ppa
  149. exit_script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement