定时 数据库清理的两个脚本:
按天删除:
代码语言:javascript复制#!/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语句
按照具体时间删除:
代码语言:javascript复制#!/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的设置:
代码语言:javascript复制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