Last active
December 24, 2019 14:04
-
-
Save ivanpetrushev/3c96cf982e8c605df481b123d609bdcf to your computer and use it in GitHub Desktop.
backup all mysql docker containers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# to restore use: | |
# cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -uroot -p"$MYSQL_ROOT_PASSWORD" DATABASE | |
NOW=$(date +%Y-%m-%d) | |
WHERE=/backup/daily-mysql/ | |
# list only running mysql containers | |
CONTAINERS=$(docker ps --format 'table {{.Image}} {{.Names}}' | grep mysql | awk '{print $2}') | |
echo "All mysql containers: " | |
echo $CONTAINERS | |
for CONTAINER in ${CONTAINERS[@]}; do | |
mkdir -p $WHERE/$CONTAINER | |
echo -n "Dumping $CONTAINER... " | |
docker exec $CONTAINER sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' | gzip > $WHERE/$CONTAINER/$CONTAINER-$NOW.sql.gz | |
echo "done!" | |
done | |
echo "All done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment