How to perform daily scheduled backup of MySQL in Docker

1. Create a backup directory (it is recommended to place it at the host directory level of mysql mapping)
Insert image description here

cd /softapp/mysql/
#创建目录
mkdir databak
cd databak
#创建备份Shell脚本
touch DatabaseBackup.sh
#添加可执行权限
chmod u+x DatabaseBackup.sh

2. Script writing.
Paste the following script directly into DatabaseBackup.sh.
Note:
Replace username with the actual user name;
replace password with the actual password;
replace DatabaseName with the actual database name.

docker exec -it mysql(容器名)  /bin/bash -c 'mysqldump -uroot -p123456 --databases 需要备份的数据库' > /data/backup/test_`date +%F`.sql(宿主机的文件路径);

3. Add scheduled tasks

crontab -e

Set up a backup of the database at 23:30 every day

30 23 * * *  /data/backup/DatabaseBackup.sh

You can put the first line
Insert image description here
to see the effect:
Insert image description here

Guess you like

Origin blog.csdn.net/mars131458/article/details/132537485