#!/bin/bash
# @Author: richard
# @Date: 2017-08-11 17:27:49
# @Last Modified by: richard
# @Last Modified time: 2017-08-11 18:04:58
#保留近 N 天
KEEP_DAYS=7
# 刪除前 N的所有天到 前N+10天==>每天執行
function get_todelete_days()
{
# declare -A DAY_ARR
# DAY_ARR=""
for i in $(seq 1 10);
do
THIS_DAY=$(date -d "$(($KEEP_DAYS+$i)) day ago" +%Y.%m.%d)
DAY_ARR=( "${DAY_ARR[@]}" $THIS_DAY)
done
echo ${DAY_ARR[*]}
}
# 返回數組的寫法
TO_DELETE_DAYS=(`get_todelete_days`)
for day in "${TO_DELETE_DAYS[@]}"
do
echo "$day will be delete"
curl -XDELETE 'http://127.0.0.1:9200/*-'${day}
done
寫到 crontab 里面去
crontab -l * 1 * * * /home/admin/elasticsearch/es_clear.sh
看看效果 只保留最近 7 天

