From f2472bd0d3ebe684db7fa80f1aa0a0dda44d0899 Mon Sep 17 00:00:00 2001 From: Philipp Rothmann Date: Tue, 23 Nov 2021 17:38:31 +0100 Subject: [PATCH 1/3] make backup.path comma separated list --- README.md | 4 ++-- backup.sh | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 19485ce..ccc7589 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,11 @@ services: backupbot.backup: "true" backupbot.backup.pre-hook: 'mysqldump -u root -p"$(cat /run/secrets/db_root_password)" -f /tmp/dump/dump.db' backupbot.backup.post-hook: "rm -rf /tmp/dump/dump.db" - backupbot.backup.path: "/tmp/dump/" + backupbot.backup.path: "/tmp/dump/,/etc/foo/" ``` - `backupbot.backup` -- set to `true` to back up this service (REQUIRED) -- `backupbot.backup.path` -- file path within the service to copy (REQUIRED) +- `backupbot.backup.path` -- comma separated list of file paths within the service to copy (REQUIRED) - `backupbot.backup.pre-hook` -- command to run before copying files (optional) - `backupbot.backup.post-hook` -- command to run after copying files (optional) diff --git a/backup.sh b/backup.sh index fc0fb22..1ea1ad0 100755 --- a/backup.sh +++ b/backup.sh @@ -88,16 +88,19 @@ if [[ \ $*\ != *\ --skip-backup\ * ]]; then container=$(docker container ls -f "name=$service" --format '{{ .ID }}') echo "backing up $service" - test -d "$backup_path/$service" || mkdir "$backup_path/$service" if [ "$pre" != "null" ]; then # run the precommand # shellcheck disable=SC2086 docker exec "$container" sh -c "$pre" fi + + test -d "$backup_path/$service" || mkdir -p "$backup_path/$service" # run the backup - docker cp "$container:$path" "$backup_path/$service" + for p in ${path//,/ }; do + docker cp "$container:$p" "$backup_path/$service" + done if [ "$post" != "null" ]; then # run the postcommand @@ -115,5 +118,5 @@ if [[ \ $*\ != *\ --skip-backup\ * ]]; then fi if [[ \ $*\ != *\ --skip-upload\ * ]]; then - _restic backup --tag coop-cloud "$backup_path" + _restic backup --host "$server_name" --tag coop-cloud "$backup_path" fi From ce42fb06fd0a338f94cb7457ffe81cd93e55731d Mon Sep 17 00:00:00 2001 From: Philipp Rothmann Date: Wed, 24 Nov 2021 11:17:13 +0100 Subject: [PATCH 2/3] fix docker cp paths --- backup.sh | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/backup.sh b/backup.sh index 1ea1ad0..e20daa7 100755 --- a/backup.sh +++ b/backup.sh @@ -21,7 +21,7 @@ if [ -n "$ssh_key_file" ] && [ -f "$ssh_key_file" ]; then # Only check server against provided SSH_HOST_KEY, if set if [ -n "$SSH_HOST_KEY" ]; then tmpfile=$(mktemp) - echo "$SSH_HOST_KEY" >> "$tmpfile" + echo "$SSH_HOST_KEY" >>"$tmpfile" echo "using host key $SSH_HOST_KEY" ssh_options="-o 'UserKnownHostsFile $tmpfile'" elif [ "$SSH_HOST_KEY_DISABLE" = "1" ]; then @@ -79,27 +79,28 @@ if [[ \ $*\ != *\ --skip-backup\ * ]]; then pre=$(echo "$details" | jq -r '.["backupbot.backup.pre-hook"]') post=$(echo "$details" | jq -r '.["backupbot.backup.post-hook"]') path=$(echo "$details" | jq -r '.["backupbot.backup.path"]') - + if [ "$path" = "null" ]; then echo "ERROR: missing 'path' for $service" - continue # or maybe exit? + continue # or maybe exit? fi container=$(docker container ls -f "name=$service" --format '{{ .ID }}') - + echo "backing up $service" - + if [ "$pre" != "null" ]; then # run the precommand # shellcheck disable=SC2086 docker exec "$container" sh -c "$pre" fi - - test -d "$backup_path/$service" || mkdir -p "$backup_path/$service" # run the backup for p in ${path//,/ }; do - docker cp "$container:$p" "$backup_path/$service" + # creates the parent folder, so `docker cp` has reliable behaviour no matter if $p ends with `/` or `/.` + dir=$backup_path/$service/$(dirname "$p") + test -d "$dir" || mkdir -p "$dir" + docker cp "$container:$p" "$dir/$(basename "$p")" done if [ "$post" != "null" ]; then @@ -120,3 +121,5 @@ fi if [[ \ $*\ != *\ --skip-upload\ * ]]; then _restic backup --host "$server_name" --tag coop-cloud "$backup_path" fi + +rm -rf "${backup_path}" \ No newline at end of file From d6faffcbbd43c9f17e0ab4c7d5359d6097116aa0 Mon Sep 17 00:00:00 2001 From: Philipp Rothmann Date: Mon, 13 Dec 2021 11:37:02 +0100 Subject: [PATCH 3/3] move rm up, to keep the latest backup in the volume --- backup.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backup.sh b/backup.sh index e20daa7..709964b 100755 --- a/backup.sh +++ b/backup.sh @@ -72,6 +72,8 @@ else fi if [[ \ $*\ != *\ --skip-backup\ * ]]; then + rm -rf "${backup_path}" + for service in "${services[@]}"; do echo "service: $service" details=$(docker service inspect "$service" --format "{{ json .Spec.Labels }}") @@ -100,7 +102,7 @@ if [[ \ $*\ != *\ --skip-backup\ * ]]; then # creates the parent folder, so `docker cp` has reliable behaviour no matter if $p ends with `/` or `/.` dir=$backup_path/$service/$(dirname "$p") test -d "$dir" || mkdir -p "$dir" - docker cp "$container:$p" "$dir/$(basename "$p")" + docker cp -a "$container:$p" "$dir/$(basename "$p")" done if [ "$post" != "null" ]; then @@ -122,4 +124,3 @@ if [[ \ $*\ != *\ --skip-upload\ * ]]; then _restic backup --host "$server_name" --tag coop-cloud "$backup_path" fi -rm -rf "${backup_path}" \ No newline at end of file