Skip to content

Instantly share code, notes, and snippets.

@alberthdev
Created April 5, 2015 20:31
Show Gist options
  • Save alberthdev/45ec0dbc83f102e17c4d to your computer and use it in GitHub Desktop.
Save alberthdev/45ec0dbc83f102e17c4d to your computer and use it in GitHub Desktop.
Script to reinstall Debian packages that have D-Bus configuration files
#!/bin/bash
# Debian System Reinstaller - D-Bus config file package reinstaller
# Copyright (C) 2015 Albert Huang
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ---
# This script assumes you are using a Debian based system
# (Debian, Mint, Ubuntu, #!), and have sudo installed. If you don't
# have sudo installed, replace "sudo" with "su -c" instead.
#
# Note that this script is specifically designed to reinstall D-Bus
# configuration files missing from your system. This does NOT reinstall
# your whole system.
#
becho() {
echo -e "\033[1m$@\033[0m"
}
spinat=0
spinner() {
spinat=`expr $spinat + 1`
if [ "$spinat" = "1" ];then
spin="|"
elif [ "$spinat" = "2" ];then
spin="/"
elif [ "$spinat" = "3" ];then
spin="-"
elif [ "$spinat" = "4" ];then
spin="\\"
spinat=0;
else
spinat=0;
fi
printf "\b$spin"
}
spinner_loop() {
while :; do
spinner
sleep 0.1s
done
}
spinner_register() {
spinner_proc=$1
export spinner_proc
}
spinner_cancel() {
kill -PIPE $spinner_proc
printf "\b \b"
}
stopnow=0
export stopnow
trap "echo 'Detected CTRL-C, exiting...'; stopnow=1; export stopnow" INT TERM
becho " * Scanning for packages with /etc/dbus-1/system.d files..."
spinner_loop &
spinner_register %%
systemdotd_pkgs=`dpkg -S dbus-1/system.d | cut -f1 -d':' | sed 's/,//g'`
spinner_cancel
[ "$stopnow" = "1" ] && exit
becho " * Scanning for all installed packages on the system..."
spinner_loop &
spinner_register %%
pkgs=`dpkg --get-selections | grep -w 'install$' | cut -f 1 | \
egrep -v '(dpkg|apt)'`
spinner_cancel
[ "$stopnow" = "1" ] && exit
becho " * Filtering system.d installed packages..."
spinner_loop &
spinner_register %%
final_pkgs=""
for syspkg in $systemdotd_pkgs; do
echo $pkgs | grep $syspkg 2>&1 >/dev/null && \
echo $final_pkgs | egrep -v $syspkg 2>&1 >/dev/null && \
if [ "$final_pkgs" == "" ]; then final_pkgs="$syspkg"; \
else final_pkgs="$final_pkgs $syspkg"; fi
done
spinner_cancel
[ "$stopnow" = "1" ] && exit
becho " * Final packages to reinstall to recover /etc/dbus-1/system.d files:"
echo " $final_pkgs"
becho " * Reinstallation will start in 3 seconds..."
[ "$stopnow" = "1" ] && exit
sleep 3s
[ "$stopnow" = "1" ] && exit
becho " * Reinstalling..."
rm -f reinstall_dbus.log
for pkg in $final_pkgs; do
echo -e "\033[1m * Reinstalling:\033[0m $pkg"
echo "***** Reinstalling: $pkg *****" >> reinstall_dbus.log
printf " "
[ "$stopnow" = "1" ] && exit
spinner_loop &
spinner_register %%
sudo apt-get -q -y --force-yes install --reinstall -o Dpkg::Options::="--force-confmiss" $pkg >> reinstall_dbus.log
cerr=$?
spinner_cancel
backstep " "
if [ ! "$cerr" = "0" ]; then
echo "ERROR: Reinstallation failed. See reinstall.log for details."
exit 1
fi
[ "$stopnow" = "1" ] && exit
done
@ageis
Copy link

ageis commented Feb 24, 2018

@alberthdev Oh gosh, this script saved my life. Thank you. I'd suggest running shellcheck; I don't know what the "backstep" command is, it's not registered. Also --force-yes is deprecated these days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment