删除MySQL历史数据并修改sqlsource.status lastindex脚本

每月1号 00:00:00定时执行

保存最近一个月数据,如1月1日删除12月1日之前(11月)数据,12月31日时有最多的两个月数据(11与12月)

[flume@hqc-test-hdp3 ~]$ crontab -e
no crontab for flume - using an empty one
crontab: installing new crontab
[flume@hqc-test-hdp3 ~]$ crontab -l 
0 0 1 * * sh /xx/mysql_flume.sh > /xx/mysql_flume.log 2>&1 &

mysql_flume.sh

#!/bin/sh
p=$(cat /xx/mysql_p)
datatime=$(date +%Y%m%d -d '1 month ago')
echo $datatime
sql="SELECT count(*) FROM hqc.xx WHERE datatime<'$datatime'"  
cnt=$(mysql -hhqc-test-hdp1 -uroot -p$p -s -e "$sql") 
echo $cnt
# delete mysql history data
mysql -hhqc-test-hdp1 -uroot -p$p --database hqc -e "DELETE FROM xx WHERE datatime<'$datatime'"
# get sqlsource.status lastindex
last_index=$(cat /xx/sqlsource.status | cut -d '"' -f 12)
echo $last_index
# lastindex - cnt of mysql 
let "d_value=last_index-cnt"
echo $d_value
# replace sqlsource.status lastindex
sed -i "s/[1-9][0-9]\{4,\}/$d_value/" /xx/sqlsource.status
# restart agent
sh /xx/flume_agent.sh restart

exit

flume_agent.sh

#!/bin/bash
#echo "begin start flume..."

JAR="flume"

function start(){
echo "begin start flume process ...."

nohup /usr/hdp/current/flume-server/bin/flume-ng agent --name agent --conf conf --conf-file /xx/mysql_kafka_hive.conf > /var/log/flume/agent.out 2>&1 &

echo "start success...."
echo "日志路径: /var/log/flume/"
}
function stop(){
echo "stop flume process.."
num=`ps -ef|grep java|grep $JAR|wc -l`
#echo "$num...."
if [ "$num" != "0" ];then
#正常停止flume
ps -ef|grep java|grep $JAR|awk '{print $2;}'|xargs kill
echo "进程已经关闭..."
else
echo "服务未启动,无须停止..."
fi
}
function restart(){

#执行stop函数
stop

echo "flume process stoped,and starting..."
#执行start
start
echo "started...."
}
#case 命令获取输入的参数,如果参数为start,执行start函数,如果参数为stop执行stop函数,如果参数为restart,执行restart函数
case "$1" in
"start")
start
;;
"stop")
stop
;;
"restart")
restart
;;
*)
;;
esac

知识点

1.截取字符串可以用cut
last_index=$(cat sqlsource.status | cut -d '"' -f 12)

2.对花括号中的“\”字符转义 [1-9]开头 [0-9]\{4,\}4位以上数字  加-i才会替换原文本
sed -i "s/[1-9][0-9]\{4,\}/$d_value/" /xx/sqlsource.status
[root@hqc-test-hdp1 ~]# sed 's/[1-9][0-9]\{4,\}/401830/' sqlsource.status
{"ColumnsToSelect":"*","Table":"K401_online","LastIndex":"401830","SourceName":"r1","URL":"jdbc:mysql:\/\/xx.xx.xx.xx:3306\/hqc"}

3.MySQL远程访问只需安装客户端

4.crontab 
crontab -u //设定特定用户的定时服务
crontab -l //列出当前用户定时服务内容 
crontab -r //删除当前用户的定时服务
crontab -e //编辑当前用户的定时服务

0 0 1 * * sh /xx/mysql_flume.sh > /xx/mysql_flume.log 2>&1 &
例子:
#每月的最后1天
0 0 L * * *

说明:
Linux
*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
|    |    |    +---------- month (1 - 12) OR jan,feb,mar,apr ...
|    |    +--------------- day of month (1 - 31)
|    +-------------------- hour (0 - 23)
+------------------------- minute (0 - 59)


最后的& 后台执行
2>&1 错误正确日志都写入同一文件

你可能感兴趣的:(大数据动物园,Flume,MySQL,crontab,shell)