#!/bin/bash # Check if the Docker Compose file was provided if [ $# -eq 0 ]; then echo "No Docker Compose file provided" exit 1 fi COMPOSE_FILE=$1 DIR=$(dirname "${COMPOSE_FILE}") BASE_DIR_NAME=$(basename "${DIR}") BACKUP_DIR=/mnt/backups/$BASE_DIR_NAME TIMESTAMP=$(date +"%Y%m%d%H%M%S") # Create a new backup directory for this stack if it doesn't already exist mkdir -p $BACKUP_DIR # Stop the Docker Compose stack docker-compose -f $COMPOSE_FILE down # Tarball the Docker Compose directory tar -czvf $BACKUP_DIR/"files_${DIR##*/}"_$TIMESTAMP.tar.gz -C / $DIR # Identify and save each Docker image used by the stack IMAGES=$(docker-compose -f $COMPOSE_FILE config | awk '{if ($1 == "image:") print $2;}') for IMAGE in $IMAGES; do docker save $IMAGE | gzip > $BACKUP_DIR/"image_${IMAGE//[:\/]/_}"_$TIMESTAMP.tar.gz done # Restart the Docker Compose stack docker-compose -f $COMPOSE_FILE up -d