Baking Up Your OpenEMR Server

When you install OpenEMR, some extra stuff will be availabe under /your/www/path/openerm/contrib, there are two folders one is called /forms and the other /util.

Within the folder /util there's a file called backup_oemr.sh the .sh stands for "shell script". This file has the instructions to back up the entire server setup. Let's see how.

On black, you'll see the original content of the file, on blue my coments about it.

#!/bin/bash
Everithing starting with a # is a comment and will be ignored by the script, the only exception is the first line, which tells where the bash interpreter is, and the default should be fine in most Linux distributions

# backups an openemr system and burns a cd
#vars

db='openemr'
This line asks for the database name where the OpenEMR information is stored in.

dbuser='backuper'
This is the user name for that database, it is advisable to set up a different user who only has "backuping" privileges to the OpenEMR database and nothing else

dbpass='bakuper'
Obviously it want's the password for the given user.

pguser='sql-ledger'
This section is for backing up the ledger information, here you have to enter the user you set up when creating the ledger.

workdir='/var/tmp/backups/'
This is the directory that will be used for temporary writes, in some distributions the partition squema will not give enough room, so be sure to have a temp folder where you can write stuff

tempdir='/var/tmp/isos/'
Another temp folder, here the backup script will be creating a temporary .iso file, to be burnt later on the CD

installdir='/var/www/openemr'
This must reflect your full path to the openemr installation

newerthan='2005-01-01'
#newerthan is a workaround for when your media gets full,
#re-edit this file and change the date to the last succesfull cd copy
As explained in the previous lines, this is a workaround to a full disk. Some day, it will happen that the information won't fit in a single CD, when that day comes, you just re-edit this file and change the newerthan to the day that happend. The format is yyyy-mm-dd.

What will happen here is that the new CD will have only the database info plus the "newer than" patient uploaded documents. So you need to keep also the last CD before this change to reconstruct the full info.


# a name for the file that is unique of that day
file=`date +%F`
Here the script will automaticale take the current date to generate some file names

#get the content from the mysql database
mysqldump -u ${dbuser} -p${dbpass} --databases ${db} > ${workdir}${file}.sql.bak
The information from the MySQL database is being saved in a file with a format of yyyy-mm-dd.sql.bak, in the directory that you entered as "workdir"

# compress the file
bzip2 ${workdir}${file}.sql.bak
The bzip2 utility will compress the file to save space, you must have bzip2 installed.

#get the content from the postgres database
pg_dumpall -U ${pguser} > ${workdir}${file}postgres.bak
Same thing, the content from the ledger databse will be saved

# compress the file
bzip2 ${workdir}${file}postgres.bak
...and compressed

# get all the files on the site
# we are using bzip2 here
tar -N ${newerthan} -jcf ${workdir}${file}openemr.tar.bzip2 ${installdir}
These lines make a packet with all the files on the /openemr as declared in the line where you entered the full path to the installation directory

# create a disk image
label=`date +%y%m%d`
mkisofs -J -R -V ${label} -o ${tempdir}image.iso ${workdir}
The CD .iso image file is generated here

#burn it
cdrecord -dao -v -data -eject ${tempdir}image.iso
You must have the "cdrecord" utility installed in your system for the CD to be burnt. The instruction will burn the .iso and eject the CD, so when it's done you'll find it with the tray out.

#delete the original files
rm -rf ${workdir}*
Cleans up the mess.

exit 0
end of script.

So now that you have adjusted your script to your needs you'll need to make it run automatically so there's no messing instructions to remember. When this is done, all you'll do is take the CD out of the open tray, label it, save it in safe place, replace an empty cd in the tray, and press the close button.

Some preliminary work will be put the script in the right place and with the right permissions.

Linux offers the cron deamon to run stuff for you when you want.

Log in your shell
andres@openemr:$

be root for a while
andres@openemr:$sudo su -
root@openemr:#

Make a directory for your scripts
root@openemr:# mkdir /root/scripts

Copy your script there, (replace /var/www/openemr with your actual path to openemr)
root@openemr:# cp /var/www/openemr/contrib/util/backup_oemr.sh /root/scripts/

Be sure it's owned by root
root@openemr:# chown root:root /root/scripts/backup_oemr.sh

Change permissions to the script so it's only executable.
root@openemr:# cmod 100 /root/scripts/backup_oemr.sh

Now, we need to tell cron daemon, the one who will be running the backup on schedule to do so.
root@openemr:# crontab -e

that will bring up either, an empty file, or if you already had any entry a file with some lines. (If you type: $ man 5 crontab
that can give you more info in how to use cron an its tables.)

To make the script run from Monday to Friday at 1am add the following to the cron table.

0 1 * * 1,2,3,4,5 /root/scripts/backup_openemr.sh

Most distributions will show the table using vi or vim text editors. To enter into writing mode type "i" (for insert), then type the text as shown in the line above, when you are done, type "ESC" (the escape key), then :wq and you'll see a message like this:
crontab: installing new crontab

Congratulations, you have setup your backup script, rembember to place a blank CD and to replace it every day.

Popular Links
Wiki Index
Login
Username:

Password:


Lost Password?

Powered by XOOPS 2.2.3 Final © 2001-2005 The XOOPS Project