From ce42fb06fd0a338f94cb7457ffe81cd93e55731d Mon Sep 17 00:00:00 2001 From: Philipp Rothmann Date: Wed, 24 Nov 2021 11:17:13 +0100 Subject: [PATCH] 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