A tool to manage Elastic snapshots and data streams, which creates snapshots, and optionally deletes data streams and old snapshots according to configurable rules.
Tested with Elasticsearch version 9.0.0
- Creates one snapshot per data stream, which simplifies restoring specific data streams individually.
- Optionally deletes old snapshots.
- Optionally deletes data streams after successful snapshots.
- Supports dry-run mode to simulate operations.
- Parallel execution configurable via environment variable.
- Identifies all data streams matching a defined pattern.
- Checks the age of each data stream.
- If a data stream is older than ELASTIC_MIN_DAYS_TO_SNAPSHOT days:
- A snapshot is created using the data stream name.
- If ELASTIC_DELETE_DATA_STREAM_AFTER_SNAPSHOT=true, the data stream is deleted after the snapshot.
- If ELASTIC_DELETE_OLD_SNAPSHOTS=true, snapshots older than ELASTIC_MIN_DAYS_TO_DELETE_SNAPSHOT days are deleted.
- Python >= 3.12.
- Elasticsearch 9.0.0 (or compatible).
Variable | Description | Required |
---|---|---|
ELASTIC_TARGET |
URL of your Elasticsearch instance (e.g. https://elastic.example.com ) |
Yes |
ELASTIC_USER |
Username for Elasticsearch authentication | Yes |
ELASTIC_PASS |
Password for Elasticsearch authentication | Yes |
ELASTIC_REPOSITORY_NAME |
Name of the snapshot repository | Yes |
ELASTIC_DATA_STREAM_PATTERN |
Pattern used to match data streams (e.g. logs-* ) |
Yes |
ELASTIC_MIN_DAYS_TO_SNAPSHOT |
Minimum age (in days) of data streams to create a snapshot | Yes |
ELASTIC_DELETE_OLD_SNAPSHOTS |
Enable deletion of old snapshots (default: false) | No |
ELASTIC_MIN_DAYS_TO_DELETE_SNAPSHOT |
Minimum age (in days) of snapshots to be deleted | Yes (if ELASTIC_DELETE_OLD_SNAPSHOTS=true ) |
ELASTIC_DELETE_DATA_STREAM_AFTER_SNAPSHOT |
Delete the data stream after a successful snapshot (default: false) | No |
MAX_WORKERS |
Number of parallel workers for snapshot and delete operations (default: 4) | No |
ELASTIC_TARGET=https://elastic.example.com
ELASTIC_USER=elastic
ELASTIC_PASS=your-secure-password
ELASTIC_REPOSITORY_NAME=your-elastic-repository-name
ELASTIC_DATA_STREAM_PATTERN=logs-*
ELASTIC_DELETE_DATA_STREAM_AFTER_SNAPSHOT=false
ELASTIC_MIN_DAYS_TO_SNAPSHOT=14
ELASTIC_DELETE_OLD_SNAPSHOTS=false
ELASTIC_MIN_DAYS_TO_DELETE_SNAPSHOT=30
MAX_WORKERS=4
git clone https://github.com/alexsanderp/elastic-datastream-snapshots.git
cd elastic-datastream-snapshots
cd src
pip install -r requirements.txt
cp .env.example .env # Fill in your environment details
python main.py
To simulate the operations without making actual changes (dry run mode):
python main.py --dry-run
docker pull alexsanderp/elastic-datastream-snapshots:latest
docker run --rm --env-file .env alexsanderp/elastic-datastream-snapshots:latest
To run the tests:
cd src
pip install -r requirements-test.txt
pytest
- In dry-run mode, no snapshots or data streams are created, deleted, or modified.
- The options
ELASTIC_DELETE_OLD_SNAPSHOTS
andELASTIC_DELETE_DATA_STREAM_AFTER_SNAPSHOT
are disabled by default, ensuring safety by avoiding unintended deletions.
Contributions are welcome! Please open issues or submit pull requests for new features, bug fixes, or improvements. See CONTRIBUTING.md for guidelines.
This project is licensed under the MIT License — see the LICENSE file for details.
For questions or feedback, feel free to open an issue on GitHub.