Use elasticsearch to delete index data regularly

Use elasticsearch to delete index data regularly

1. Sometimes we use ES

Due to limited resources or business needs, we only want to save data from the most recent period, so it is necessary to delete data at a scheduled time.

2. Write a script

vim del_es_by_day.sh
#!/bin/bash
#Scheduled deletion of elasticsearch index#author menard 2019-3-25
date=`date -d "-7 days" "+%Y.%m.%d"`
/usr/bin/curl -v --user elastic:password -XDELETE "http://192.168.10.201:9200/*-$date"

Add executable permissions chmod +x del_es_by_day.sh

3. Create an index for testing

put test-2019.03.18
put index-2019.03.18 

4. Execute the script test results and you can see that the deletion is successful

5. Do scheduled tasks

crontab -e
00 01 * * * /workspace/script/del_es_by_day.sh

Supplement: Elasticsearch scheduled backup index data and recovery

Scheduled backup script

Linux scheduled tasks use cron service to perform

Writing cron expressions for scheduled tasks

crontab -e #Enter cron scheduled task editing

Scheduled tasks

*/1 * * * * /opt/scheduler/es_bk.sh >> /opt/scheduler/bk_log.txt 2>&1

The es_bk.sh script in the /opt/scheduler/ directory is executed every 1 minute, and the data content is written to the bk.log.txt file in the /opt/scheduler directory.

Check the contents of the es_bk.sh script

#!/bin/bash
echo '==================================start========================================'
#Delete the backup snapshot curl -i -X ​​DELETE localhost:9200/_snapshot/es_backup/snapshot01
#Backup again curl -i -X ​​PUT localhost:9200/_snapshot/es_backup/snapshot01
echo '===================================end=========================================='

ES backup data requires a snapshot of the index data to be backed up. A snapshot name needs to be specified, and the same snapshot cannot be used. Therefore, the old snapshot needs to be deleted before each backup and then backed up again.

ES backup and recovery

Create a backup repository (directory)

mkdir -p /bk/es/data
#Change permissions chmod -R 777 bk

Modify the elasticsearch.yml file and specify the warehouse location

Send a request to initialize the repository

curl -i -H ""'Content-Type:application/json;charset=UTF-8'"" -X PUT --data '{"type": "fs","settings": {"location": "/bk/es/data"}}' localhost:9200/_snapshot/es_backup

es_backup is the backup namespace and can be specified at will

Creating the first snapshot

curl -i -X ​​PUT localhost:9200/_snapshot/es_backup/snapshot01

We will use es_backup to back up all index data of es to snapshot01. Of course, we can also back up only the specified index.

curl -i -H ""'Content-Type:application/json;charset=UTF-8'"" -X PUT --data '{"indices": "'bk_user_index_server'"}' localhost:9200/_snapshot/es_backup/snapshot01

Restart scheduled tasks

systemctl restart cron

Restore Index

Restore Assignment

curl -i -H ""'Content-Type:application/json;charset=UTF-8'"" -X POST --data '{"indices": "'bk_user_index_server'"}' localhost:9200/_snapshot/es_backup/snapshot01/_restore

Restore All

curl -i -X ​​POST localhost:9200/_snapshot/es_backup/snapshot01/_restore

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Elasticsearch source code analysis index action implementation
  • Detailed explanation of Elasticsearch Recovery index shard allocation
  • Elasticsearch document index basic operations add, delete, modify and query examples
  • Elasticsearch inverted index and index operations
  • ElasticSearch reasonable allocation of index fragmentation principle
  • ElasticSearch adds index code example analysis
  • Elasticsearch index data function source code example

<<:  Detailed explanation of MySQL index selection and optimization

>>:  CSS simulates float to achieve the effect of center text surrounding the image on the left and right

Recommend

Vue implements graphic verification code

This article example shares the specific code of ...

CSS uses radial-gradient to implement coupon styles

This article will introduce how to use radial-gra...

Design a simple HTML login interface using CSS style

login.html part: <!DOCTYPE html> <html l...

Analysis and solution of data loss during Vue component value transfer

Preface In the previous article Two data types in...

Things to note when writing self-closing XHTML tags

The img tag in XHTML is so-called self-closing, w...

N ways to achieve two-column layout with CSS

1. What is a two-column layout? There are two typ...

Develop a vue component that encapsulates iframe

Table of contents 1. Component Introduction 2. Co...

Summary of 10 advanced tips for Vue Router

Preface Vue Router is the official routing manage...

React dva implementation code

Table of contents dva Using dva Implementing DVA ...

Summary of the three stages of visual designer growth

Many people have read this book: "Grow as a ...

One minute to experience the smoothness of html+vue+element-ui

Technology Fan html web page, you must know vue f...

W3C Tutorial (6): W3C CSS Activities

A style sheet describes how a document should be ...

Detailed explanation of various loop speed tests in JS that you don’t know

Table of contents Preface 1. for loop 2. while lo...