DATABASES=`docker exec mysql sh -c 'export MYSQL_PWD=$MYSQL_ROOT_PASSWORD; exec mysql -uroot -e "show databases;" | grep -Ev "(Database|information_schema|performance_schema|sys)"'`
for db in $DATABASES; do
echo "$db"
docker exec mysql sh -c 'export MYSQL_PWD=$MYSQL_ROOT_PASSWORD; exec mysqldump --databases '$db' -uroot' | gzip > ./$db-databases_`date +%Y_%d_%H%M`.sql.gz
done
Use this command to get the list of all databases inside the container
DATABASES=`docker exec mysql sh -c 'export MYSQL_PWD=$MYSQL_ROOT_PASSWORD; exec mysql -uroot -e "show databases;" | grep -Ev "(Database|information_schema|performance_schema|sys)"'`
Loop through the list of databases and execute a mysqldump command for each database. This command will create a backup file of your MySQL database in the current directory:
for db in $DATABASES; do
echo "$db"
docker exec mysql sh -c 'export MYSQL_PWD=$MYSQL_ROOT_PASSWORD; exec mysqldump --databases $db -uroot' | gzip > ./$db-databases_`date +%Y_%d_%H%M`.sql.gz
done
You can then copy the backup files to a safe location.
It's important to test your backups to ensure that they can be successfully restored in case of an emergency.
Note that in the provided code, the variable MYSQL_ROOT_PASSWORD should be replaced by the actual root password of your MySQL container and mysql should be replaced by the name of your container.