Nx Witness allows third-party developers to back up and restore their system database using the API. In this article, we provide basic code examples in Python 3 to demonstrate a method for accomplishing these tasks.
Examples and explanations are provided. Refer to Server HTTP REST API for more information on our APIs and on accessing our API documentation.
Prerequisites
Authentication
Nx Witness uses by default bearer/session token authentication. We perform the API requests below as local users.
The examples in this article are based on the code in the Nx Github repository. Refer to Nx Witness Authentication for a detailed description.
How to Create a System Backup
The API requests for creating a system backup accomplish this by creating a binary dump of your system database, which you would need to capture into a file.
The two code examples below are designed to create a backup of your Nx Witness system in the form of a binary file, which will be named as systembackup_{backup_time}.db, where the {backup_time} portion will reflect the time at which the script was run.
For the new API, you will use GET/rest/v3/system/database.
get_method_header = create_header(primary_token)
system_backup = request_api(LOCAL_URL, f'/rest/v1/system/database', 'GET', verify=False, headers=get_method_header)
delete_method_header = create_header(secondary_token)
request_api(LOCAL_URL, f'/rest/v1/login/sessions/{secondary_token}', 'DELETE', verify=False, headers=delete_method_header)
backup_time = datetime.now().strftime('%m-%d-%Y-%H-%M-%S')
with open(f'systembackup_{backup_time}.db', 'bw') as backup:
backup.write(base64.b64decode(system_backup["data"]))
How to Restore a System Backup
The API request for restoring a system accomplish this by loading the system database from the database file that was provided in the backup request.
The code examples below assume your system backup is in database file format. To use them, replace ‘FILENAME’ with the name of the backup file you want to restore your system database with.
After restoring the system, Nx Witness Server will restart automatically. For the changes to appear in Nx Witness Desktop, restart the client.
To restore the backup, you use POST/rest/v3/system/database.
filename = 'FILENAME' # replace FILENAME with your backup file
with open(f'{filename}', 'br') as recovery:
recovery_file = recovery.read()
recovery_file = json.dumps({"data": base64.b64encode(recovery_file).decode()})
get_method_header = create_header(primary_token)
request_api(LOCAL_URL, f'/rest/v1/system/database', 'POST', verify=False, headers={**get_method_header, 'Content-type':'application/json'}, data=recovery_file)
delete_method_header = create_header(secondary_token)
Comments
0 comments
Article is closed for comments.