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}"