Thursday 5 July 2007

MySQL shell: Binlog & Cron

1. CACTI
cacti监控磁盘使用状况,如果有问题就发送email

2. 删除日志
一种是从MySQL内部删除另外一种就是手工删除:

2.1 MySQL内部删除,主要就是更新mysql-bin.index,如果用2的手工删除,mysql-bin.index内部的记录不会被清除,MySQL内部删除就没用了,只能手动修改mysql-bin.index,然后重启生效。

PURGE {MASTER | BINARY} LOGS TO ‘log_name’


PURGE MASTER LOGS TO ‘mysql-bin.010
;

PURGE {MASTER | BINARY} LOGS BEFORE ‘date’
PURGE MASTER LOGS BEFORE ‘2008-06-22 13:00:00
;

PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 3 DAY);

2.2 手工删除,三种方法那种都行:

$ sudo su - root -c "rm -f /to/log/path/mysql-bin.0020*"

$ sudo su - root -c "for i in $(ls -v /to/log/path/mysql-bin.0020*); do rm -f $i ; done"

$ sudo su - root -c "find /to/log/path/ -name "mysql-bin.*" -mtime +4 | xargs rm -fr"

保存其中一个为:delete_log_before_5_days.sh
日志文件为:delete_log_before_5_days_log

3. Cron

$emacs -nw /etc/rc.d/rc.local
#
末尾加: /sbin/service crond start

3.1 直接用crontab命令编辑

$ crontab -u
#
设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数
$ crontab -l #
列出某个用户cron服务的详细内容
$ crontab -r #
删除某个用户的cron服务
$ crontab -e #
编辑某个用户的cron服务

$ crontab -u root -l #root
查看自己的设置
$ crontab -u username -r #root
删除其他人的cron设置


3.2 cron格式

cron log:
*/1 * * * * ls >> /tmp/ls.txt //for a single cron
e /etc/syslog.conf //for all crons
#uncomment below:

cron.* /var/log/cron.log
* 逗号(',') 指定列表值。如: "1,3,4,7,8"
* 中横线('-') 指定范围值 如 "1-6", 代表 "1,2,3,4,5,6"
* 星号 ('*') 代表所有可能的值

* "/"
代表每的意思
* "*/5"
表示每5个单位

Linux(开源系统似乎都可以)下还有个 "/" 可以用. 在 Minute 字段上,*/15 表示每 15 分钟执行一次. 而这个特性在商业 Unix ,比如 AIX 上就没有.

# Use the hash sign to prefix a comment
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
然后设置为:

0 23 * * *
/to/sh/path/delete_log_before_5_days.sh
> delete_log_before_5_days_log
#
每天23点运

更多例子:

0 */2 * * * echo "Have a break now." >> /tmp/test.txt
#
每两个小时

0 23-7/2
8 * * * echo "Have a good dream:)" >> /tmp/test.txt
#
晚上11点到早上8点之间每两个小时,早上八点

0 11 4 * 1-3 command line
#
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11

0 4 1 1 * command line
#1
1日早上4


3.3 编辑/etc/crontab 文件配置cron

cron
服务每分钟不 仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用 cron服务做一些事 情。用crontab配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务。此文件的文件格式是:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root //
如果出现错误,或者有数据输出,数据作为邮件发给这个帐号
HOME=/ //
使用者运行的路径,这里是根目录

# run-parts

01 * * * * root run-parts /etc/cron.hourly //
每小时执行/etc/cron.hourly内的脚本
02 4 * * * root run-parts /etc/cron.daily //
每天执行/etc/cron.daily内的脚本
22 4 * * 0 root run-parts /etc/cron.weekly //
每星期执行/etc/cron.weekly内的脚本
42 4 1 * * root run-parts /etc/cron.monthly //
每月去执行/etc/cron.monthly内的脚本

大家注意"run-parts"这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名了。
--------------------------------------
基本格式 :
*
  *  *  *  *  command
分 时 日 月 周 命令
1列表示分钟159 每分钟用*或者 */1表示
2列表示小时1230表示0点)
3列表示日期131
4列表示月份112
5列标识号星期060表示星期天)
6列要运行的命令

网上crontab
文件的一些例子:

30 21 * * * /usr/local/etc/rc.d/lighttpd restart
#每晚的21:30重启lighttpd

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
#每月1、10、22日的4 : 45重启lighttpd

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
#每周六、周日的1 : 10重启lighttp

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
#每天18 : 00至23 : 00之间每隔30分钟重启lighttpd

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
#每星期六的11 : 00 pm重启lighttpd

* */1 * * * /usr/local/etc/rc.d/lighttpd restart
#每一小时重启lighttpd

* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
#晚上11点到早上7点之间,每隔一小时重启lighttpd

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
#每月的4号与每周一到周三的11点重启lighttpd

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
#一月一号的4点重启lighttpd