22. Regularly back up the database

1. The solution The solution for
regularly backing up the database is roughly divided into three parts. The first is the timing scheduler, such as the timing plan on the Windows platform and the Crontab on the Linux platform. The second is database backup scripts, such as DOS batch scripts on the Windows platform and Shell scripts on the Linux platform. The last is the backup sql statement, mainly using select...into outfile and mysqldump.

2. Timed backup MySQL
schtasks.exe on Windows is used to schedule commands and programs to run or run regularly. It can add and delete tasks from the schedule, start and stop tasks as needed, display and change scheduled tasks.

#创建计划任务(每隔指定时间备份一次MySQL)
schtasks /create /sc minute /mo 1 /tn taskName /tr d:\backup\mysql_mydb_backup.bat

#备份数据库的脚本mysql_databaseName_backup.bat
mysqldump -h localhost -uroot -p123456 databaseName > d:\backup\databaseName.sql

#删除计划任务
schtasks /delete /tn taskName

3. Realize regular backup MySQL
Crontab on Linux

#每天凌晨00:00执行shell脚本(备份数据库)
0 0 * * * bash /home/backup/mysql_mydb_backup.sh

Shell script and backup statement

#!/bin/bash
#备份目录
backup_dir = /home/backup

#备份文件名
backup_filename = “mydb-`date +%Y%m%d`.sql“

#进入备份目录
cd $backup_dir

#备份数据库
mysqldump -h localhost -uroot -p123456 mydb > ${backup_dir}/${backup_filename}

#删除7天以前的备份
find ${backup_dir} -mtime +7 -name "*.sql" -exec rm -rf {
    
    } \;

Guess you like

Origin blog.csdn.net/Jgx1214/article/details/107496297