#!/bin/bash ########################################################################################################################################################################## ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## + Name: MaintenanceWithReboot.sh ## + Purpose: Performs daily Ubuntu maintenance tasks and updates Docker containers. Reboots machine if need after updates are installed. ## + Author: Vince Cantrell ## + ## + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## + Instructions: ## + 1. Download the script to a location such as /scripts and make it executable with chmod +x ## + 2. If you use HealthChecks (Recommend: docs.linuxserver.io/images/docker-healthchecks/) Download Runitor to /usr/sbin/runitor (github.com/bdd/runitor) ## + 3. Add the following line to your crontab: 00 4 * * * /usr/sbin/runitor -api-url="%%YOUR_PING_URL%%" -uuid %%YOUR_UUID%% -- /scripts/MaintenanceWithReboot.sh ## + 4. Optionally: Install apt-fast to help with streamlining the updating of apps from aptitude. (github.com/ilikenwf/apt-fast) ## + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## + ## + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## + Last Modification Date: 03/19/2024 ## + DevOps Pipeline Version Number: 1.5 ## + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## + ## + --------------------------------------------------------------------------------------------------------------------------------------------------------------------- ## + Change Log: ## + ## + v1.5 - 03/19/2024 - Vince Cantrell - Enhanced formatting and rewrote the update section to work whether apt-fast is installed or not. Re-released script. ## + ## + v1.4 - 02/15/2024 - Vince Cantrell - Completely rewrote the docker section to recursively loop through all directories underneath '/compose/'. ## + ## + v1.3 - 03/17/2021 - Vince Cantrell - Formatted and released the initial release of this maintenance script and distributed it to all my ubuntu hosts. ## + ## + v1.2 - 02/21/2021 - Vince Cantrell - Added the docker cleanup section and updated the aptitude section to use apt-fast. ## + ## + v1.1 - 01/11/2021 - Vince Cantrell - Added in the docker update section to update all docker-compose directories under '/compose/'. ## + ## + v1.0 - 12/07/2020 - Vince Cantrell - Initial build of the Script with just the basic aptitude update sections without structure or formatting. ## + --------------------------------------------------------------------------------------------------------------------------------------------------------------------- ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ########################################################################################################################################################################## ################################################################################################################################ ###=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=### ################################################################################################################################ #################################################################################################### #/////////////////////////////////////////////////////////////////////////////////////////////////// #//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #// SCRIPT SETUP: SET FUNCTIONS AND VARIABLES #//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #/////////////////////////////////////////////////////////////////////////////////////////////////// #################################################################################################### #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ##///////////////////////////////////////////////////////////## ##=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=## ##=~=~=~=~=~=~=~= Script Functions =~=~=~=~=~=~=~=~## ##=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=## ##///////////////////////////////////////////////////////////## #------------------------------------------------------------------------ ######################################################### ### Docker Compose Process Directories Function ### ######################################################### # Function to process each directory process_directory() { for dir in "$1"/*; do if [ -d "$dir" ]; then echo "Processing $dir" # Change to the directory cd "$dir" || exit # Pull the docker compose, suppressing stdout and stderr docker compose pull > /dev/null 2>&1 docker compose up -d --remove-orphans # Go back to the previous directory cd - > /dev/null 2>&1 # Recursively process subdirectories process_directory "$dir" fi done } #------------------------------------------------------------------------ #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ##///////////////////////////////////////////////////////////## ##=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=## ##=~=~=~=~=~=~=~= Script Variables =~=~=~=~=~=~=~=~## ##=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=## ##///////////////////////////////////////////////////////////## #------------------------------------------------------------------------ ######################################################### ### Docker Compose Directory Location Variable ### ######################################################### # Directory that houses all of the docker-compose directories on the system compose_dir="/compose" #------------------------------------------------------------------------ #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ################################################################################################################################ ###=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=### ################################################################################################################################ #################################################################################################### #/////////////////////////////////////////////////////////////////////////////////////////////////// #//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #// SCRIPT EXECUTION SECTION: UPDATE UBUNTU VIA APT AND DOCKER VIA DOCKER-COMPOSE #//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #/////////////////////////////////////////////////////////////////////////////////////////////////// #################################################################################################### #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ##///////////////////////////////////////////////////////////## ##=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=## ##=~=~=~=~=~=~=~= System Update Section =~=~=~=~=~=~=~=## ##=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=## ##///////////////////////////////////////////////////////////## #------------------------------------------------------------------------ ######################################################### ### Update using Apt-Get or Apt-Fast (If Installed) ### ######################################################### echo "---------------------------------------------------------------" echo "Commencing System & Docker Update & Cleanup: `date`" echo "---------------------------------------------------------------" echo "1. Commencing Apt Update and Cleanup..." # Check if apt-fast is installed if command -v apt-fast &> /dev/null; then echo "apt-fast is installed" # Update and upgrade with apt-fast, handling potential interactivity apt-fast update -y || true # Update, suppress errors if interaction is needed apt-fast upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" || true apt-fast dist-upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" || true apt-fast autoclean -y apt-fast clean -y apt-fast autoremove -y else echo "apt-fast is not installed, using apt-get" # Update and upgrade with apt-get, handling potential interactivity apt-get update -y || true apt-get upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" || true apt-get dist-upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" || true apt-get autoclean -y apt-get clean -y apt-get autoremove -y fi echo "Ubuntu Apt Update and Cleanup Complete!" echo "---------------------------------------------------------------" #------------------------------------------------------------------------ #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ##///////////////////////////////////////////////////////////## ##=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=## ##=~=~=~=~=~=~=~= Docker Update Section =~=~=~=~=~=~=~=## ##=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=## ##///////////////////////////////////////////////////////////## #------------------------------------------------------------------------ ######################################################### ### Update All Docker Containers and Prune System ### ######################################################### echo "2. Commencing Docker Pull and Up" process_directory "$compose_dir" echo "---------------------------------------------------------------" echo "4. Pruning Docker & Restarting Code Server" docker system prune --volumes -af service code-server@root restart echo "---------------------------------------------------------------" #------------------------------------------------------------------------ #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ##///////////////////////////////////////////////////////////## ##=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=## ##=~=~=~=~=~=~=~= System Reboot Section =~=~=~=~=~=~=~=## ##=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=## ##///////////////////////////////////////////////////////////## #------------------------------------------------------------------------ ######################################################### ### Reboot the System if a Reboot is Required ### ######################################################### echo "5. Rebooting if necessary" if [ -f /var/run/reboot-required ] then echo "Reboot is required. Rebooting in 1 minute." shutdown -r +1 else echo "No Reboot Necessary!" fi echo "---------------------------------------------------------------" echo "Finished System & Docker Update & Cleanup: `date`" echo "---------------------------------------------------------------" #------------------------------------------------------------------------ #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #// END OF FILE #//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////