zoukankan      html  css  js  c++  java
  • shell脚本-删除当天日期前3个月的数据表

    #!/bin/bash
    #author:skycheng
    
    #get current date string
    datestr=`date +'%Y-%m-%d'`
    start_time=`date +'%Y-%m-%d %H:%M:%S'`
    
    #get logfile name
    logfile='/var/log/fs/drop_tables_'$datestr'.log'
    
    if [ ! -f $logfile ]
    then
        sudo touch $logfile
    fi
    
    #drop tables table name:sms_send_log_X_yyyymmdd
    do_drop_tables()
    {
        for table in $tables
            do 
                #get table date 'yyyymmdd'
                echo $table|grep sms_send_log>/dev/null 2>&1
                if [ $? -eq 0 ] 
                then
                    table_date=${table:0-8}
                    if [ $table_date < $drop_date ]
                    then
                        #echo $table
                        mysql -u$db_user -p$db_pass -h$db_host $database -e 'drop tables '$table  2>>$logfile
                        [ $? -eq 0 ] && echo "drop table:"$table "success"|tee -a $logfile || echo "drop table:"$table "fail"
                    else
                        continue
                    fi
                else
                    continue
                fi
        done
    }
    
    echo starttime:$start_time>>$logfile
    
    #get three month ago date tables
    drop_date=`date -d '3 month ago' +"%Y%m%d"`
    echo 'drop_date='$drop_date
    
    db_user=dbuser
    db_pass=dbpass
    
    #get all database tables on host db_host
    db_host=db_host
    database=database
    tables=`mysql -u$db_user -p$db_pass -h$db_host $database -e 'show tables;'|sed 1d`
    
    do_drop_tables
  • 相关阅读:
    NYOJ 10 skiing DFS+DP
    51nod 1270 数组的最大代价
    HDU 4635 Strongly connected
    HDU 4612 Warm up
    POJ 3177 Redundant Paths
    HDU 1629 迷宫城堡
    uva 796
    uva 315
    POJ 3180 The Cow Prom
    POJ 1236 Network of Schools
  • 原文地址:https://www.cnblogs.com/sky-cheng/p/10565663.html
Copyright © 2011-2022 走看看