Skip to content

Commit

Permalink
added snapshot sample and updated snapshot.md
Browse files Browse the repository at this point in the history
Signed-off-by: Raman Saparkhan <[email protected]>
  • Loading branch information
roma2023 committed Sep 7, 2023
1 parent 722a173 commit cecb9b5
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Improved CI performance of integration with unreleased OpenSearch ([#318](https://github.com/opensearch-project/opensearch-py/pull/318))
- Added k-NN guide and samples ([#449](https://github.com/opensearch-project/opensearch-py/pull/449))
- Added the ability to run tests matching a pattern to `.ci/run-tests` ([#454](https://github.com/opensearch-project/opensearch-py/pull/454))
- Added new guide: `snapshot.md` for Snapshot API. ([#486](https://github.com/opensearch-project/opensearch-py/pull/429))
- Added a guide for taking snapshots ([#486](https://github.com/opensearch-project/opensearch-py/pull/429))
### Changed
- Moved security from `plugins` to `clients` ([#442](https://github.com/opensearch-project/opensearch-py/pull/442))
- Updated Security Client APIs ([#450](https://github.com/opensearch-project/opensearch-py/pull/450))
Expand Down
44 changes: 40 additions & 4 deletions guides/snapshot.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# Table of Contents
- [Snapshot Actions](#snapshot-actions)
- [Setup](#setup)
- [API Actions](#api-actions)
- [Create Snapshot Repository](#create-snapshot-repository)
- [Create Snapshot](#create-snapshot)
- [Verify Snapshot Repository](#verify-snapshot-repository)
- [Delete Snapshot](#delete-snapshot)
- [Restore Snapshot](#restore-snapshot)
- [Get Snapshot Status](#get-snapshot-status)
- [Clone Snapshot](#clone-snapshot)
- [Get Snapshot](#get-snapshot)
- [Get Repository](#get-repository)
- [Repository Analyze](#repository-analyze)
- [Cleanup](#cleanup)

# Snapshot Actions
In this guide, we will look at some snapshot actions that allow you to manage and work with snapshots of your indices.

Expand All @@ -7,10 +23,16 @@ Let's create a client instance, and an index named `movies`:
```python
from opensearchpy import OpenSearch

host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.

client = OpenSearch(
hosts=['https://admin:admin@localhost:9200'],
use_ssl=True,
verify_certs=False
hosts = [{'host': host, 'port': port}],
http_auth = auth,
use_ssl = True,
verify_certs = False,
ssl_show_warn = False
)

print(client.info()) # Check server info and make sure the client is connected
Expand All @@ -27,7 +49,12 @@ repo_body = {
"location": "/path/to/repo",
}
}
client.snapshot.create_repository(repository='my_repository', body=repo_body)

# Create the snapshot repository and capture the response
response = client.snapshot.create_repository(repository='my_repository', body=repo_body)

# Print the response to see the result
print(response)
```

### Create Snapshot
Expand All @@ -42,6 +69,12 @@ The `verify_repository` API action allows you to verify a snapshot repository. V

```python
response = client.snapshot.verify_repository(repository='my_repository')

# Print the HTTP status code
print("HTTP Status Code:", response.status_code)

# Print the response content
print("Response Content:", response.content)
```

### Delete Snapshot
Expand Down Expand Up @@ -82,6 +115,9 @@ response = client.snapshot.get(
repository='my_repository',
snapshot='my_snapshot'
)

# Print the response to see the result
print(response)
```

## Get Repository
Expand Down
63 changes: 63 additions & 0 deletions samples/snapshot/snapshot_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

from opensearchpy import OpenSearch

# connect to OpenSearch

host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.

client = OpenSearch(
hosts = [{'host': host, 'port': port}],
http_auth = auth,
use_ssl = True,
verify_certs = False,
ssl_show_warn = False
)

# Create an index

index_name = "test-snapshot"
client.indices.create(index = index_name)

# Create a snapshot repository

repo_body = {
"type": "fs", # Replace 'fs' with the appropriate repository type
"settings": {
"location": "/path/to/repo", # Replace with the desired repository location
}
}

repository_name = 'my_repository'
response = client.snapshot.create_repository(repository = repository_name, body = repo_body)

print(response)

# Create a snapshot

snapshot_name = 'my_snapshot'
response = client.snapshot.create(repository = repository_name, snapshot = snapshot_name, body={"indices": index_name})

print(response)

# Get Snapshot Information

snapshot_info = client.snapshot.get(repository = repository_name, snapshot = snapshot_name)

print(snapshot_info)

# Clean up - Delete Snapshot and Repository

client.snapshot.delete(repository = repository_name, snapshot = snapshot_name)
client.snapshot.delete_repository(repository = repository_name)

# Clean up - Delete Index

client.indices.delete(index = index_name)

0 comments on commit cecb9b5

Please sign in to comment.