Files
env-docker/bin/backup.sh

97 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# 2024-01-29 Hyperling
# Create a backup file with a generic name for polling.
DIR="$(dirname -- "${BASH_SOURCE[0]}")"
PROG="$(basename -- "${BASH_SOURCE[0]}")"
echo "$DIR/$PROG"
source $DIR/../source.env
## Variables ##
TAG="Docker"
DATE="`date "+%Y%m%d-%H%M%S"`"
BACKUP_DIR="/srv/backup"
BASENAME="Backup"
file="$BACKUP_DIR/$BASENAME.$DATE.$HOSTNAME.$TAG.zip"
time="`which time`"
## Functions ##
function usage {
cat <<- EOF
Backup script for Hyperling's self-managed Docker setup.
Usage: $PROG [-u] [-d] [-h]
-u : Bring all containers up after the backup has finished.
-d : Bring all containers down before taking the backup.
-h : Display this help text.
Example:
$PROG -ud
EOF
exit $1
}
## Parameters ##
up=FALSE
down=FALSE
while getopts ':udh' opt; do
case "$opt" in
u) up=TRUE ;;
d) down=TRUE ;;
h) usage 0 ;;
*) echo "ERROR: Option $OPTARG not recognized." >&2
usage 1 ;;
esac
done
## Main ##
# Ensure backup directory exists with correct permissions.
mkdir -pv "$BACKUP_DIR"
mkdir -pv "$BACKUP_DIR/TRASH"
chmod -Rv 775 "$BACKUP_DIR"
chown -Rv root:root "$BACKUP_DIR"
# Remove the last backup.
echo "`date` - Removing existing files."
cd "$BACKUP_DIR"
mv -v "$BASENAME"*"$TAG"* TRASH/
rm -v TRASH/*
if [[ "$down" == "TRUE" ]]; then
echo -e "\n`date` - Take down services for a cold backup."
manage.sh -d
else
echo -e "\n`date` - Skipping take down."
fi
echo -e "\n`date` - Create the backup for '$DOCKER_HOME'."
$time zip -r $file.tmp \
$DOCKER_HOME \
/etc/crontab /etc/cron.d /var/spool/cron \
/var/{log,mail} 1>/dev/null
mv -v $file.tmp $file
echo -e "\n`date` - Done with zipping, check size."
ls -sh $file
echo -e "\n`date` - Ensure other users can access the file."
chmod -v 755 $file
if [[ "$up" == "TRUE" ]]; then
echo -e "\n`date` - Bring services back up."
manage.sh -u
else
echo -e "\n`date` - Skipping restoring services."
fi
## Finish ##
echo -e "\n`date` - Done!"
exit 0