commit 13eb58716bb1fb98fdb45e1c3b85e851cc72d0dc Author: 3wc <3wc@doesthisthing.work> Date: Mon Oct 25 13:35:40 2021 +0200 Initial version of filthy backup script diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..43fa83f --- /dev/null +++ b/backup.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +# FIXME: just for testing +backup_path=backups + +# FIXME: just for testing +export DOCKER_CONTEXT=demo.coopcloud.tech + +mapfile -t services < <(docker service ls --format '{{ .Name }}') + +# FIXME: just for testing +services=( "ghost_demo_app" "ghost_demo_db" ) + +for service in "${services[@]}"; do + echo "service: $service" + details=$(docker service inspect "$service" --format "{{ json .Spec.Labels }}") + if echo "$details" | jq -r '.["backupbot.backup"]' | grep -q 'true'; 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? + fi + + 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" $pre + fi + + # run the backup + docker cp "$container:$path" "$backup_path/$service" + + if [ "$post" != "null" ]; then + # run the postcommand + # shellcheck disable=SC2086 + docker exec "$container" $post + fi + fi + restic -p restic-password \ + backup --quiet -r sftp:u272979@u272979.your-storagebox.de:/demo.coopcloud.tech \ + --tag coop-cloud "$backup_path" +done