1. Create a backup directory (it is recommended to place it at the host directory level of mysql mapping)
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
to see the effect: