docker-maintenance/docker-maintenance-functions

137 lines
3.3 KiB
Bash

#!/bin/bash
# -- Docker Maintenance Script Functions
# -- 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/)
# WRITE_LOG FUNCTION ----------------------------------------------------------
function write_log() {
local logDate=$(date +'%F')
local logTime=$(date +'%T')
echo "$logDate $logTime $2" >> "$1"
}
# BACKUP_CHECK FUNCTION -------------------------------------------------------
function backup_check() {
ext=".tar"
# check for limit to number of backups to keep
if [ $keepBackups -ne 0 ]
then
# get number of backups & backup file names
numBackups=`ls -l $backupLocation/$c | grep backup | grep $ext | wc -l`
backupArray=(`ls $backupLocation/$c | grep backup | grep $ext`)
# loop while numBackups >= keepBackups to delete the oldest file
while [ $numBackups -gt $keepBackups ]
do
# echo deletion to standard output
echo "Deleting $backupLocation/$c/${backupArray[0]}"
echo
# write backup deletion to log
write_log "$maintenanceLog" "deleting $backupLocation/$c/${backupArray[0]}"
echo "" >> $maintenanceLog
# remove oldest backup
rm -f $backupLocation/$c/${backupArray[0]}
# get number of backups & backup file names
numBackups=`ls -l $backupLocation/$c | grep backup| grep $ext | wc -l`
backupArray=(`ls $backupLocation/$c | grep backup | grep $ext`)
done
fi
}
# CONTAINER_SIZE FUNCTION -----------------------------------------------------
function container_size() {
# find the size of the container & split it into the digits and units
containerArray=(`docker ps -s | grep "$c" | sed 's/.$//'`)
unitSize=${containerArray[16]}
onlyDigits=(`echo $unitSize | sed 's/..$//'`)
sizeLen=${#onlyDigits}
sizeLen=$((sizeLen+1))
onlyUnits=(`echo $unitSize | cut -c $sizeLen-`)
sizeArray=(`du -s $containerLocation/$cDir | grep "$cDir"`)
dirSize=${sizeArray[0]}
# convert size to kilobytes
if [ "$onlyUnits" = "GB" ]
then
containerSize=$((onlyDigits*1024*1024+dirSize))
fi
if [ "$onlyUnits" = "MB" ]
then
containerSize=$((onlyDigits*1024+dirSize))
fi
if [ "$onlyUnits" = "KB" ]
then
containerSize=$((onlyDigits+dirSize))
fi
}
# BACKUP_SIZE FUNCTION --------------------------------------------------------
function backup_size() {
requiredSpace=0
# get required space if backup method is "snapshot"
if [ "$backupMethod" = "snapshot" ]
then
dirCount=0
# loop through containers to calculate size of complete backup
for c in ${containerNames[@]}; do
# get corresponding container directory
cDir=${containerDirs[$dirCount]}
dirCount=$((dirCount+1))
#get container size
container_size
# calculate total size of all containers
requiredSpace=$((requiredSpace+containerSize))
done
fi
# get required space if backup method is "files"
if [ "$backupMethod" = "files" ]
then
# loop through container directories
for c in ${containerDirs[@]}; do
sizeArray=(`du -s $containerLocation/$c | grep $c`)
dirSize=${sizeArray[0]}
requiredSpace=$((requiredSpace+dirSize))
done
fi
}
# END OF FUNCTIONS ------------------------------------------------------------