利用crontab为Linux(ubuntu)定时备份Mysql数据库,定时执行备份文件,按日期对备份结果进行保存,达到备份的目的.

1、创建一个crontab命令文件
sudo vim /usr/sbin/backup
输入
DB_USER=root
DB_PASS=123456
BAK_DIR=/home/webback
NAME=$(date +%Y%m%d)
mysqldump -u$DB_USER -p$DB_PASS dbname >$BAK_DIR/dbname_$NAME.sql

/home/webback表示保存备份文件的目录,这个每个人也可以根据自己的要求来做

2、修改文件属性,使其可执行
chmod +x /usr/sbin/backup

3、修改/etc/crontab
sudo crontab -e
添加内容
01 1 * * 5 /usr/sbin/backup
表示每周五1点1分执行备份mysql中dbname数据库.

4、重新启动crond
sudo /etc/init.d/cron restart

完成操作。这样每天你在/home/webback可以看到这样的文件
dbname_20100819.sql

One Comment

  1. We have examine few of the web sites on your website given that yesterday, and I truly such as your kind of blogging and site-building. I book-marked that in order to my favorite features website checklist and you will be checking out back shortly. Kindly visit my own web site in addition as well as let me know your own view-point.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


*