crontab定时备份mysql

来自http://tunps.com/crontab-mysql-backup

rhel5的默认的/etc/crontab文件内容:

[root@localhost ~]# more /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

run-parts是一个bash脚本,可以运行指定目录的shell,将以下内容保存为*.sh,并保存在/etc/cron.daily目录:
前面的内容分为5个部分,分别是:分 时 日 月 周 , root表示以root用户权限运行
那么/etc/cron.daily下脚本将会在每天临晨4点2分的时候运行

mysqldump --opt tunpscom | bzip2 -c > /data/`date +%Y%m%d_%H%M%S`.bz2

反引号里面的内容是执行date命令,后面%指定日期格式,具体请man date。还有一点需要注意的是bzip2压缩出来的体积比gz小,但是压缩时间长,几百MB看不出来,如果上了G就很明显了。所以也可以用gzip压缩代替:

mysqldump --opt tunpscom | gzip -c > /data/`date +%Y%m%d_%H%M%S`.gz

然后需要将这个*.sh文件的权限修改:chmod 755,让大家都有rx权限。

生成的文件的大概是:20110426_030103.bz2。

ps:10G大小的文本文件用gzip压缩后大小在300MB左右,用bzip2压缩后大小在100MB左右。除了以上介绍的方法,可以使用
crontab -e 新建一个计划任务。
crontab -r 删除计划任务。
crontab -l 列出计划任务。

About tunpishuang

just 4 fun·····
This entry was posted in 未分类 and tagged . Bookmark the permalink.

发表评论

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

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>