#fullbackup2.sh
#
#OpenEMR backup script. This script will backup the full
#   MySQL and PostgreSQL servers in gzip format, and it
#   will backup the ../openemr/documents and ../openemr/edi and ../freeb/public
#   directories in DAR format.(NOT TAR)
#
#This new version was created for appliance OpenEMR 2.8.3+, to backup
#the ../openemr/edi directory, which saves the X12 batch files.
#
#Author: Brady Miller   Email:brady@sparmy.com
#Date:9/16/2007
#
#This is still an alpha version. Do whatever you want with this script. Use at your own risk. I am not
#responsible if any bad things happen while using this script.
#

#Edit below two variables. DVDDEVICE is device of your 
#  DVDWriter. YOURFULLNAME is your full name that you typed
#  when making your encryption key.
DVDDEVICE=/dev/hdb
YOURFULLNAME='Brady Miller'

#Constant variables
DIR=/backup/`date -I`
ENCRYPTDIR=/backup/encrypted
ENCRYPTSAVEDIR=${ENCRYPTDIR}/encryp`date -I`
MYSQLFILE=${DIR}/`date -I`_mysql_backup.gz
POSTGRESQLFILE=${DIR}/`date -I`_postgresql_backup.gz
OPENEMRDOCS=${DIR}/`date -I`_openemrdocs
OPENEMREDIS=${DIR}/`date -I`_openemredis
FREEBDOCS=${DIR}/`date -I`_freebdocs
TEMPFILE=/tmp/`date -I`temporarydvdbackuplogfile

#to avoid a 'can't move into directory' error
cd /tmp

#make the new /backup/currentdate directory
# and the temporary to be encrypted directories
mkdir $DIR
chown root:root $DIR
chmod 700 $DIR
mkdir $ENCRYPTDIR
chown root:root $ENCRYPTDIR
chmod 700 $ENCRYPTDIR
mkdir $ENCRYPTSAVEDIR
chown root:root $ENCRYPTSAVEDIR
chmod 700 $ENCRYPTSAVEDIR

#backup MySQL database
mysqldump --opt --all-databases | gzip  > $MYSQLFILE
chown root:root $MYSQLFILE
chmod 600 $MYSQLFILE

#backup POSTgreSQL database
sudo -u postgres pg_dumpall | gzip  > /tmp/postgresqlbackup`date -I`
mv /tmp/postgresqlbackup`date -I` $POSTGRESQLFILE
chown root:root $POSTGRESQLFILE
chmod 600 $POSTGRESQLFILE

#Backup patient docs in /var/www/html/openemr/documents
dar -y -Q -N -s 600M -D -R /var/www/html/openemr/documents -c $OPENEMRDOCS -Z "*.gz" -Z "*.bz2" -Z "*.zip" -Z "*.png" > /dev/null
dar -t $OPENEMRDOCS -Q -N > /dev/null
chown root:root $OPENEMRDOCS*.dar
chmod 600 $OPENEMRDOCS*.dar

#Backup X12 billing docs in /var/www/html/openemr/edi
dar -y -Q -N -s 600M -D -R /var/www/html/openemr/edi -c $OPENEMREDIS -Z "*.gz" -Z "*.bz2" -Z "*.zip" -Z "*.png" > /dev/null
dar -t $OPENEMREDIS -Q -N > /dev/null
chown root:root $OPENEMREDIS*.dar
chmod 600 $OPENEMREDIS*.dar

#Backup freeb insurance bill copies in /usr/share/freeb/public
dar -y -Q -N -s 600M -D -R /usr/share/freeb/public -c $FREEBDOCS  -Z "*.gz" -Z "*.bz2" -Z "*.zip" -Z "*.png" > /dev/null
dar -t $FREEBDOCS -Q -N > /dev/null
chown root:root $FREEBDOCS*.dar
chmod 600 $FREEBDOCS*.dar

#encrypt the files and move to temporary encrypted directory
gpg -e -r "$YOURFULLNAME" $MYSQLFILE
gpg -e -r "$YOURFULLNAME" $POSTGRESQLFILE
gpg -e -r "$YOURFULLNAME" $OPENEMRDOCS*.dar
gpg -e -r "$YOURFULLNAME" $OPENEMREDIS*.dar
gpg -e -r "$YOURFULLNAME" $FREEBDOCS*.dar
mv ${DIR}/*.gpg $ENCRYPTSAVEDIR
chown root:root $ENCRYPTSAVEDIR/*
chmod 600 $ENCRYPTSAVEDIR/*


#Burn the encrypted backup files to a R DVD via multi-session

#First, need to see if DVD is blank. The DVD command growisofs
#  requires a -Z option if blank, and a -M option if contains
#  any data.
growisofs -dry-run -M $DVDDEVICE -R -J $ENCRYPTDIR 2> $TEMPFILE
chown root:root $TEMPFILE
chmod 600 $TEMPFILE
cat $TEMPFILE

#Second, use output from above command to decide which growisofs
#   option to use and then burn the backup
X=$(grep -c 'use -Z option' $TEMPFILE)
Y=0
if [ $X -gt $Y ]
  then
    echo "blank dvd"
    #DVD was completely blank, need to use the -Z option
    growisofs -Z $DVDDEVICE -R -J $ENCRYPTDIR
  else
    echo "multisession dvd"
    #DVD wasn't blank, so -M option will be used
    growisofs -M $DVDDEVICE -R -J $ENCRYPTDIR
  fi

#remove the local temporary encrypted files
rm -fR $ENCRYPTDIR

#remove other temporary files
rm -f $TEMPFILE
