Read about OpenEMR's Response to the COVID-19 Pandemic at https://www.open-emr.org/covid19/

OpenEMR 5.0.1 running on Ubuntu 18.04 / Backup on an external drive

Greetings,

I’ve installed OpenEMR 5.0.1 on Ubuntu 18.04 LTS.

I wanted a script which makes daily automated backups of the OpenEMR web directories and MySQL DB on an external hard drive but it seems that the instructions to do this are not featured on the “OpenEMR backup tools” page.

So I was trying to tweak the script provided in the “Automated Backups to an Alternate Server” guide even though I don’t have all the technical expertise it requires.
What I thought of doing is:

create a /root/bin directory and a file /root/bin/backup on the production server rather than on the backup server


I am pretty sure there are others things which need to be modified as well.
Could you let me know your thoughts on this?

Thank you

Dagem, If you have a reliable internet connection between the 2 servers it is a relatively simple matter to setup Rsync with an ssh key to keep the backup in sync with the production server. If you’re interested in the details just ask. My system backups are nearly 22Gb and the entire process takes about an hour and a half. It runs on cron jobs automatically every night. If my production server dies, the backup server is always available.

Hi David,
Thanks for your answer. But I don’t have a reliable Internet connection (my clinic is in Ethiopia). That’s the reason why I am looking for a script which could make automated backups of OpenEMR web directories and MySQL DB on an external drive (hard drive or USB).

I’ve currently installed OpenEMR 5.0.1 Ubuntu-Debian Package on Ubuntu 18.04

hi @dagem, sure we can contribute a script that would be useful for an external drive as well. Will post a draft script soon. We can take advantage of @hitechelp’s suggestion for rsync for the openemr folder and probably do a mysqldump of the database/

This scheme uses a folder in your home directory named BackupRepository which contains sub folders [Sun, Mon, Tue, etc.] This should be run nightly via a cron job to tar the openemr directory, do a mysqldump and move both files to that day’s folder. Rsync is used to sync them to another location because its much faster than copying, which moves the entire file. Rsync applies only what has been changed in the source files to the destination files. HINT: After the first run, copy both files into all the daily folders, this way Rsync only has to send the changes instead of the entire file. This scheme keeps a weeks worth of backups at your disposal before overwriting them.
INSTRUCTIONS
Create a folder named BackupRepository in your home directory. Inside that folder you need to
create daily folders with 3 letter abrv. as shown using; mkdir /home/username/BackupRepository/Sun
Continue creating the daily folders using the “up arrow” to recall and edit the previous command.
The first letter of the daily folder must be capitalized, as in Sun, Mon, Tue, etc.

To mount your external drive assuming it is not partioned AND on a usb2 port (usb3 doesn’t play nice)
First create a directory in the media folder using; sudo mkdir /media/xdrive
Next identify which device it is by using; lsblk (we’ll assume its sdb1 as used in the next command)
Then mount it with; sudo mount /dev/sdb1 /media/xdrive/
Check access using; cd /media/xdrive
Followed by; ls -al to list files and directories in it.
Create the BackupRepository directory using; sudo mkdir /media/xdrive/BackupRepository
As before, create sub-folders with 3 letter abrv. using; sudo mkdir /media/xdrive/BackupRepository/Sun

To Restore OpenEMR from a backup;

First restore the SQL Dump using:
sudo mysql -uroot -p[password] openemr < /media/xdrive/[DAY]/mysql_backup.sql

Next Delete or rename the /var/www/openemr directory
Then restore the openemr directory using:

sudo tar xf /media/xdrive/[DAY]/openemr_backup.tar -C /var/www

####################################################################################
There are some edits you need to make this script work.
Line 90; Replace YourMysqlRootPassword with the actual password between apostrophies & no spaces.
Lines 96, 99 and 101 replace the ? with name used for your home directory.
####################################################################################
#backup.sh
#Backup, and RSYNC to External Drive written for Ubuntu 18.4 Bionic

#OpenEMR backup script. This script will backup the full MySQL and
#WWW server directory for transfer to a remote location using RSYNC

#Copyright © 2020 David Martelle hitechelp@users.sf.net

#This program is free software: you can redistribute it and/or
#modify it under the terms of the GNU General Public License as
#published by the Free Software Foundation, either version 2 of
#the License, or (at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program. If not, see http://www.gnu.org/licenses/.
#User assumes all risk in using this script.

#START
#Editable variables -------------------------------------------------

#BASENAME is the name of the temp directory for backup files

BASENAME=/tmp/oemrbkup

#End of editable variables -------------------------------------------

#get the timestamp and format it for day only
#NOW=$(date +"%a_%d_%b_%Y") day_date_month_year
NOW=$(date +"%a")

#directory for todays backup files
DIR=${BASENAME}/${NOW}

#filename for mysql backup file
MYSQLFILE=${DIR}/mysql_backup.sql

#filename for openemr web backup file
WWWFILE=${DIR}/openemr_backup.tar

#cd to avoid directory not found error
cd /tmp

#set default umask for files created to u=rw, g=, o=
#directories will be u=rwx, g=, o=
umask 077

#make the new backup directory
mkdir -p “${DIR}”

#backup MySQL database
mysqldump -uroot -p’YourMysqlRootPassword’ openemr > “${MYSQLFILE}”

#Backup /var/www/openemr,
cd /var/www; tar -c openemr/ > “${WWWFILE}”

#Delete last week’s daily backup directory
rm -f -r /home/?/BackupRepository/"${NOW}"

#Move the Backup folder from tmp to home directory
mv -f /tmp/oemrbkup/"${NOW}" /home/?/BackupRepository

rsync -avrze /home/?/BackupRepository/"${NOW}"/ /media/xdrive/BackupRepository/"${NOW}"

Hi,
@hitechelp @stephenwaite Many thanks for contributing this comprehensive script.
I’ll try it out (including the restore process) and advise you the results.
Best,

My pleasure Dagem,
Remember that the day folder used to store the backup will depend on when the cron job was run.

Best of luck, and stay safe.
Regards, D.