#!/usr/bin/env bash script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/${script_name%%.*}.conf" log_file="$(mktemp)" random_prefix="$(head -c 300 /dev/urandom | tr -cd 'a-zA-Z0-9' | head -c 8)" backup_date=$(date +%Y-%m-%d-%H%M) # ============= # --- Some Variables # ============= export DEBIAN_FRONTEND=noninteractive removed_packages_log="/var/log/removed-debian-packages.log" updated_packages_log="/var/log/updated-debian-packages.log" # ============= # --- Some Functions # ============= clean_up() { if [[ -f "$_backup_crontab_file" ]]; then blank_line echononl " (Re)Install Crontab from previously saved crontab file '$_backup_crontab_file'.." crontab $_backup_crontab_file >> $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat $log_file)" fi fi # Perform program exit housekeeping rm -f $log_file rm -rf /tmp/*.${random_prefix} blank_line exit $1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n "[ \033[5m\033[1m....\033[m ]\033[13G$*\\c" 1>&2 else echo -e -n "[ \033[5m\033[1m....\033[m ]\033[13G$*" 1>&2 fi rm /tmp/shprompt$$ fi } #echo_print_command() { # if $terminal ; then # echo X\\c > /tmp/shprompt$$ # if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then # echo -e -n "\033[60G - \033[1m$*\\c\033[m" 1>&2 # else # echo -e -n "\033[60G - \033[1m$*\033[m" 1>&2 # fi # rm /tmp/shprompt$$ # fi #} echo_print_command() { if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n "\n\033[13G\033[1m$*\\c\033[m" 1>&2 else echo -e -n "\n\033[13G\033[1m$*\033[m" 1>&2 fi rm /tmp/shprompt$$ fi } echo_done() { if $terminal ; then echo -e "\033[1G[ \033[1;32mdone\033[m ]" fi } echo_ok() { if $terminal ; then echo -e "\033[1G[ \033[1;32mok\033[m ]" fi } echo_ignore() { if $terminal ; then echo -e "\033[1G[ \033[1;33mignore\033[m ]" fi } echo_warning() { if $terminal ; then echo -e "\033[1G[ \033[1;33m\033[1mwarn\033[m ]" fi } echo_failed(){ if $terminal ; then echo -e "\033[1G[ \033[1;31mfail\033[m ]" fi } echo_skipped() { if $terminal ; then echo -e "\033[1G[ \033[1;37mskip\033[m ]" fi } echo_wait(){ if $terminal ; then echo -en "\033[1G[ \033[5m\033[1m...\033[m ]" fi } fatal (){ blank_line if $terminal ; then echo -e " [ \033[31m\033[1mFatal\033[m ]: \033[37m\033[1m$*\033[m" echo "" echo -e " \033[31m\033[1m Script will be interrupted..\033[m\033[m" else echo "fatal: $*" echo "Script will be interrupted.." fi clean_up 1 } error(){ blank_line if $terminal ; then echo -e " [ \033[31m\033[1mFehler\033[m ]: $*" else echo "" echo "[ Error ]: $*" echo "" fi blank_line } warn (){ if $terminal ; then echo "" echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" echo "" fi } info (){ if $terminal ; then echo "" echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" echo "" fi } ## - Check if a given array (parameter 2) contains a given string (parameter 1) ## - containsElement () { local e for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done return 1 } trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo -n "$var" } blank_line() { if $terminal ; then echo "" fi } # ============= # --- Jobhandling # ============= # - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM # - trap clean_up SIGHUP SIGINT SIGTERM # ============= # --- Some Checks # ============= # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false fi if $terminal ; then echo "" echo "" echo -e "\033[13G\033[33mUpgrade Packages with a 'smart' conflict resolution system\033[m" echo "" fi blank_line echononl "Resynchronize the package index files" echo_print_command "apt-get update" if $terminal; then echo -e "\033[2A" fi apt-get -y update > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed if ! $terminal ; then error "Command \"apt-get update\" failed!" fi fatal "$(cat $log_file)" else echo_done fi if $terminal ; then echo -e "\033[2B" fi # - get list of upgradable packages # packages_updated="$(apt-get -V -s full-upgrade | grep -E "^ .*=>.*" | awk '{print$1}')" #blank_line #echononl "Upgrade Packages" #echo_print_command "apt full-upgrade" #if $terminal; then # echo -e "\033[2A" #fi #if [[ -n "$packages_updated" ]]; then # apt full-upgrade -y > $log_file 2>&1 # if [[ $? -gt 0 ]]; then # echo_failed # if ! $terminal ; then # error "Command \"apt full-upgrade\" failed!" # fi # fatal "$(cat $log_file)" # else # echo_done # echo # echo " This packages have been updated:" # for _package in $packages_updated ; do # echo -e " \033[1m$_package\033[m" # done # echo "" # fi #else # echo_skipped #fi #if $terminal ; then # echo -e "\033[2B" #fi blank_line echononl "Upgrade Packages" if ! $terminal && [[ -n "$packages_updated" ]] ; then echo "" echo " Upgrade Packages.." echo " apt-get full-upgrade" fi echo_print_command "apt-get full-upgrade" if $terminal; then echo -e "\033[2A" fi if [[ -n "$packages_updated" ]]; then apt-get full-upgrade -y > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed if ! $terminal ; then error "Command \"apt-get full-upgrade\" failed!" fi fatal "$(cat $log_file)" else echo_done echo echo if $terminal ; then echo -e "\033[13G\033[33mThis packages have been updated:\033[m" else echo " This packages have been updated:" fi echo "[ $(date +%Y-%m-%d" "%H:%M) ] This packages have been updated:" >> "$updated_packages_log" for _package in $packages_updated ; do if $terminal ; then echo -e "\033[13G \033[1m$_package\033[m" else echo " $_package\033[m" fi echo " $_package" >> "$updated_packages_log" done echo "" if $terminal ; then echo -e "\033[13GSee also: $updated_packages_log" else echo " See also: $updated_packages_log" fi echo "" echo "" >> "$updated_packages_log" fi else echo_skipped fi if $terminal ; then echo -e "\033[2B" fi blank_line echononl "Remove unused Packages" echo_print_command "apt-get autoremove" apt-get autoremove -y > $log_file 2>&1 if $terminal; then echo -e "\033[2A" fi if [[ $? -gt 0 ]]; then echo_failed if ! $terminal ; then error "Command \"apt-get autoremove\" failed!" fi fatal "$(cat $log_file)" else echo_done fi if $terminal ; then echo -e "\033[2B" fi blank_line echononl "Configure not yet configured packages" echo_print_command "dpkg --configure -a" dpkg --configure -a > $log_file 2>&1 if $terminal; then echo -e "\033[2A" fi if [[ $? -gt 0 ]]; then echo_failed if ! $terminal ; then error "Command \"dpkg --configure -a\" failed!" fi fatal "$(cat $log_file)" else echo_done fi if $terminal ; then echo -e "\033[2B" fi blank_line echononl "Clear out the local repository of retrieved package files" echo_print_command "apt-get clean" apt-get clean -y > $log_file 2>&1 if $terminal; then echo -e "\033[2A" fi if [[ $? -gt 0 ]]; then echo_failed if ! $terminal ; then error "Command \"apt-get clean\" failed!" fi fatal "$(cat $log_file)" else echo_done fi if $terminal ; then echo -e "\033[2B" fi # - Get list of packages with onlx config files present # - rc_packages="$(dpkg --list | awk '/^rc/ { print $2 }')" blank_line if $terminal; then echo -e "[ \033[5m\033[1m....\033[m ]\033[13GRemove (purge) packages with only config files installed (rc):" echo -en "\033[13G\033[1mapt-get purge \$(dpkg --list | awk '/^rc/ { print $2 }')\033[m" echo -e "\033[2A" else if [[ -n "$rc_packages" ]] ; then echo "" echo " Remove (purge) packages with only config files installed (rc).." echo " apt-get purge \$(dpkg --list | awk '/^rc/ { print $2 }')" fi fi if [[ -n "$rc_packages" ]] ; then apt-get purge -y $(dpkg --list | awk '/^rc/ { print $2 }') > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed if ! $terminal ; then error "Command \"apt-get purge -y \$(dpkg --list | awk '/^rc/ { print \$2 }')\" failed!" fi fatal "$(cat $log_file)" else echo_done echo echo if $terminal ; then echo -e "\033[13G\033[33mThis packages with only config files installed have been removed:\033[m" else echo " This packages with only config files installed have been removed:" fi echo "[ $(date +%Y-%d-%m" "%H:%M) ] This packages with only config files installed have been removed:" >> "$removed_packages_log" for _package in $rc_packages ; do if $terminal; then echo -e "\033[13G \033[1m$_package\033[m" else echo " $_package" fi echo " $_package" >> "$removed_packages_log" done echo "" if $terminal ; then echo -e "\033[13GSee also: $removed_packages_log" else echo " See also: $removed_packages_log" fi echo "" echo "" >> "$removed_packages_log" fi else echo_skipped fi if $terminal ; then echo -e "\033[2B" fi if [[ -f "/var/run/reboot-required" ]]; then if $terminal ; then echo -e "\n\t[ \033[31m\033[1m Reboot required! \033[m\033[m ]\n" else echo "" echo " System was updated - Reboot required!" echo "" fi fi clean_up 0