176 lines
3.6 KiB
Bash
176 lines
3.6 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.0
|
|
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 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 copyVolumes() {
|
|
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 copyStacks() {
|
|
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.
|
|
copyStacks "$temp"
|
|
|
|
# copy stack configuration.
|
|
copyVolumes "$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.
|
|
copyVolumes "$dir"
|
|
copyStacks "$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 "$@"
|