定时 数据库清理的两个脚本:

按天删除:

#!/bin/bash

ndate=3
datestr=`date -d "-$ndate day" +%Y-%m-%d` #生成ndate天前的日期,如:
echo $datestr
delSqlStr="DELETE FROM xxxx WHERE GeneDate = '$datestr'" #SQL语句
mysql -uxxx -pxxxx database -e "$delSqlStr" #执行SQL语句

按照具体时间删除:

#!/bin/bash

datestr=$(date -d "72 hour ago" +"%Y-%m-%d %H:%M")                        #生成ndate天前的日期,如:
echo $datestr
delSqlStr="DELETE FROM xxxx WHERE insertTime < '$datestr'"     #SQL语句
mysql -uxxx -pxxx database -e "$delSqlStr"                           #执行SQL语句

crontab的设置:

0 3 * * * cd /home/dir && ./deletedbdata.sh
0 4 * * * cd /home/dir && ./deletedbdata2.sh
0 5 * * * cd /home/dir && ./deletedbdata3.sh
0 6 * * * cd /home/dir && ./deletedbdata4.sh
posted on 2019-12-20 09:53  小勇DW3  阅读(1039)  评论(0编辑  收藏  举报