Skip to content

Instantly share code, notes, and snippets.

@VirtuBox
Last active January 13, 2023 13:30
Show Gist options
  • Save VirtuBox/150549f42ad4daff5b1ac00c26a69788 to your computer and use it in GitHub Desktop.
Save VirtuBox/150549f42ad4daff5b1ac00c26a69788 to your computer and use it in GitHub Desktop.
simple maintenance bash script
MAINTENANCE_APT() {
# Colors
# Colors
CSI='\033['
CEND="${CSI}0m"
CGREEN="${CSI}1;32m"
if [ "$(id -u)" = "0" ] || [ -n "$IS_SUDOERS" ]; then
export DEBIAN_FRONTEND=noninteractive
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT UPDATE '
echo -e "${CGREEN}#############################################${CEND}"
if ! {
apt-get update --allow-releaseinfo-change
}; then
apt-get update
fi
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT FULL-UPGRADE '
echo -e "${CGREEN}#############################################${CEND}"
apt-get --option=Dpkg::options::=--force-confmiss --option=Dpkg::options::=--force-confold --option=Dpkg::options::=--force-unsafe-io -y dist-upgrade
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT-GET AUTOREMOVE '
echo -e "${CGREEN}#############################################${CEND}"
apt-get -o Dpkg::Options::="--force-confmiss" -o Dpkg::Options::="--force-confold" -y --purge autoremove
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT AUTOCLEAN '
echo -e "${CGREEN}#############################################${CEND}"
apt-get -o Dpkg::Options::="--force-confmiss" -o Dpkg::Options::="--force-confold" -y autoclean
apt-get -y clean
## clean packages in deinstall state
DEINSTALLED=$(dpkg --get-selections | grep deinstall | cut -f1)
if [ -n "$DEINSTALLED" ]; then
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' CLEAN DEINSTALLED PACKAGES '
echo -e "${CGREEN}#############################################${CEND}"
dpkg --get-selections | grep deinstall | cut -f1 | xargs dpkg --purge
fi
if [ "$1" = "--docker" ]; then
if command_exists docker; then
list_images=$(docker images --filter "dangling=true" -q --no-trunc)
list_volumes=$(docker volume ls -qf dangling=true)
if [ -n "$list_images" ]; then
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' DOCKER IMAGES CLEANUP '
echo -e "${CGREEN}#############################################${CEND}"
docker rmi "$list_images"
fi
if [ -n "$list_volumes" ]; then
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' DOCKER VOLUMES CLEANUP '
echo -e "${CGREEN}#############################################${CEND}"
docker volume rm "$list_volumes"
fi
fi
fi
OLD_LOGS=$(find /var/log/ -type f -mtime +30 -iname "*.gz")
if [ -n "$OLD_LOGS" ]; then
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' CLEANUP OLD LOGS '
echo -e "${CGREEN}#############################################${CEND}"
find /var/log/ -type f -mtime +30 -iname "*.gz" -exec rm '{}' \;
fi
else
echo "you need to be root or sudoers to launch the maintenance"
fi
}
alias maintenance=MAINTENANCE_APT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment