
正文
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 >&
if [ $? -eq ]
then
table_date=${table:-}
if [ $table_date \< $drop_date ]
then
#echo $table
mysql -u$db_user -p$db_pass -h$db_host $database -e 'drop tables '$table >>$logfile
[ $? -eq ] && 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_datedb_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








