Skip to content

Instantly share code, notes, and snippets.

@ipcjk
Created March 3, 2019 13:45
Show Gist options
  • Save ipcjk/66b1734e774e60020cd7bedbce196c1f to your computer and use it in GitHub Desktop.
Save ipcjk/66b1734e774e60020cd7bedbce196c1f to your computer and use it in GitHub Desktop.
backup mysql databases inside docker containers
#!/bin/bash
mysql_containers=`docker ps -f ancestor=mysql:8.0 --format '{{.ID}}:{{.Names}}'`
for i in $(echo $mysql_containers)
do
echo $i
arr=($(echo $i | sed "s/:/ /g"))
if [ "${#arr[@]}" -eq "2" ]; then
docker exec ${arr[0]} sh -c 'exec mysqldump -uroot -p"$MYSQL_ROOT_PASSWORD" --all-databases' > /mysqlbackups/${arr[1]}.sql
gzip -9f /mysqlbackups/${arr[1]}.sql
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment