1. 编写脚本backup.sh,放在/usr/local/mysql/bin目录下
#!/bin/bash
backup_date=$(date "+%Y%m%d")
./mysqldump --add-drop-table -h127.0.0.1 localsvrdb > /root/backup/localsvrdb-$backup_date.sql
2. 给脚本赋予可执行的权限:
chmod +x backup.sh
3. 在/etc/my.cnf配置文件中配置MySQL的账号和密码,这样在mysqldump命令中就不需要写账号和密码了:
[mysqldump]
user=your user name
password=your password
4. 用crontab -e编辑crontab配置文件,添加如下行,定时启动MySQL备份脚本:
0 2 * * * . /etc/profile;/bin/sh /usr/local/mysql/bin/backup.sh