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.

Hi @hitechelp,
I didn’t get the chance to try out the script until today.
I copied it on the text editor, made the edits on the lines you indicated and saved it on the /home/openemr directory then turn it to an executable using sudo chmod a+x /home/openemr/backup and ran it using sudo /home/openemr/backup. However, at the terminal, I got this error message.


Any thoughts?
Thanks

Dagem,
I am so sorry.
While preparing to post the script file I overwrote the very first line of the instructions with;
*************INSTRUCTIONS
Here is the first line of the instructions;

Create a folder named oemrbkup in the /tmp folder using; sudo mkdir -m 777 /tmp/oemrbkup

That should take care of the problem.

Regards,
David

Hi,
Thank you for correcting the script. I tried the new script you sent.
And it runs with no problems but the backup files (.tar and .sql) are not copied onto the external drive even though it is mounted and accessible (I checked access using cd /media/xdrive).
Is it because I plugged the drive on a USB 3.0 port (I don’t have a USB 2.0 port on my laptop)?
Have you tried it on a USB 2.0 Port?
Thanks

I forgot to mention that the files are backed up correctly inside the “Wed” folder contained in the BackupRepository folder I created in the Home directory.

Dagem, check your system logs for any Rsync errors, if the backups are in your /home/username/BackupRepository/Day folder then the only thing that didn’t fly was the rsync.

Enter this on the command line and let me know what you get. Replace YourUserName
sudo rsync -avrze /home/YourUserName/BackupRepository/Wed/ /media/xdrive/BackupRepository/Wed

Hi,
This is what I got after running the command


I ran the backup script again today but today’s backup folder on xdrive is empty.

The backup files contained in the /home directory are owned by ‘root’.


I manually tried to copy today’s backup folder to the mounted drive and got this error.

Maybe ‘rsync’ doesn’t also have permissions to read.
What are your thoughts?
Thanks

Dagem,

First question is; Are you running a GUI on your server? If the answer is “Yes” then:
Unless you make some special provisions, a normal GUI user cannot perform actions as root (sudo)
Although there is a workaround, I highly recommend that you uninstall the GUI and install Webmin and use the browser of another computer on your local (or remote) network to administer your server.
There are many advantages to this including; Releasing essential resources consumed by the GUI such as memory, CPU clock cycles, disk space and gaining an amazing set of utilities in a secure GUI.

Install Webmin and access to automatic updates using;
sudo nano /etc/apt/sources.list - Remove the “#” from all lines ending in “universe” and add the line;
deb https://download.webmin.com/download/repository sarge contrib at the bottom of the file.
Save the file [Ctrl+o] then save it [Ctrl+q].
Obtain the secure GPG key to access the install package using;
cd /root
wget https://download.webmin.com/jcameron-key.asc
apt-key add jcameron-key.asc

You will be able to install Webmin and all the required dependencies using;
apt-get install apt-transport-https
apt-get update
apt-get install webmin

Access Webmin by browsing to; https://YourSeverIPAddress:10000/

Second question is; When you set up the cron job, did you set it to run as root? If not,
you can easily do so using Webmin. go to System>Scheduled Cron Jobs.
If you don’t see your Cron Job, Click “Create a new scheduled cron job”
On the first line; “Execute Cron job as”; (select “root” from the drop down listbox)
On the second line; “Active?” (accept the default yes)
Third line is “Command” enter the path to the script file followed by “./” then the filename. Like this;
/home/YourUserName/./backup.sh
Then set the “When to execute” to suit your needs.

Best wishes,
@hitechelp

Hi,
I’ve followed your guidelines and got the script working as expected.
I’ll be testing it for a week or two before starting using it to provide healthcare services for real patients.
Thank you very much for your support.