HANA Database Backup using Cron

HANA Database Backup using Cron

To create backups using HANA we

  1. create a user
  2. create a user key
  3. create a script
  4. schedule the script to run via cron job

1) Create a HANA user to use specifically for Cron Job – Daily HANA Database Backup

Open an SQL Window and run the following to create a user,  give them access to backup,  and then remove their password so they can not connect via login.

create user backup_operator password Xxxxxxxx1;
grant backup operator to backup_operator;
alter user backup_operator disable password lifetime;

2)  Create a HANA user key to be used for automated connection to the HANA database by the Backup User

Login to the server console using putty.
Under the hdbclient/ directory use the hdbuserstore command to create a key, -i makes it so you will then have to type in a password key from the command above

#/vol/vol_HDB/sysfiles/hana/shared/HDB/hdbclient/hdbuserstore -i SET cronbackupkey localhost:30015 backup_operator

Next,  list all keys so that we know where the keys are that we will use to run the cron job automatically.

#/vol/vol_HDB/sysfiles/hana/shared/HDB/hdbclient/hdbuserstore list
DATA FILE : /home/backupuser/.hdb/sid-hdb/SSFS_HDB.DAT

KEY CRONBACKUPKEY
 ENV : localhost:30015
 USER: backup_operator

Run a quick check of the new key by running a test command, this is where the password you entered above will be used

#/vol/vol_HDB/sysfiles/hana/shared/HDB/hdbclient/hdbsql -U cronbackupkey "select now() from dummy;"|more
CURRENT_TIMESTAMP
"2014-12-26 21:46:24.799000000"
1 row selected (overall time 600 usec; server time 98 usec)

If you run into a situation where the password is wrong, you may end up with a message usage as this:

* 416: user is locked; try again later: lock time is 1440 minutes; user is locked until 2014-12-27 21:35:34.3170000 (given in UTC) [1440,2014-12-27 21:35:34.3170000] SQLSTATE: HY000

If that happens, fix the password by running the hdbuserstore -i DELETE cronbackupkey and hdbuserstore -i SET command above with the correct password than run the following commands to allow the user access again.

alter user backup_operator RESET CONNECT ATTEMPTS;

Using these method the automated method finally came together. Keep in mind the the password for connecting to the database is stored in the key, so if you update the password in the database for the user, you will need to also update the password stored in the key.

 

3) Create a bash script to backup the HANA database to a time and date file

Create a bash script file you can run from a cron job,  that does the work of creating a backup file.  I create a wrapper script instead or running  a command from the cron job,  so I can decide in the script whether I would like to receive an email with the output of the command.

#touch /customcommands/hanabackup
#chown hdbadm.sapsys /customcommands/hanabackup
#chmod 754 /customcommands/hanabackup
#vi /customcommands/hanabackup
tmpfile=/tmp/`date +s`
textsearch="successful"
sqlcommand="BACKUP DATA USING FILE ('$(date +F_%k%M)')"
/vol/vol_HDB/sysfiles/hana/shared/HDB/hdbclient/hdbsql -U CRONBACKUPKEY $sqlcommand>$tmpfile
#look up the backup that just completed"
/vol/vol_HDB/sysfiles/hana/shared/HDB/hdbclient/hdbsql -U CRONBACKUPKEY "SELECT top 1 *
 FROM M_BACKUP_CATALOG
 WHERE ENTRY_TYPE_NAME = 'complete data backup'
 ORDER BY UTC_START_TIME desc">>$tmpfile
success=`grep $textsearch $tmpfile`
if [ "$success" == "" ]; then
 echo "HANA BACKUP FAILED: `date`"
 echo "SQL COMMAND: $sqlcommand"
 echo "TEXT NOT FOUND:$textsearch"
 echo
 echo "File Out Put Below"
 echo ---------------------------------
 echo
 cat $tmpfile
 exit 10
fi
exit 0

This script will output a message ONLY if the HANA backup fails,  if it is successful,  it just quietly completes

 

4) Finally setup a cron job which runs the HANA database backup once a day

create a crontab entry to run one time per day at 1:01 am

#crontab -e
MAILTO=myemail@myemail.com
1 1 * * * /customcommands/hanabackup

In order to test that this works, you can force a test failure by setting a data_backup_buffer that is to high for your system see a recent post where we corrected this issue.
You can set a much higher limit than you system allows, and you can confirm that you have the CRON process send you an email each time that the process fails.