From 189f2c36de260fe3edadc454aca0732798f4a16f Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Thu, 17 Apr 2025 14:00:16 -0400 Subject: [PATCH] feat: Adds restore script. --- README.md | 11 +++++++++++ docker-backup.sh | 13 ++++++------- docker-restore.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 docker-restore.sh diff --git a/README.md b/README.md index f57e66b..d3d20bb 100644 --- a/README.md +++ b/README.md @@ -50,3 +50,14 @@ sudo systemctl start backup.service 1. Add any extra volumes to backup. 1. Make script executable `chmod +x docker-backup.sh` 1. Manually run the script `sudo ./docker-backup.sh` + +## Restore + +This repository also contains a restore script that can restore the stacks and +volumes from a backup (hopefully it's never needed!). + +### Usage + +1. Download the script `wget https://git.housh.dev/homelab/backup/raw/branch/main/docker-restore.sh` +1. Make script executable `chmod +x docker-restore.sh` +1. Run the script `sudo ./docker-restore.sh /backups/.tar.gz` diff --git a/docker-backup.sh b/docker-backup.sh index 0a8c09e..5371804 100644 --- a/docker-backup.sh +++ b/docker-backup.sh @@ -30,7 +30,7 @@ function copyStacks() { local dir="$1/stacks" mkdir "$dir" for service in "${services[@]}"; do - cp -R "$service" "$dir" + cp -r --parents "$service" "$dir" done } @@ -39,17 +39,16 @@ function copyVolumes() { local dir="$1/volumes" mkdir "$dir" for volume in "${volumes[@]}"; do - cp -R "$volume" "$dir" + cp -r --parents "$volume" "$dir" done } function backup() { echo "Creating backup..." - tar -cvf "/$today.tar" "$1" - gzip "/$today.tar" - cp -R "/$today.tar.gz" "$backupdir" + tar -cvzf "/$today.tar.gz" "$1" + cp -r "/$today.tar.gz" "$backupdir" # Cleanup temporary files. - rm -rf "$1" "/$today.tar" "/$today.tar.gz" + rm -rf "$1" "/$today.tar.gz" } function main() { @@ -57,7 +56,7 @@ function main() { stopServices # Create a temporary directory. - local temp="/tmp/$today" + local temp="/$today" mkdir "$temp" # copy stack configuration. diff --git a/docker-restore.sh b/docker-restore.sh new file mode 100644 index 0000000..a9d588e --- /dev/null +++ b/docker-restore.sh @@ -0,0 +1,46 @@ +#!/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 "$@"