docker-maintenance/docker-maintenance

207 lines
5.4 KiB
Bash

#!/bin/bash
# -- Docker Maintenance Script
# -- Written by: Robert Partridge (https://fosstodon.org/@techaddressed)
# -- Project URL: https://gitea.techaddressed.com/robert/docker-maintenance/
# -- License: MIT License (https://choosealicense.com/licenses/mit/)
# determine the script's directory
scriptDirectory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# import script functions
source $scriptDirectory/docker-maintenance-functions
# import user modifiable variables from .env file
source $scriptDirectory/docker-maintenance.env
# check whether to bypass root permission requirement
if [ "$bypassRoot" = false ]
then
# check for root privileges
if [ "$EUID" -ne 0 ]
then
# write error to standard output & exit
echo "Error: please run this script with sudo / root privileges"
exit
fi
fi
# get the current date
currentDate=$(date +'%F')
currentYear=$(date +'%Y')
currentMonth=$(date +'%m')
# create backup directory if necessary
mkdir -p $backupLocation
# get log filenames
maintenanceLog="$logLocation/$currentYear/$currentMonth-maintenance.log"
errorLog="$logLocation/$currentYear/$currentMonth-errors.log"
# setup log folders/files if necessary
mkdir -p $logLocation/$currentYear
touch $maintenanceLog
touch $errorLog
# determine space required to perform container backup
backup_size
# get available space at backup location
availableArray=(`df $backupLocation | grep "% /"`)
availableSpace=${availableArray[3]}
# check for sufficient space at backup location
if [ $requiredSpace -gt $availableSpace ]
then
# write error to standard output & error log then exit
echo "Error: insufficient space available for backup files"
write_log "$errorLog" "Error: insufficient space available for backup files - $requiredSpace kilobytes required / $availableSpace kilobytes available"
exit
fi
echo
echo "-- BEGINNING DOCKER MAINTENANCE --"
echo
write_log "$maintenanceLog" "Beginning Maintenance - Backup Method: $backupMethod"
if [ "$backupMethod" = "files" ]
then
# loop through container subdirectories
for c in ${containerDirs[@]}; do
# create backup folder if needed
mkdir -p $backupLocation/$c
# navigate to container's folder
cd $containerLocation/$c
# get size of the container
sizeArray=(`du -s`)
containerSize=${sizeArray[0]}
# record stopping the container
write_log "$maintenanceLog" "stopping $c"
# stop the container
docker-compose down
# record backup .tar.gz file creation to log
currentTime=$(date +'%T')
write_log "$maintenanceLog" "compressing $c-backup-$currentDate-$currentTime.tar.gz"
# create tar archive
echo "Creating $c archive"
tar -czvf $backupLocation/$c/$c-backup-$currentDate-$currentTime.tar.gz ./ >> /dev/null
# get archive size
sizeArray=(`du -s $backupLocation/$c/$c-backup-$currentDate-$currentTime.tar.gz`)
tarSize=${sizeArray[0]}
# record compression to log
write_log "$maintenanceLog" "$containerSize KB compressed to $tarSize KB"
# record image pull to log
write_log "$maintenanceLog" "pulling updated container image"
# pull updated container image
docker-compose pull
# record container start to log
write_log "$maintenanceLog" "starting / updating $c"
# start / update container
docker-compose up -d
# backup retention check
backup_check $c $keepBackups $numBackups ${backupArray[0]} $backupLocation $maintenanceLog
done
fi
if [ "$backupMethod" = "snapshot" ]
then
dirCount=0
# loop through container names
for c in ${containerNames[@]}; do
# create backup folder if needed
mkdir -p $backupLocation/$c
# record backup image creation
write_log "$maintenanceLog" "creating $c-backup image"
# commit backup image
echo "Committing $c backup image"
docker commit -p $c $c-backup >> /dev/null
# get size of the backup image
container_size
# record backup .tar file creation to log
currentTime=$(date +'%T')
write_log "$maintenanceLog" "backing up $c to $backupLocation/$c/$c-backup-$currentDate-$currentTime.tar"
# save backup image to .tar file
echo "Save $c backup image to disk"
docker save -o $backupLocation/$c/$c-backup-$currentDate-$currentTime.tar $c-backup >> /dev/null
# get size of .tar file
sizeArray=(`du -s $backupLocation/$c/$c-backup-$currentDate-$currentTime.tar`)
tarSize=${sizeArray[0]}
# record compression to log
write_log "$maintenanceLog" "$containerSize KB compressed to $tarSize KB"
# remove backup image
echo "Remove $c backup image"
docker image rm $c-backup >> /dev/null
# update container
cd $containerLocation/${containerDirs[$dirCount]}
docker-compose pull
docker-compose up -d
# backup retention check
backup_check $c $keepBackups $numBackups ${backupArray[0]} $backupLocation $maintenanceLog
# record container start to log
write_log "$maintenanceLog" "updated $c (if available)"
dirCount=$((dirCount+1))
done
fi
# clean up old container images
containerImages=$(docker images --quiet --filter "dangling=true")
if [ -n "$containerImages" ]
then
echo "Removing old container images"
write_log "$maintenanceLog" "remove old container images: $containerImages"
docker rmi $containerImages >> /dev/null
fi
# output maintenance completion
write_log "$maintenanceLog" "Maintenance Complete"
echo "" >> $maintenanceLog
echo
echo "-- DOCKER MAINTENANCE COMPLETE --"
echo