Back to Yugabyte Db

Backups

managed/api-examples/python-simple/backups.ipynb

2026.1.0.0-b254.9 KB
Original Source

Backups

Boilerplate - Get session Info

  • Using API key make a request get ascociated session_info
  • session_info contains current user and current customer uuid.
python
import http.client
import json
import os
from pprint import pprint

platform_api_key = os.getenv('YB_API_KEY')
platform_address = os.getenv('API_BASE_URL', "portal.dev.yugabyte.com")

conn = http.client.HTTPConnection(f"{platform_address}")

headers = {
  'Content-Type': "application/json",
  'X-AUTH-YW-API-TOKEN': f"{platform_api_key}"
}

conn.request("GET", "/api/v1/session_info", headers=headers)

res = conn.getresponse()
data = res.read()
session_info = json.loads(data)
pprint(session_info)

Storage Configuration Parameter

  • Now pick a storage config you want to use to store the backups. See ./list-customer-config.ipynb for how to list all configs
python
storage_config_uuid = os.getenv('STORAGE_CONF_UUID',
                                "f5cfe9fb-cd4f-46e7-8ed8-af285b730cc0")

Universe UUID Parameter

uuid of universe to be backed up. See list-universes.ipynb for how to

python
universe_uuid = os.getenv('UNIVERSE_UUID',
                          "303cf5ac-a6f5-492b-b68c-7d4d9633c4cc")

Create one-time (i.e. unscheduled) Whole Universe Backup

  • delete (garbage collect) backup after 10 days (864000000 ms)
python

payload = ('{\n'
           '  "actionType": "CREATE",\n'
           '  "keyspace": "postgres",\n'
           f'  "storageConfigUUID": "{storage_config_uuid}",\n'
           '  "timeBeforeDelete": "864000000",\n'
           '  "sse": "false",\n'
           '  "transactionalBackup": "false",\n'
           '  "parallelism": "8",\n'
           '  "backupType": "PGSQL_TABLE_TYPE"\n'
           '}')

url = f"/api/v1/customers/{session_info.get('customerUUID')}" \
      f"/universes/{universe_uuid}/multi_table_backup"
conn.request("PUT", url, payload, headers)

res = conn.getresponse()
yb_task = json.load(res)
pprint(yb_task)

List tables

table_name

  • We assume that you have created table with name "cassandrakeyvalue" using yb-sample-app
python
table_name = "cassandrakeyvalue"

table_name

  • We will list tables and get the uuid of table with name "cassandrakeyvalue"
python
url = f"/api/v1/customers/{session_info.get('customerUUID')}" \
      f"/universes/{universe_uuid}/tables"
conn.request("GET", url, headers=headers)
res = conn.getresponse()
data = res.read()
table_list = json.loads(data)
for table in table_list:
    if table['tableName'] == table_name:
        break
pprint(table)

Create one-time (i.e. unscheduled) single table backup

python
payload = ('{\n'
           '  "actionType": "CREATE",\n'
           f'  "keyspace": "{table.get("keySpace")}",\n'
           f'  "tableName": "{table_name}",\n'
           f'  "storageConfigUUID": "{storage_config_uuid}",\n'
           '  "timeBeforeDelete": "864000000"\n'
           '}')
print(payload)
url = f"/api/v1/customers/{session_info.get('customerUUID')}" \
      f"/universes/{universe_uuid}/tables/{table['tableUUID']}/create_backup"
print(url)
conn.request("PUT", url, payload, headers)

res = conn.getresponse()
yb_task = json.load(res)
pprint(yb_task)

Stop an ongoing backup

python
url = f"/api/v1/customers/{session_info.get('customerUUID')}" \
      f"/backups/{yb_task.get('resourceUUID')}/stop"
print(url)
conn.request("POST", url, "{}", headers=headers)
res = conn.getresponse()
print(json.loads(res.read()))

Delete a the completed backup

python
url = f"/api/v1/customers/{session_info.get('customerUUID')}" \
      f"/backups"
payload = ('{\n'
           f'  "backupUUID": ["{yb_task.get("resourceUUID")}"]\n'
           '}')
print(payload)
conn.request("DELETE", url, payload, headers=headers)
res = conn.getresponse()
print(res.read())

Create Scheduled Whole Universe Backup

  • schedulingFrequency (i.e. take backup every) 1 day (86400000 ms)
  • delete (garbage collect) backup after 10 days (864000000 ms)
python

payload = ('{\n'
           '  "actionType": "CREATE",\n'
           '  "keyspace": "postgres",\n'
           f'  "storageConfigUUID": "{storage_config_uuid}",\n'
           '  "timeBeforeDelete": "864000000",\n'
           '  "sse": "false",\n'
           '  "transactionalBackup": "false",\n'
           '  "parallelism": "8",\n'
           '  "schedulingFrequency": "86400000",\n'
           '  "backupType": "PGSQL_TABLE_TYPE"\n'
           '}')

url = f"/api/v1/customers/{session_info.get('customerUUID')}" \
      f"/universes/{universe_uuid}/multi_table_backup"
conn.request("PUT", url, payload, headers)

res = conn.getresponse()
schedule_info = json.load(res)
pprint(schedule_info)

Delete backup Schedule

python
url = f"/api/v1/customers/{session_info.get('customerUUID')}" \
      f"/schedules/{schedule_info.get('scheduleUUID')}"
conn.request("DELETE", url, headers=headers)
res = conn.getresponse()
print(res.read())