shell脚本实例


代码如下:
<pre name="code" class="html">#!/bin/bash
# clean job info from database which created 60 days ago
# before clean data, get offset from bns, then delay [offset * 1800] s
# if get offset failed. clean operation will not be executed


readonly MYSQL_HOSTNAME=xx
readonly PORT=xx
readonly USER=xx
readonly PASSWORD=xx
readonly TABLE=xx.xx
readonly TEST_BNS=xx
readonly PROXY_BNS=xx
readonly WORKSPACE=$(cd $(dirname $0); pwd)
readonly LOCAL_HOSTNAME=`hostname`
readonly HOSTNAME=${LOCAL_HOSTNAME/.baidu.com/}
readonly DATE=-60day
readonly CREATE_TIME="$(date -d $DATE '+%F %H:%M:%S')"
readonly DEL_OPERATION="DELETE  FROM $TABLE WHERE create_time < '$CREATE_TIME' limit 1000"
readonly SELECT_OPERATION="select count(*) as cnt FROM $TABLE WHERE create_time < '$CREATE_TIME'"

#get bns offset, if failed, gen random number: [0-4]
offset=-1
multiply_factor=1800

#record how many data left in database
count=-1

Prompt(){
	echo "[$(date '+%D %H:%M:%S')] $@"
}

ok() {
    echo "[ok] ${1}"
}

#gen random number between [1, 5]
randNumber() {
    tmp=$RANDOM
    offset=$[ tmp % 5 ]
}


delayTime() {
    #here we cannot check $?, which is aways 0
    tmp=$(get_instance_by_service "$PROXY_BNS" -o | grep "$HOSTNAME" | awk '{print $2}')
    if [ "$tmp"x = ""x ]; then
        Prompt "get bns offset failed, clean operation will not be executed.."
        exit -1
    else
        offset=$tmp
        Prompt "get bns offset success, bns offset: $offset"
    fi
}

delOperation() {
    mysql  -h $MYSQL_HOSTNAME -P$PORT -u$USER -p$PASSWORD -e "${DEL_OPERATION}"
}

countOperation() {
    declare countTmp=`mysql  -h $MYSQL_HOSTNAME -P$PORT -u$USER -p$PASSWORD -e "${SELECT_OPERATION}"`

    if [ "$countTmp"x = ""x ]; then
        Prompt "count expired job failed, left clean operation will not be executed.."
        exit -1
    else
        count=$(echo $countTmp | awk '{print $2}')
        Prompt "count expired job success, [$count] job-data left"
    fi
}

#only reserve data whose create_time within 60 days
cleanExpiredData() {
    Prompt "select operation is $SELECT_OPERATION "
    Prompt "delete operation is $SELECT_OPERATION "
    Prompt "will del jobs where create_time < $CREATE_TIME"

    countOperation
    while [ $count -gt 0 ]
    do
        delOperation
        countOperation
    done
}


start() {
    delayTime
    if [ $offset -eq -1 ]; then
        Prompt "get factor for delay time failed, please check.."
        exit -1
    else
        ok "get factor for delay time success"
    fi
    sleeptime=$[ offset * multiply_factor ]
    Prompt "clean mysql data after $sleeptime seconds .."
    sleep $sleeptime
    Prompt "wake up from sleep, start to clean data .."
    cleanExpiredData
    if [ $? -eq 0 ]; then
        ok "clean data success."
    else
        Prompt "clean data failed .."
        exit -1
    fi
}

start



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值