# How to store a backup (snapshot) of my indexes to local storage using python in elasticsearch and then also be able to restore them?

**URL:** <https://discuss.elastic.co/t/how-to-store-a-backup-snapshot-of-my-indexes-to-local-storage-using-python-in-elasticsearch-and-then-also-be-able-to-restore-them/210940>\
**Category:** Elasticsearch\
**Created:** [December 6, 2019, 9:17pm UTC](https://discuss.elastic.co/t/how-to-store-a-backup-snapshot-of-my-indexes-to-local-storage-using-python-in-elasticsearch-and-then-also-be-able-to-restore-them/210940 "2019-12-06T21:17:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hjiop\_oop](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hjiop\_oop](https://discuss.elastic.co/u/hjiop_oop)\
**Post date:** [December 6, 2019, 9:17pm UTC](https://discuss.elastic.co/t/how-to-store-a-backup-snapshot-of-my-indexes-to-local-storage-using-python-in-elasticsearch-and-then-also-be-able-to-restore-them/210940/1 "2019-12-06T21:17:24Z")

</div>

Hello,

I know that I need to create a snapshot (?) for doing so, but can someone please tell me how?

Say I have indexes: indexA and indexB

How do I go about storing a snapshot (containing these indexes) to local storage using elasticsearch on python?

It would help if you could also tell me how to restore them.

---

<div class="post-metadata">

**Author:** ![rschirin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rschirin/32/45283_2.png) [@rschirin](https://discuss.elastic.co/u/rschirin)\
**Post date:** [December 7, 2019, 7:31am UTC](https://discuss.elastic.co/t/how-to-store-a-backup-snapshot-of-my-indexes-to-local-storage-using-python-in-elasticsearch-and-then-also-be-able-to-restore-them/210940/2 "2019-12-07T07:31:36Z")

</div>

first of all, you should indicate your ES version.  
anyway, you can follow the guide [here](https://elasticsearch-py.readthedocs.io/en/master/api.html?highlight=shrink#snapshot)

basically, you have to create an `Elasticsearch connection object` (i.e. `es`) and then you can use the standard function `es.snapshot.create` and `es.snapshot.restore`

for instance:

> es.snapshot.create(repository=repository,snapshot=sname,wait\_for\_completion="true",body=settingsSnap)

where:

> repository = "myfsrepo"  
> today = datetime.datetime.today()  
> timestamp = today.strftime("%Y.%m.%d")  
> snapshot = "snaptest\_" + timestamp  
> settingsSnap = {  
> "indices": indexA,  
> "ignore\_unavailable": "true",  
> "include\_global\_state": "true"  
> }

to restore it, you can work in a similar way.  
Remember that before to work with Python, you have to configure the `path.repo` key value in your` elasticsearch.yml` and create your repository (maybe using Kibana if you prefer).

---

<div class="post-metadata">

**Author:** ![hjiop\_oop](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hjiop\_oop](https://discuss.elastic.co/u/hjiop_oop)\
**Post date:** [December 10, 2019, 5:12pm UTC](https://discuss.elastic.co/t/how-to-store-a-backup-snapshot-of-my-indexes-to-local-storage-using-python-in-elasticsearch-and-then-also-be-able-to-restore-them/210940/3 "2019-12-10T17:12:07Z")

</div>

This is my version information:

```
{'name': '', 'cluster_name': '', 'cluster_uuid': '', 'version': {'number': '6.7.2', 'bui
ld_flavor': 'oss', 'build_type': 'tar', 'build_hash': '', 'build_date': :
  , 'build_snapshot': False, 'lucene_version': '7.7.0', 'minimum_wire_compatibilit
y_version': '5.6.0', 'minimum_index_compatibility_version': '5.0.0'}, 'tagline': 'You Know, for Search'}

```

Will the above work for storing it on local storage somewhere? For eg. if I wanted to store the ES indexes into /home/usr1/indexes/ ?

---

<div class="post-metadata">

**Author:** ![hjiop\_oop](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hjiop\_oop](https://discuss.elastic.co/u/hjiop_oop)\
**Post date:** [December 10, 2019, 5:32pm UTC](https://discuss.elastic.co/t/how-to-store-a-backup-snapshot-of-my-indexes-to-local-storage-using-python-in-elasticsearch-and-then-also-be-able-to-restore-them/210940/4 "2019-12-10T17:32:54Z")

</div>

I tried this:

```
snapshot_body = {
"type": "url",
"settings": {
        "url": "http://download.elasticsearch.org/definitiveguide/sigterms_demo/"
    }
}
body = {"snapshot": snapshot_body}
es.snapshot.create_repository(repository='test', body=snapshot_body)
index_body = {
  "indices":"index1,index2" 
}
es.snapshot.create(repository='test', snapshot='my_snapshot', body=index_body)

```

getting error:

`elasticsearch.exceptions.TransportError: TransportError(500, 'repository_exception', "[test] file url [http://download.elasticsearch.org/definitiveguide/sigterms_demo/] doesn't match any of the locations specified by path.repo or repositories.url.allowed_urls")`

Am I creating the snapshot correctly?

I want to store the data from index1,index2 into a path /home/usr1/indexes/

I know that I can manually scroll through the index data and then save it but I also read somewhere that it is not recommended. Can someone please give me a small working example which they tested on their side?

---

<div class="post-metadata">

**Author:** ![hjiop\_oop](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hjiop\_oop](https://discuss.elastic.co/u/hjiop_oop)\
**Post date:** [December 13, 2019, 10:37pm UTC](https://discuss.elastic.co/t/how-to-store-a-backup-snapshot-of-my-indexes-to-local-storage-using-python-in-elasticsearch-and-then-also-be-able-to-restore-them/210940/5 "2019-12-13T22:37:54Z")

</div>

Can someone please answer this query? I don't think the response appropriately answers the question.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 14, 2019, 7:13am UTC](https://discuss.elastic.co/t/how-to-store-a-backup-snapshot-of-my-indexes-to-local-storage-using-python-in-elasticsearch-and-then-also-be-able-to-restore-them/210940/6 "2019-12-14T07:13:50Z")

</div>

Snapshots in Elasticsearch require shared storage which all the nodes in the cluster have access to on the same path. Local storage can only be used if you only have a single nodes/host.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 11, 2020, 7:14am UTC](https://discuss.elastic.co/t/how-to-store-a-backup-snapshot-of-my-indexes-to-local-storage-using-python-in-elasticsearch-and-then-also-be-able-to-restore-them/210940/7 "2020-01-11T07:14:00Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
