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
原文地址:https://www.cnblogs.com/sky-cheng/p/10565663.html