diff --git a/tools/backup_docker.sh b/tools/backup_docker.sh new file mode 100755 index 0000000..b207cc6 --- /dev/null +++ b/tools/backup_docker.sh @@ -0,0 +1,31 @@ +#!/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