backup dockerise mysql database from host volume
I have a little script for my MariaDb database inside a docker container.
The setup is comparable. I have a database container with a mounted volume. Furthermore I have a second volume for backups mounted under /backup.
The script is run on the host via cron and uses the database instance inside the container, so only one database accesses the data. That is the docker exec part.
I'm sure, you can adopt it to mysql. You only have to configure the CONTAINER_NAME and change the command inside the container.
#!/bin/bash
DATE_TIME=`date +&quot;%Y_%m_%d_%H_%M&quot;`
CONTAINER_NAME=&quot;mariadb&quot;
CONTAINER=`docker ps -q -f name=${CONTAINER_NAME}`
PASSWORD=`docker exec ${CONTAINER} sh -c 'echo $MYSQL_ROOT_PASSWORD'`
BACKUPFILE=${DATE_TIME}_${CONTAINER_NAME}.backup.gz

echo &quot;Backup of MariaDb&quot;
echo &quot;&quot;
echo &quot;Old backps will be deleted.&quot;
echo &quot;Datenbank Container Name: $CONTAINER_NAME&quot;
echo &quot;Datenbank Container:  $CONTAINER&quot;

echo &quot;Writing backup to ${BACKUPFILE}&quot;
echo &quot;&quot;
docker exec ${CONTAINER} sh -c &quot;find /backup/ -mindepth 1 -mtime +8 -delete; /usr/bin/mariabackup --backup --user root --password=${PASSWORD} --stream=xbstream | gzip &gt; /backup/${BACKUPFILE}&quot;
echo &quot;&quot;
echo &quot;Backup finished&quot;