Last active
May 15, 2020 16:15
-
-
Save TBog/d856bee22beba03bed5910b0d8f6a8f8 to your computer and use it in GitHub Desktop.
Upgrade and save log output for future replay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SCRIPT_DIR=$(cd `dirname $0` && pwd) | |
# if not root, run as root | |
if (( $EUID != 0 )); then | |
echo sudo $SCRIPT_DIR/`basename $0` "$@" | |
sudo $SCRIPT_DIR/`basename $0` "$@" | |
exit | |
fi | |
# the current directory is used for output | |
pushd $SCRIPT_DIR &> /dev/null | |
TEXT_YELLOW='\e[0;33m' | |
TEXT_RED='\e[1;31m' | |
TEXT_RESET='\e[0m' | |
if [[ "$(ps -ocommand= -p $PPID | awk '{print $1}')" != "script" ]]; then | |
# forward all params escaped | |
COMMAND="$0" | |
for arg in "$@"; do | |
COMMAND+=" " | |
COMMAND+=$(printf %q "$arg") | |
done | |
timestamp=$(date +%Y%m%d-%H%M%S) | |
script --quiet --command "$COMMAND" --timing=$timestamp.timing $timestamp.log | |
TAR_FILE="upgrade_log_$timestamp.tar.bz2" | |
tar -cjf $TAR_FILE $timestamp.timing $timestamp.log | |
if [[ $? -eq 0 ]]; then | |
rm --interactive=never $timestamp.timing | |
rm --interactive=never $timestamp.log | |
echo -e "Logs: $(pwd)/${TEXT_YELLOW}${TAR_FILE}${TEXT_RESET}" | |
else | |
echo -e "${TEXT_RED}Failed to compress logs$TEXT_RESET" | |
echo -e "timing file: $(pwd)/${TEXT_YELLOW}$timestamp.timing${TEXT_RESET}" | |
echo -e " log file: $(pwd)/${TEXT_YELLOW}$timestamp.log${TEXT_RESET}" | |
fi | |
popd &> /dev/null | |
exit | |
fi | |
time ( \ | |
apt update && \ | |
echo -e "${TEXT_YELLOW}apt update${TEXT_RESET} finished\n" && \ | |
apt -y autoremove && \ | |
echo -e "${TEXT_YELLOW}apt autoremove${TEXT_RESET} finished\n" && \ | |
apt -y full-upgrade && \ | |
echo -e "${TEXT_YELLOW}apt full-update${TEXT_RESET} finished\n" && \ | |
apt purge -y $(dpkg --get-selections | awk '{if ($2=="deinstall") print $1}') && \ | |
echo -e "${TEXT_YELLOW}apt purge${TEXT_RESET} finished" \ | |
) | |
if [ -f /var/run/reboot-required ]; then | |
echo -e $TEXT_RED | |
cat /var/run/reboot-required | |
echo -e $TEXT_RESET | |
fi | |
updatedb | |
popd &> /dev/null | |
echo "done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment