Building GUI based tools rather than scripting are conceived as complex and time-consuming, but it is not. Here in this blog, I am going to show you how easy it is to build a 100% Python based GUI tool which can backup SAP HANA systems using Pure storage snapshots. For more information regarding the SAP HANA system copy using snapshots please refer to my another blog: “How to “automate” SAP HANA Backups using Storage Snapshots on Pure Storage”
I have added this entire code to Github, and here is how it looks like in action. Here I am selecting the SAP HANA mode and then selecting the operation –> Selection of SID –> Check which creates connection objects to Pure Storage FlashArray and SAP HANA system and then Run which takes SAP HANA system backup using snapshots.
These are the building blocks which I have used to build this tool:
For GUI – Python module ttk (Tkinter):
I have used Python module ttk (Tkinter) which is a python binding to the Tk GUI toolkit. Using this you can create GUI widgets like the radio button, drop down, progress bars, buttons, tabs etc.
For eg. to add a radio button, you need to import the Radiobutton
from tkinter import Radiobutton
Radiobutton(opsFrame, text=” Backup using Snapshots “, variable=ops, value=1, command=operation).grid(row=0,sticky=W+E+N+S,ipadx=10,ipady=5)
This will add a radio button to the frame mentioned, with the text mentioned. Assign the radio button to a variable in your Python code so that it can be manipulated or used accordingly. Also, you need to mention the location of this radio button which is the last part using the grid function.
For connecting to SAP HANA – Database Client for Python
For connecting to SAP HANA for eg. to put it in “Create Snapshot” mode and closing the SAP HANA snapshot mode etc, I have used database client for Python to connect to SAP HANA.
The function pyhdb.connect creates a new database session and returns a new Connection instance. Please note that port isn’t the instance number of your SAP HANA database. The SQL port of your SAP HANA is made up of 3<instance-number>15 for example the port of the default instance number 00 is 30015.
The pyhdb only supports the user and password authentication method. If you need another authentication method like SAML or Kerberos then please open a GitHub issue. Also, there is currently no support for encrypted network communication between client and database.
With the method cursor of your Connection object you create a new Cursor object. This object is able to execute SQL statements and fetch one or multiple rows of the resultset from the database.
For eg. Here is the code to connect to SAP HANA system and execute a SQL statement to put SAP HANA in “Create snapshot mode”.
import pyhdb
connection = pyhdb.connect( host=”saphananode”, port=30015, user=”user”, password=”secret” )
cursor = connection.cursor()
cursor.execute(“BACKUP DATA CREATE SNAPSHOT”)
connection.close()
This package PyHDB for SAP HANA can be downloaded from the Github.
For connecting to Pure Storage – Pure Storage REST Client
This Python package is used to access a Pure Storage Flash Array using a REST API. It communicates with the array using the python requests HTTP library. You can find more information regarding this package at
https://pythonhosted.org/purestorage/index.html
Below are the code snippets to show how to connect to a Pure Storage Flash Array//m: Start by importing the purestorage module and then FlashArray object.
import purestorage
from purestorage import FlashArray
REST sessions are automatically established when a FlashArray object is instantiated. To instantiate a FlashArray object, provide the IP address or domain name of the target array as well as a username and password, or API token.
array = purestorage.FlashArray(“localhost”, “pureuser”, “pureuser”)
OR
array = purestorage.FlashArray(“localhost”,api_token=”6e1b80a1-cd63-de90-b74a-7fc16d034016″)
Then you can use this FlashArray object to create volumes, take snapshots, copy volumes etc. Below is the code snippet to take the snapshot of the volume “hanadatavolPSD”
array.create_snapshot(“hanadatavolPSD”)
One of the very important step is to freeze the data volumes file system for SAP HANA before taking the snapshot. I have done it using PYRO module to call the remote scripts which will freeze the XFS files system for data volume. PYRO is a library that enables you to build applications in which objects can talk to each other over the network, with minimal programming effort. You can just use normal Python method calls to call objects on other machines. Pyro is written in 100% pure Python and therefore runs on many platforms and Python versions, including Python 3.x.
Rather than using PYRO which can be notoriously difficult to configure, deploy the script on SAP HANA host server itself so you don’t have to use the PYRO.
Please find the full python code in Github:
https://github.com/krishnasatyavarapu/SAP_HANA/blob/master/PureFlashManager_githubVersion.py
Happy coding and never ever stop coding