Files
backup/docker-backup.sh

194 lines
4.0 KiB
Bash

#!/bin/bash
function usage() {
cat <<EOF
docker-backup [options] [command] [args...]
COMMNADS:
run: Create a new backup (default if no options or command is passed)
restart: Restart all docker stacks.
restore: Restore from a backup, accepts argument which is path to an archive, if
no argument is supplied, then it will use the latest backup found.
OPTIONS:
-h | --help: Show this message.
--version: Show the version.
EOF
}
# ==================================================
# Variables
# ==================================================
debug=${DEBUG:-}
version=1.0.1
today=$(date +'%F')
stackdir="/etc/komodo/stacks"
backupdir="/backups"
services=($(find "$stackdir" -maxdepth 1 -mindepth 1 -type d))
# Directories to backup.
volumes=(
"/var/lib/docker/volumes"
"/opt"
)
# ==================================================
# Helpers
# ==================================================
function stopServices() {
echo "Stopping services..."
for service in "${services[@]}"; do
cd "$service" && docker compose stop
done
}
function startServices() {
echo "Restarting services..."
for service in "${services[@]}"; do
cd "$service" && docker compose up -d
done
}
function backupStacks() {
echo "Backing up stacks..."
local dir="$1/stacks"
mkdir "$dir"
for service in "${services[@]}"; do
cp -r --parents "$service" "$dir"
done
}
function backupVolumes() {
echo "Backing up volumes..."
local dir="$1/volumes"
mkdir "$dir"
for volume in "${volumes[@]}"; do
cp -r --parents "$volume" "$dir"
done
}
function backup() {
echo "Creating backup..."
tar -cvzf "/$today.tar.gz" "$1"
cp -r "/$today.tar.gz" "$backupdir"
# Cleanup temporary files.
rm -rf "$1" "/$today.tar.gz"
}
function cleanBackups() {
echo "Removing old backups..."
# Remove backups older than 7 days.
local dirs=($(find "$backupdir" -maxdepth 1 -mindepth 1 -name "*.tar.gz" -mtime +7))
for dir in "${dirs[@]}"; do
rm -rf "$dir"
done
}
function copyIfNotDebug {
if [ -z "$debug" ]; then
cp -r "$1" "$2"
else
echo "$1 -> $2"
fi
}
function restoreVolumes() {
echo "Copying volumes..."
local volumes=($(find "$1/volumes" -maxdepth 1 -mindepth 1 -type d))
for volume in "${volumes[@]}"; do
local dest="${volume#"$1"/volumes*}"
copyIfNotDebug "$volume" "$dest"
done
}
function restoreStacks() {
echo "Copying stacks..."
local stacks=($(find "$1/stacks" -maxdepth 1 -mindepth 1 -type d))
for stack in "${stacks[@]}"; do
local dest="${stack#"$1"/stacks*}"
copyIfNotDebug "$stack" "$dest"
done
}
function run() {
# stop services.
stopServices
# Create a temporary directory.
local temp="/$today"
mkdir "$temp"
# copy stack configuration.
backupStacks "$temp"
# copy stack configuration.
backupVolumes "$temp"
# Create a tar to backup.
backup "$temp"
# Restart services.
startServices
# Remove backups older than 7 days.
cleanBackups
}
function restore() {
# Parse the backup to use, if an argument is passed in use that as the source
# for the backup, otherwise use the last backup.
local archive="$1"
if [ -z "$archive" ]; then
echo "Backup not specified using latest backup..."
archive=$(find /backups -maxdepth 1 -mindepth 1 -name "*.tar.gz" | sort -nr | head -1)
fi
local dir="${archive%*.tar.gz}"
# unzip the archive.
echo "Unzipping backup: $archive"
tar -xvf "$archive"
# copy stacks and volumes to their locations.
restoreVolumes "$dir"
restoreStacks "$dir"
# cleanup
rm -rf "$dir"
echo "Restarting services..."
startServices
}
# ==================================================
# MAIN
# ==================================================
function main() {
case "$1" in
-h | --help)
usage && return 0
;;
--version)
echo "$version" && return 0
;;
restart)
startServices && return 0
;;
restore)
shift && restore "$@" && return 0
;;
*)
run "$@" && return 0
;;
esac
}
main "$@"