1. Create a new script, add a script content
we mysql_backup_test_backup.sh
#!/bin/bash Mysql # Set the login user name and password (fill in the actual situation) mysql_user="root" mysql_password="root" mysql_host="localhost" mysql_port="3306" mysql_charset="utf8mb4" # Backup file storage address (fill in the actual situation) backup_location=/usr/local # Whether to delete the expired data expire_backup_delete="ON" expire_days=7 backup_time=`date +%Y%m%d%H%M` backup_dir=$backup_location welcome_msg="Welcome to use MySQL backup tools!"
# Specify the database backup data (here assume that the database is mysql_backup_test)
docker exec -it mysql mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -B test1 > $backup_dir/mysql_backup_test-$backup_time.sql
# Delete the expired data if [ "$expire_backup_delete" == "ON" -a "$backup_location" != "" ];then `find $backup_location/ -type f -mtime +$expire_days | xargs rm -rf` echo "Expired backup data delete complete!" fi
2. Create a scheduled task
# Create a scheduled task command
crontab -e
# View scheduled tasks command
crontab the -l
# delete all scheduled tasks command
crontab -r
43 11,23 * * * cd /root/mysql/conf;sh mysql_backup_test_backup.sh >> log.txt 2>>log.txt