Reply

API call to retrieve Backup DB

New Member
Posts: 2
1471     1

Hi all and thanks in advance for any replies.

 

First time poster, long time subscriber.

 

Anyway, I am trying to retrieve the Backup DB .csv via api but keep getting the below error

C:\Users\e157716\PycharmProjects\SunchProdAndDev\venv\lib\site-packages\urllib3\connectionpool.py:1100: InsecureRequestWarning: Unverified HTTPS request is being made to host 'sdcscadnsgmavip.swacorp.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
Error creating a database backup: 400

Process finished with exit code 0

My code is as follows:

import requests

# Infoblox API information
infoblox_url = 'xxx' # Adjust the URL and version as needed
infoblox_user = 'yyy'
infoblox_password = 'zzz'

# Function to create an Infoblox database backup
def create_database_backup():
endpoint = '/grm/backup'
url = infoblox_url + endpoint

# Prepare a request to create a database backup
data = {
"comment": "Database backup created via API" # Optional comment
}

response = requests.post(url, auth=(infoblox_user, infoblox_password), json=data, verify=False) # Disable SSL verification; use it with caution

if response.status_code == 200:
response_data = response.json()
backup_url = response_data['url']

# You can download the backup file if needed
# backup_response = requests.get(backup_url, auth=(infoblox_user, infoblox_password), verify=False)
# Handle the downloaded backup file as required
print("Database backup created successfully.")
else:
print(f"Error creating a database backup: {response.status_code}")

if __name__ == "__main__":
create_database_backup()


We are running: 8.6.3-51135-1241097029df

Re: API call to retrieve Backup DB

Moderator
Moderator
Posts: 315
1471     1

Looks like your grid is using self-signed certificates. That's not a problem but you will need to set up a requests session, and supply some parameters to bypass SSL certificate validation.

 

Here's a quick example of setting up a session, and getting the current WAPI version

global SESSION
SESSION = requests.session()
get_version = f'https://{gm}/wapi/v1.0/?_schema'
json_headers = {'content-type': 'application/json'}


resp = SESSION.get(get_version, headers=json_headers,
	auth=authentication,
	verify=False,
	timeout=600)

json_data = json.loads(resp.text)

# Save the authentication cookie for use in subsequent requests.
ibapauth_cookie = resp.cookies['ibapauth']

# Use the ibapauth cookie to authenticate instead of userid/password.
global COOKIE
COOKIE = {'ibapauth': ibapauth_cookie}

# Get supported versions
versions = json_data["supported_versions"]
versions.sort(key=lambda s: list(map(int, s.split('.'))))  # Sort Versions

# Get the latest version
global WAPI
WAPI = versions[-1]
log.info("Using wapi version %s", WAPI)
log.info("Capturing supported objects for version %s", WAPI)

You will need to generate the backup file first, using the fileop function.

 

Here's an example with curl. 

# Download Grid Backup


# Request grid backup generation

curl -k -u admin:infoblox -X POST 'https://192.168.1.2/wapi/v2.12/fileop?_function=getgriddata&type=BACKUP'

{
    "token": "eJytjj0LwjAURf9KyWyTvrSExq1SBUFaEMExpE2swX6ZRlDE/24z6Ori+M7lnnefSN9HYx/CmU6j\nZQCMp8CiNKaYA0toxBcButl2jtDZuXFaEgKcYmApBkyJZ0IZq2snTqbVwgzE6qswKszLY7ErszyE\niCYwq2LKIWFRTJR0spKTxpW8oNnvb6H7elCmb/yn1bb48m5QfhjKs0Mm9uvNJ/CMTG6wstHEdeN/\nphjlvb9q6PUGzdpb/w==\n", 
    "url": "https://192.168.1.2/http_direct_file_io/req_id-DOWNLOAD-1024142032914603/database.bak"
}

# Download file from URL

curl -k -u admin:infoblox -H "Content-type:application/force-download" -O 'https://192.168.1.2/http_direct_file_io/req_id-DOWNLOAD-1024142032914603/database.bak'
 
# And then close the file using the token
 
curl -k1 -u admin:infoblox -X POST 'https://192.168.1.2/wapi/v2.12/fileop?_function=downloadcomplete' \
-H "Content-Type: application/json" -d \
'{
  "token": "eJytjj0LwjAURf9KyWyTvrSExq1SBUFaEMExpE2swX6ZRlDE/24z6Ori+M7lnnefSN9HYx/CmU6j\nZQCMp8CiNKaYA0toxBcButl2jtDZuXFaEgKcYmApBkyJZ0IZq2snTqbVwgzE6qswKszLY7ErszyE\niCYwq2LKIWFRTJR0spKTxpW8oNnvb6H7elCmb/yn1bb48m5QfhjKs0Mm9uvNJ/CMTG6wstHEdeN/\nphjlvb9q6PUGzdpb/w==\n"
}'

 

 

Re: API call to retrieve Backup DB

New Member
Posts: 2
1471     1
Wow, thanks for the quick reply.

Could I have an example with Pythin?

Nuno
[Image result for Southwest Airlines heart clipart]

******* CONFIDENTIALITY NOTICE *******

This e-mail message and all attachments transmitted with it may contain legally privileged and confidential information intended solely for the use of the addressee. If the reader of this message is not the intended recipient, you are hereby notified that any reading, dissemination, distribution, copying, or other use of this message or its attachments is strictly prohibited. If you have received this message in error, please notify the sender immediately and delete this message from your system. Thank you.

Re: API call to retrieve Backup DB

New Member
Posts: 5
1471     1

This works - could add session stuff:

 

"""
This script downloads an Infoblox database backup and saves it to the local file system.
It connects to an Infoblox Grid Manager using the RESTful API and authenticates using
the provided credentials. The backup file is downloaded using the URL received from
the response of initiating the backup session. The file is then saved locally with a
timestamped filename. Finally, a POST request is sent to trigger the download complete
using the received token.

"""

import requests
import json
import time


# Disable SSL warnings
requests.packages.urllib3.disable_warnings()

# Infoblox Grid variables
gm_hostname = "gridmaster.domain.org"
gm_url = f"https://{gm_hostname}/wapi/v2.11"
gm_user = "admin"
gm_password = "your-pwd"
now = time.strftime("%Y-%m-%d")
outputfile = f"infoblox_backup_{now}.bak"
headers = {"content-type": "application/json"}


# Function to create an Infoblox database backup
def create_infoblox_backup():
"""Function to download Infoblox database backup"""

# Prepare a request to create a database backup
# print('\n' + gm_url + '/fileop?_function=getgriddata' + '\n')
payload = {"type": "BACKUP"}
data = {"comment": "Database backup created via API"} # Optional comment

print("Initiating the backup on server ...\n")
response = requests.post(
f"{gm_url}/fileop?_function=getgriddata",
auth=(gm_user, gm_password),
data=json.dumps(payload),
headers=headers,
verify=False,
)

if response.status_code == 200:
print("Creation of the backup file was successful ...\n")
response_dict = response.json()
backup_url = response_dict["url"]
backup_token = response_dict["token"]
print("backup URL: ", backup_url + "\n")
print(backup_token + "\n")

# Step 2: You can download the backup file if needed using the url
print( "Initiating download of backup file from URL provided in API response ...\n" )
download_headers = {"content-type": "application/force-download"}
download_response = requests.get(
backup_url,
auth=(gm_user, gm_password),
stream=True,
headers=download_headers,
verify=False,
)
open(outputfile, "wb").write(download_response.content)

print("Telling Infoblox download is complete, start cleanup ...\n")
close_response = requests.post(
f"https://{gm_hostname}/fileop?_function=downloadcomplete",
auth=(gm_user, gm_password),
data=json.dumps(backup_token),
headers=headers,
verify=False
)

print("Database backup created and downloaded successfully.\n")

else:
print(f"Error creating a database backup: {response.status_code}")


if __name__ == "__main__":
create_infoblox_backup()
Showing results for 
Search instead for 
Did you mean: 

Recommended for You