To back up your MySQL database, the general syntax is:

sudo mysqldump -u[user] -p[password] [database_name] > [filename].sql


To restore a MySQL backup, enter:

mysql -u[user] -p[password]
create database [database_name];

mysql -u [user] -p [database_name] < [filename].sql


How to periodically backup MySQL Automatically

https://www.youtube.com/watch?reload=9&v=ECi4s1JXLNo

  1. 编写脚本
#!/bin/bash
YEAR=`date +%Y`
MONTH=`date +%m`
DAY=`date +%d`
cd ~/backup
mkdir -p $YEAR/$MONTH/
mysqldump -uroot -p****** dingtalk > $YEAR/$MONTH/$YEAR-$MONTH-$DAY-backup.sql


备份docker 中MySQL脚本

#!/bin/bash
mkdir -p ~/backup/$(date +"%Y/%m/")
docker exec -it dingtalk_mysql_1 mysqldump -uroot -p*** dingtalk > ~/backup/$(date +"%Y/%m/"%Y-%m-%d-%H:%M)-backup.sql


  1. crontab -e  添加定时任务
0 1 * * * exec `/bin/bash  /home/ubuntu/backup/backup.sh`