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
|
#!/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')
|
today=$(date +'%F')
|
||||||
stackdir="/etc/komodo/stacks"
|
stackdir="/etc/komodo/stacks"
|
||||||
backupdir="/backups"
|
backupdir="/backups"
|
||||||
|
|
||||||
services=($(find "$stackdir" -maxdepth 1 -mindepth 1 -type d))
|
services=($(find "$stackdir" -maxdepth 1 -mindepth 1 -type d))
|
||||||
|
|
||||||
# Directories to backup.
|
# Directories to backup.
|
||||||
volumes=(
|
volumes=(
|
||||||
"/var/lib/docker/volumes"
|
"/var/lib/docker/volumes"
|
||||||
"/opt"
|
"/opt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# ==================================================
|
||||||
|
# Helpers
|
||||||
|
# ==================================================
|
||||||
|
|
||||||
function stopServices() {
|
function stopServices() {
|
||||||
echo "Stopping services..."
|
echo "Stopping services..."
|
||||||
for service in "${services[@]}"; do
|
for service in "${services[@]}"; do
|
||||||
@@ -26,28 +53,6 @@ function startServices() {
|
|||||||
done
|
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() {
|
function backup() {
|
||||||
echo "Creating backup..."
|
echo "Creating backup..."
|
||||||
tar -cvzf "/$today.tar.gz" "$1"
|
tar -cvzf "/$today.tar.gz" "$1"
|
||||||
@@ -65,7 +70,33 @@ function cleanBackups() {
|
|||||||
done
|
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.
|
# stop services.
|
||||||
stopServices
|
stopServices
|
||||||
|
|
||||||
@@ -89,4 +120,56 @@ function main() {
|
|||||||
cleanBackups
|
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 "$@"
|
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 "$@"
|
|
||||||
25
install.sh
25
install.sh
@@ -5,21 +5,24 @@ systemdir="/etc/systemd/system"
|
|||||||
bindir="/usr/local/bin"
|
bindir="/usr/local/bin"
|
||||||
|
|
||||||
function downloadScripts() {
|
function downloadScripts() {
|
||||||
local scripts=(
|
local script="docker-backup"
|
||||||
"docker-backup"
|
|
||||||
|
local scriptsToRemove=(
|
||||||
"docker-restore"
|
"docker-restore"
|
||||||
"docker-restart"
|
"docker-restart"
|
||||||
)
|
)
|
||||||
|
|
||||||
for script in "${scripts[@]}"; do
|
# set the destination of the script.
|
||||||
# set the destination of the script.
|
local dest="$bindir/$script"
|
||||||
local dest="$bindir/$script"
|
# Remove the file if it exists.
|
||||||
# Remove the file if it exists.
|
[ -f "$dest" ] && rm -f "$dest"
|
||||||
[ -f "$dest" ] && rm -f "$dest"
|
# download the script to the destination.
|
||||||
# download the script to the destination.
|
wget "$baseurl/$script.sh" -O "$dest"
|
||||||
wget "$baseurl/$script.sh" -O "$dest"
|
# make the script executable.
|
||||||
# make the script executable.
|
chmod +x "$dest"
|
||||||
chmod +x "$dest"
|
|
||||||
|
for script in "${scriptsToRemove[@]}"; do
|
||||||
|
[ -f "$script" ] && rm -f "$script"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user