From 054ab8fbe63351674dacc73815ef5e6edd450f62 Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Thu, 8 Jul 2021 18:39:49 +0200 Subject: [PATCH] when stopped container was part of a stack service, update service instead --- README.md | 7 +++++++ src/backup.sh | 29 ++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ef8b6c4..720f2da 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,12 @@ volumes: data: ``` +## Using with Docker Swarm + +By default, Docker Swarm will restart stopped containers automatically, even when manually stopped. If you plan to have your containers / services stopped during backup, this means you need to apply the `on-failure` restart policy to your service's definitions. A restart policy of `always` is not compatible with this tool. + +--- + ## Differences to `futurice/docker-volume-backup` This image is heavily inspired by the `futurice/docker-volume-backup`. We decided to publish this image as a simpler and more lightweight alternative because of the following requirements: @@ -115,3 +121,4 @@ This image is heavily inspired by the `futurice/docker-volume-backup`. We decide - The original image proposed to handle backup rotation through AWS S3 lifecycle policies. This image adds the option to rotate old backups through the same script so this functionality can also be offered for non-AWS storage backends like MinIO. - InfluxDB specific functionality was removed. - `arm64` and `arm/v7` architectures are supported. +- Docker in Swarm mode is supported. diff --git a/src/backup.sh b/src/backup.sh index 95f0089..d46c7a2 100644 --- a/src/backup.sh +++ b/src/backup.sh @@ -39,7 +39,7 @@ if [ "$CONTAINERS_TO_STOP_TOTAL" != "0" ]; then fi info "Creating backup" -BACKUP_FILENAME="$(date +"${BACKUP_FILENAME:-backup-%Y-%m-%dT%H-%M-%S.tar.gz}")" +BACKUP_FILENAME="$(date +"$BACKUP_FILENAME")" tar -czvf "$BACKUP_FILENAME" $BACKUP_SOURCES # allow the var to expand, in case we have multiple sources if [ ! -z "$GPG_PASSPHRASE" ]; then @@ -51,8 +51,31 @@ if [ ! -z "$GPG_PASSPHRASE" ]; then fi if [ "$CONTAINERS_TO_STOP_TOTAL" != "0" ]; then - info "Starting containers back up" - docker start $CONTAINERS_TO_STOP + info "Starting containers/services back up" + # The container might be part of a stack when running in swarm mode, so + # its parent service needs to be restarted instead once backup is finished. + SERVICES_REQUIRING_UPDATE="" + for CONTAINER_ID in $CONTAINERS_TO_STOP; do + SWARM_SERVICE_NAME=$( + docker inspect \ + --format "{{ index .Config.Labels \"com.docker.swarm.service.name\" }}" \ + $CONTAINER_ID + ) + if [ -z "$SWARM_SERVICE_NAME" ]; then + echo "Restarting $(docker start $CONTAINER_ID)" + else + echo "Removing $(docker rm $CONTAINER_ID)" + # Multiple containers might belong to the same service, so they will + # be restarted only after all names are known. + SERVICES_REQUIRING_UPDATE="${SERVICES_REQUIRING_UPDATE} ${SWARM_SERVICE_NAME}" + fi + done + + if [ -n "$SERVICES_REQUIRING_UPDATE" ]; then + for SERVICE_NAME in "$(echo -n "$SERVICES_REQUIRING_UPDATE" | tr ' ' '\n' | sort -u)"; do + docker service update --force $SERVICE_NAME + done + fi fi if [ ! -z "$AWS_S3_BUCKET_NAME" ]; then