feat: Moves to a single script with several commands.
This commit is contained in:
133
docker-backup.sh
133
docker-backup.sh
@@ -1,17 +1,44 @@
|
||||
#!/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
|
||||
@@ -26,28 +53,6 @@ function startServices() {
|
||||
done
|
||||
}
|
||||
|
||||
function copyStacks() {
|
||||
echo "Copying stack configuration..."
|
||||
local dir="$1/stacks"
|
||||
mkdir "$dir"
|
||||
for service in "${services[@]}"; do
|
||||
cp -r --parents "$service" "$dir"
|
||||
done
|
||||
}
|
||||
|
||||
function copyVolumes() {
|
||||
echo "Copying volumes..."
|
||||
local dir="$1/volumes"
|
||||
mkdir "$dir"
|
||||
for volume in "${volumes[@]}"; do
|
||||
# copy only directories.
|
||||
local dirs=($(find "$volume" -maxdepth 1 -mindepth 1 -type d))
|
||||
for v in "${dirs[@]}"; do
|
||||
cp -r --parents "$v" "$dir"
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
function backup() {
|
||||
echo "Creating backup..."
|
||||
tar -cvzf "/$today.tar.gz" "$1"
|
||||
@@ -65,7 +70,33 @@ function cleanBackups() {
|
||||
done
|
||||
}
|
||||
|
||||
function main() {
|
||||
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
|
||||
|
||||
@@ -89,4 +120,56 @@ function main() {
|
||||
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 "$@"
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Restart all stacks / services located in '/etc/komodo/stacks'.
|
||||
|
||||
stackdir="/etc/komodo/stacks"
|
||||
dirs=($(find "$stackdir" -maxdepth 1 -mindepth 1 -type d))
|
||||
|
||||
for dir in "${dirs[@]}"; do
|
||||
cd "$dir" && docker compose up -d
|
||||
done
|
||||
@@ -1,46 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
debug=${DEBUG:-}
|
||||
|
||||
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 main() {
|
||||
local archive="$1"
|
||||
local dir="${archive%*.tar.gz}"
|
||||
|
||||
# unzip the archive.
|
||||
tar -xvf "$archive"
|
||||
|
||||
# copy stacks and volumes to their locations.
|
||||
copyVolumes "$dir"
|
||||
copyStacks "$dir"
|
||||
|
||||
# cleanup
|
||||
rm -rf "$dir"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
@@ -5,13 +5,13 @@ systemdir="/etc/systemd/system"
|
||||
bindir="/usr/local/bin"
|
||||
|
||||
function downloadScripts() {
|
||||
local scripts=(
|
||||
"docker-backup"
|
||||
local script="docker-backup"
|
||||
|
||||
local scriptsToRemove=(
|
||||
"docker-restore"
|
||||
"docker-restart"
|
||||
)
|
||||
|
||||
for script in "${scripts[@]}"; do
|
||||
# set the destination of the script.
|
||||
local dest="$bindir/$script"
|
||||
# Remove the file if it exists.
|
||||
@@ -20,6 +20,9 @@ function downloadScripts() {
|
||||
wget "$baseurl/$script.sh" -O "$dest"
|
||||
# make the script executable.
|
||||
chmod +x "$dest"
|
||||
|
||||
for script in "${scriptsToRemove[@]}"; do
|
||||
[ -f "$script" ] && rm -f "$script"
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user