脚本
- cat > /data/software/crond/delete_es_index.sh <<'EOF'
- #!/usr/bin/env bash
- #####################################################################################
- #
- # 用途: 用于es中日志索引的定时删除
- #
- # 日期: 2022-11-25
- #
- # 作者: moyuanbo
- #
- #####################################################################################
-
- # 保留近 N 天,不包含第 N 天
- keep_days=61
-
- # 删除最近 M 天,即删除是从 M+N 到 N
- del_days=2
-
- # 要删除索引名,留空则使用*-日期(*-2022.01.20)
- # index_name_list=("jdxx_cp_admin" "jdxx_cp_ngnix" "jdxx_fanti_cp_admin" "jdxx_fanti_cp_nginx" "jzxjz_cp_admin" "jzxjz_cp_ngnix" "union_admin" "union_fanti_admin" "union_fanti_nginx" "union_fanti_server_sdk" "union_ngnix" "union_server_sdk")
- index_name_list=()
-
- # 访问elasticsearch的证书路径
- es_ssl='/usr/share/elasticsearch/logstash.pem'
-
- # es信息
- es_url='https://192.168.230.72:9200'
- es_user_passwd='elastic:iOgWcdovTlcQUSeRAhh3'
-
- function del_index_name() {
- for i in $(seq ${keep_days} $((${keep_days}+${del_days})));
- do
- # 要删除的日期
- del_day=$(date -d "$i day ago" +%Y.%m.%d)
-
- # 判断索引列表长度是否为0,为0直接 *-日期
- if [[ ${#index_name_list[@]} -eq 0 ]] ; then
- # 创建删除列表数组,把"*-${del_day}"值添加进列表中
- del_index_list=( "${del_index_list[@]}" "*-${del_day}")
- else
- for index in "${index_name_list[@]}"
- do
- del_index_list=( "${del_index_list[@]}" "${index}-${del_day}")
- done
- fi
- done
-
- # 循环删除索引
- for index_name in "${del_index_list[@]}"
- do
- echo -ne "\e[33;1m删除索引: ${index_name} 删除状态: \e[0m"
- # curl -XDELETE "http://192.168.240.208:9200/${index_name}"
- curl -s -XDELETE "${es_url}/${index_name}" --cacert "${es_ssl}" --user ${es_user_passwd}
- echo ""
- done
- }
-
- del_index_name
-
- EOF
-
- chmod +x /data/software/crond/delete_es_index.sh
配置定时任务
- crontab -e
-
- 0 3 * * * /data/software/crond/delete_es_index.sh &> /dev/null