Skip to content

Instantly share code, notes, and snippets.

@ivanpetrushev
Last active December 24, 2019 14:04
Show Gist options
  • Save ivanpetrushev/3c96cf982e8c605df481b123d609bdcf to your computer and use it in GitHub Desktop.
Save ivanpetrushev/3c96cf982e8c605df481b123d609bdcf to your computer and use it in GitHub Desktop.
backup all mysql docker containers
#!/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