SAP HANA XS offers the possibility to schedule applications as jobs. As database backups are triggered by executing SQL commands an application can be built which calls these SQL commands. This blog will provide an overview about howto write a simple XS application which creates a database backup and to schedule it as an XS job.

The first step is to install the SAP HANA database server. In this example SAP HANA SP 9 is used.
InstallSoftware.PNG

After the installation has finished successfully the Service for the XS engine is already running. The status can be checked within SAP HANA Studio or on the command line using the command “HDB info” as user gtiadm.

RunningXsEngine.PNG

To make a first call to the the XS engine use your favorite browser and type in the URL below.

XsEngineInBrowser.PNG

So, the database is succesfully installed, up and running. The XS engine is also working.
The next step is to create an SQL user whose permissions are limited to do backups and to work with the XS engine.

In this example the name of this user is BACKUP_OPERATOR.

CreateBackupOperator.PNG

To verify that the user is able to create backups, logon to the database using SAP HANA Studio and create the first backup.

InitialBackupInStudio.PNG

So the user is able to create backups. The next step is to logon to the XS Admin tool using BACKUP_OPERATOR to make sure this is working fine too.

LogonXsAdmin.PNG

As everything is working fine we can start to build the small SAP HANA XS application which is executing the backup command.

We also build a test application to be able to check the final backup statement and to create a backup manually.

To keep things separated I create a new workspace first.

CreatingWorkspace.PNG

After the studio has restarted choose the development perspective by clicking “Open Development”.

OpenDevelopmentView.PNG

The project data will be stored in the database GTI. Due to this we have to add the database instance to the current workspace.

AddSystem.PNG

Finally we can create to actual project.

CreateXsProject.PNG

CreateXsProject_1.PNG

CreateXsProject_2.PNG

After clicking through all the popups the project should be created succesfully. It has no files yet.

CreateApplication_2.PNG

For this project we need five files:

  1. DataBackup.xsjob
    It contains the job definition.
  2. DataBackup.xsjs
    This file is called during the job execution. It only forwards the call to the file DataBackup.xsjslib
  3. DataBackup.xsjslib
    This file contains the actual command to create the database backup.
  4. index.html
    This is a simple html file which allows us to run the backup manually.
  5. DataBackupTest.xsjs
    This is a JavaScript file which is used by file index.html

The content of the files is as follows. You can use copy and paste.:

DataBackup.xsjslib

  function getSql(prefix) {

         return “BACKUP DATA USING FILE (‘” + prefix + “_BATCH_JOB’)”;

  }

  function getTimestamp() {

        var date = new Date();

        var year = date.getFullYear();

        var month = date.getMonth() +1;

        var day = date.getDate();

        var hour = date.getHours();

        var minute = date.getMinutes();

        var second = date.getSeconds();

       return year + “_” + month + “_” + day + “_” + hour + “_” + minute + “_” + second;

}

function createBackupStatement(prefix) {

        return getSql(prefix + “_” + getTimestamp());

}

function createBackup(prefix) {

       var sql = createBackupStatement(prefix);

       var conn = $.db.getConnection();

      var stmt = conn.prepareStatement(sql);

  stmt.execute();

  stmt.close();

  conn.close();

}

DataBackupTest.xsjs

  var dataBackup = $.import(“DataBackup.xsjslib”);

var prefix = $.request.parameters.get(“prefix”);

  var action = $.request.parameters.get(“action”);

  if (“Create backup command” === action) {

  var sql = dataBackup.createBackupStatement(prefix);

  $.response.setBody(“</p>SQL command for data backup:</p>” + sql);

  }

if (“Create backup” === action) {

     dataBackup.createBackup(prefix);

     $.response.setBody(“Backup created successfully. Check backup directory.”);

}

DataBackup.xsjs

function createBackup(input) {

       var dataBackup = $.import(“DataBackup.xsjslib”);

       dataBackup.createBackup(input.prefix);

}

DataBackup.xsjob

{

      “description”: “Create data backup of database GTI”,

      “action”: “Automatic_Backup:DataBackup.xsjs::createBackup”,

      “schedules”: [ {

        “description”: “Create data backup of database GTI”,

                “xscron“: “* * * * * * 59”,

                “parameter”: { “prefix”: “MyScheduledBackup” }

        } ]

}

index.html

  <html><body>

      Project to create a database backup.  </p>

          <form action=“DataBackupTest.xsjs”>

               <p>Prefix for backup:<br>

                      <input name=“prefix” type=“text” size=“30” maxlength=“30”>  </p>

              <p>  <input type=“submit” name=“action” value=“Create backup command”>

                      <input type=“submit” name=“action” value=“Create backup”>

           <input type=“reset” value=“Reset”>

             </p>  </form>

  </body></html>

To save and activate the application press the “forward”-button. After the project was activated successfully we can start a test.
The application creates a backup using a custom prefix followed by the current timestamp.

ActivateAndTest.PNG

ActivateAndTest_1.PNG

The backup location is the same as it was when the backup was created within the Studio. Different paths or the usage of backint can be achieved with different backup commands.
The last step is to use this application as an XS job. For this we have to activate the scheduler inside the XS engine.

EnableScheduler.PNG

You might have to create the parameter “scheduler” if it does not exist.

The scheduling is active once the parameter is set. It is not necessary to restart the database or the XS engine.

To schedule the application as a job we have to logon to the XS Admin.

ScheduleJob_1.PNG

After clicking on the field “XS Job Dashboard” the job is shown with status INACTIVE. By clicking on the job you get into the details of the job.

ScheduleJob_2.PNG

By clicking the button ‘Edit Schedule’ you get a pop up where you can set the job active.

ScheduleJob_3.PNG

After pressing the button OK, the job has the new status ACTIVE.

But the job is not scheduled yet. To schedule the job the field ‘Active’ in the upper part of the browser needs to be checked. You also have to set the password for user BACKUP_OPERATOR and a Start- and End Time.

ScheduleJob_5.PNG

After pressing the button “Save Job” at the bottom of your browser the status of the job will change to “SCHEDULED” or, “RUNNING”.

JobStatus.PNG

JobsInFileSystem.PNG

Using the configuration from above the backup is scheduled every 59 seconds which is very often of course.

More details about the XS job itself can be found here.

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

  1. Rajesh Kondanapalli

    Hi Stefan,

    I used the above procedure to configure the backup and its running fine.

    But how can i change the backup type to BACKINT from File system.

    thanks

    Rajesh K

    (0) 
  2. HP Basis

    Hi Stefan/Others,

    I have followed the same procedure but i am getting error while activating xsjob like ” required object does not exist”. Please let me know if i missed anything.

    Thanks and Regards,
    Balaji

    (0) 

Leave a Reply