Making backup and restoration of database on OpenMRS Standalone

Backup

Here there are the commands for copying and restoring the database of an OpenMRS Standalone 2.10.0 server.

All this procedure can also be done remotely following same steps.

First of all, do a dump of the MySQL database:

mysqldump -u [user] -p[password] -e --single-transaction -q  --port=[port] --host=[@IP] openmrs >openmrs_bkup_`date +%y%m%d`.sql


  • [user] --> The user name of the embedded MySQL server (by default it is openmrs)
  • [port] --> The port used by the MySQL server
  • [@IP] --> The @IP of the server. If you are on a local installation, use loopback address (127.0.0.1)


For more info referring to the backup process, please check MySQL backup page.


Now copy the /appdata directory:

cp -r /referenceapplication-standalone-2.10.0/appdata /dir/temp-backup


More info about appdata directory can be found in Application Data Directory page.


And that’s all! There is a script which automatizes all this procedure.

In this script, there is also a log of the back up routine, which is sent to another machine via scp.

# Dump the data from OpenMRS to a file

mysqldump -u openmrs -p[password] -e --single-transaction -q 
--port=[MySQLport] --host=[@host] openmrs >openmrs_bkup_`date +%y%m%d`.sql
2> /dir/error_log.txt

# Copy the AppData folder 

mkdir /dir/temp-backup 
cp -r /referenceapplication-standalone-2.10.0/appdata /dir/temp-backup 2>>
/dir/error_log.txt

# Zip up the backup files

zip -r openmrs_`date +%y%m%d` openmrs_bkup.sql /dir/temp-backup/appdata
2>> /dir/error_log.txt

#Remove temporal directory

rm -r /dir/temp-backup

#Display new file

echo "created backup file : " openmrs_`date +%y%m%d` 

#Send the file into the remote server

sshpass -p "[PasswordForTheSession]" scp -r [user]@[@IP]:/dir/error_log.log
/dir/bckpFromServer/


Making backups periodically

For making new backups periodically we use cron.

Execute crontab -e

A sample command to execute the script file at 5:00 am once a week would be:

0 5 * * 1 /dir/scriptBackup.sh


Restoration

For the restoration procedure follow thse steps:


  1. Start the server:  

    java -jar openmrs-standalone.jar
  2. Import the last backup database into the MySQL server installation: 

    mysql -u[user] -p[password] --port=[MySQLport] --host=[@IP] openmrs < openmrs_bkup_’date’.sql
  3. Stop the server.

  4. Restore the appdata directory. Remove the current one and replace it with the 'backupped' one.

  5. Start the server again. It will be started in maintenance mode:


    Log in with your user and password (by default, User: admin Password:Admin123). Now, a new screen will be opened. In this screen there will be the concepts and databases to be updated. Just click in the green arrow at the bottom on the page and wait for it to finish.


That is all you need to make periodically backups and database restorations.

References

Backup and Restoration of the Shared Health Record

https://jembiprojects.jira.com/wiki/spaces/RHEAPILOT/pages/12189746/Backup+and+Restoration+of+the+Shared+Health+Record