Skip to content

Instantly share code, notes, and snippets.

@Pitastic
Last active February 13, 2024 13:47
Show Gist options
  • Save Pitastic/9151fd962408b81c3b28b8b469578745 to your computer and use it in GitHub Desktop.
Save Pitastic/9151fd962408b81c3b28b8b469578745 to your computer and use it in GitHub Desktop.
Dynamic Backup Script for docker-composes
#/bin/bash
set -e
SCRIPT_PATH="`dirname \"$0\"`"
cd $SCRIPT_PATH
if [[ -z $1 ]]; then
echo "No target path for backup file provided."
echo "See source code for help."
fi
# === Usage ===
#
# Predefine all commands in this script and place it into your projects docker-compose folder.
#
# Execute it (e.g. via cron with root) with a full path to the backup file as argument $1
# (e.g. with a timestamp) and log the output to a logfile:
#
# `/bin/bash project/backup.sh backup_location/project_$(date +'%F_%H-%M-%S').tar.gz >> /var/log/autobackup.log 2&>1`
#
#
# Set the following placeholders or use them as variables
# to your needs:
#
# FULL_CONTAINER_NAME = Docker Container Name (longer)
# CONTAINER_NAME = Docker Compose Container Name (shorter)
# SHARED_FODLER = Folder which is bound from host into container
# DATABASE = Database we want to dump
# MYSQL_ROOT_PASSWORD = Environment variable (set in standard Mysql Containers)
#
# === Pre-Backup (optional; z.B. stop Service) ===
# Choose your command to dump data out to the host
# -> Dump via Script in Container (in a shared folder)
#echo "[$(date +'%F %H:%M:%S')] + Dump XXX"
#/usr/bin/docker compose exec -it ${CONTAINER_NAME} bash -c '/path/in/container/exec'
#echo "[$(date +'%F %H:%M:%S')] + OK"
# -> Dump PostgreSQL
#echo "[$(date +'%F %H:%M:%S')] + Dump PostgreSQL DB"
#/usr/bin/docker compose exec -it ${CONTAINER_NAME} pg_dump -U "user" ${DATABASE} | gzip > ${SHARED_FOLDER}/${DATABASE}.sql.gz
#echo "[$(date +'%F %H:%M:%S')] + OK"
# -> Dump MySQL
#echo "[$(date +'%F %H:%M:%S')] + Dump MySQL DB"
#/usr/bin/docker compose exec -T ${CONTAINER_NAME} bash -c 'mysqldump -uroot -p${MYSQL_ROOT_PASSWORD} --all-databases' > all-databases.sql || exit 1
#echo "[$(date +'%F %H:%M:%S')] + OK"
# -> Dump MongoDB
#echo "[$(date +'%F %H:%M:%S')] + Dump Mongo DB"
#/usr/bin/docker compose exec -t ${CONTAINER_NAME} mongodump --quiet --archive=/tmp/${DATABASE}.archive
#/usr/bin/docker cp ${FULL_CONTAINER_NAME}:/tmp/${DATABASE}.archive ${DATABASE}..archive
#/usr/bin/docker compose exec -t ${CONTAINER_NAME} bash -c 'rm -rf /data/${DATABASE}_*.archive'
#echo "[$(date +'%F %H:%M:%S')] + OK"
# -> Dump RedisDB
#echo "[$(date +'%F %H:%M:%S')] + Dump Redis DB"
#/usr/bin/docker cp ${FULL_CONTAINER_NAME}:/var/lib/redis/dump.rdb dump.rdb
#echo "[$(date +'%F %H:%M:%S')] + OK"
# === Bundle ===
# Bundle and Compress all files we want to keep (configs and dumps)
# to a given filename ($1, should have '.tar.gz')
# > All (".") but some:
tar --exclude NotInBackup1 --exclude NotInBackup2 -czf "${1}" .
# > Given list (relative to backup script):
# tar -czf "${1}" docker-compose.yml all-databases.sql folder1 folder2
# === Clean Up ===
# CleanUp all dumps and files you created above !
#rm -f *.sql.gz
#rm -f all-databases.sql
#rm -f *.archive
#rm -f dump.rdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment