This Bash script allows you to duplicate an existing Elasticsearch index, reindex the data to the new index, and optionally move an alias from the old index to the new index. This script provides a convenient way to manage your Elasticsearch indices and aliases with ease.
- Duplicate an existing Elasticsearch index with all settings and mappings.
- Reindex data from the source index to the new index.
- Update aliases: Move an alias from the old index to the new index.
- Elasticsearch running and accessible.
jq
installed (optional, for JSON pretty-printing).
-
Clone the Repository (if this script is part of a repository):
git clone https://github.com/emreyildirim53/elastic-search-index-duplicator.git cd elastic-search-index-duplicator
-
Make the Script Executable:
chmod +x index_duplicator.sh
./index_duplicator.sh [old_index_name] [new_index_name] [alias_name]
old_index_name
: The name of the existing index (source).new_index_name
: The name of the new index to be created (destination).alias_name
: The alias to be moved to the new index.
./index_duplicator.sh my_old_index my_new_index my_alias
- Elasticsearch Connection: Connects to the specified Elasticsearch instance.
- Index Existence Check: Verifies the existence of the source index.
- Settings and Mappings Extraction: Extracts settings and mappings from the source index.
- Index Creation: Creates the new index with the extracted settings and mappings.
- Data Reindexing: Reindexes data from the source index to the new index.
- Alias Management: Moves the alias from the old index to the new index.
When the script runs successfully, it outputs a summary of operations:
==============================================================
Elasticsearch Index Operation
==============================================================
Status : SUCCESS
Operation Summary:
--------------------------------------------------------------
Source Index : my_old_index
Target Index : my_new_index
Alias Updated : my_alias
--------------------------------------------------------------
Details:
The alias 'my_alias' has been successfully reassigned from
the old index 'my_old_index' to the new index 'my_new_index'.
All relevant data has been successfully reindexed.
No errors were encountered during this operation.
==============================================================
- Elasticsearch: Ensure that your Elasticsearch instance is up and running, and accessible via the
ELASTIC_HOST
variable in the script. jq
(optional): Used for pretty-printing JSON responses.
- The script checks if the Elasticsearch host is reachable and if the specified indices exist.
- Proper error messages are displayed if an index is not found or if there are connectivity issues.
- Elasticsearch Host: Modify the
ELASTIC_HOST
variable in the script to point to your Elasticsearch instance if different from the default. - Index and Alias Names: Use the script's parameters to specify your index and alias names.
This script is open-source and available under the MIT License.
Contributions are welcome! Please submit a pull request or open an issue to discuss changes.
- Make sure to have proper backups before running this script on a production Elasticsearch cluster.
- Test the script in a development or staging environment if you're uncertain about its effects.
For further questions or support, please open an issue on this repository.