第一步:在服务器上配置备份目录代码:
--------------------------------------------------------------------------------
mkdir /var/lib/mysqlbackup
cd /var/lib/mysqlbackup
--------------------------------------------------------------------------------
第二步:编写备份脚本代码:
--------------------------------------------------------------------------------
vi dbbackup.sh
--------------------------------------------------------------------------------
粘帖以下代码,务必更改其中的username,password和dbname。
代码:
--------------------------------------------------------------------------------
#!/bin/sh
mysqldump -uuser -ppassword dbname | gzip > /var/lib/mysqlbackup/dbname`date +%Y-%m-%d_%H%M%S`.sql.gz
cd /var/lib/mysqlbackup
rm -rf `find . -name '*.sql.gz' -mtime 10` #删除10天前的备份文件
--------------------------------------------------------------------------------
第三步:更改备份脚本权限
代码:
--------------------------------------------------------------------------------
chmod +x dbbackup.sh
--------------------------------------------------------------------------------
第四步:用crontab定时执行备份脚本代码:
--------------------------------------------------------------------------------
crontab -e
--------------------------------------------------------------------------------
若每天晚上21点00备份,添加如下代码,
代码:
--------------------------------------------------------------------------------
00 21 * * * /var/lib/mysqlbackup/dbbackup.sh