Updated version of Ubuntu Backup

From OpenEMR Project Wiki
Revision as of 21:33, 24 April 2012 by Bradymiller (talk | contribs) (2 revisions: Updated_version_of_Ubuntu_Backup)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This is an updated version of brady@sparmy.com's ubuntu backup script.

changes:

  • added GPL license on it
  • do some more verbose output
  • made the default directory in /tmp. Some systems use multiple partitions (one for /var, /tmp, /usr, /home, etc) and / is mounted read only for safety
  • added a #! "shebang" line to force it to run under bash
  • read the date once then use it throughout the script to keep filenames consistent, should be okay unless the date command gets duplicated (run twice in a second, from 2 machines with time skew, clock reset backwards, etc)
  • use umask to set the default permissions and get rid of the chmod calls, rely on the script running as root to keep from having to do all those chown calls.
  • use pipes instead of temp files to move the data from mysqldump/tar into gpg. Should run a bit faster.
  • use the graft_points option to growisofs to keep from having to make special directories to make the files show up on the disc correctly.
  • check the return string to see if no disc was loaded - on my laptop the growisofs command would eject the disc, and there's no motor to pull it back in, so it would sit out. There is a secret option for growisofs to prevent that behaviour (-use-the-force-luke=notray), but it sounded risky, so I left it out.
  • add an option to leave the files on the hard disk for a certain number of days.

you can download a copy here: Media:Fullbackup4.tar.

#!/bin/bash
#
# fullbackup4.sh
#
# OpenEMR backup script. This script will backup the full MySQL and
#   WWW servers to DVD in pgp encrypted and compressed format
#
# Copyright (C) 2011 Andrew Dyer <amdyer@gmail.com>
#
#    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/>.
#
# History: This version started from fullbackup3 by Brady Miller
# <brady@sparmy.com> Date:02/01/2009.  Original license stated "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."
#

# --------------------------------------------------------------------
# Editable variables.
#
# DVDDEVICE is the linux device name of your DVD drive.
DVDDEVICE=/dev/scd0

#  YOURFULLNAME is your full name that you typed when making your
#  encryption key.
YOURFULLNAME=''

# KEEPDAYS is the number of days to keep encrypted backup files on the
# hard disk, use 0 to keep forever
KEEPDAYS=7

# BASENAME is the name of the directory where the backup files
# will be kept
BASENAME=/tmp/openemr_backup

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

# get the timestamp and format it for no spaces
NOW=$(date +"%a_%d_%b_%Y_%H:%M:%S%z")

# directory for this runs' files
DIR=${BASENAME}/${NOW}

# filename for mysql backup file
MYSQLFILE=${DIR}/mysql_openemr_bckup.pgp

# filename for openemr web backup file
WWWFILE=${DIR}/www_openemr_backup.pgp

# options passsed to growisofs/genisofs
GENISOFS_OPT="-R -J -graft-points"

# print some banner stuff
echo "============================================================="
echo "fullbackup4.sh --  openemr backup script"
echo "start time: " ${NOW}

# check if running as root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root, exiting" 1>&2
   exit 1
fi

# avoid a 'no directory' 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, pipe to gpg for compression and encryption
echo `date` "dumping mysql and encrypting"
mysqldump --opt openemr | gpg --homedir "/home/openemr/.gnupg" -z6 -e -r "${YOURFULLNAME}" > "${MYSQLFILE}"

# Backup /var/www/openemr, pipe to gpg for compression
# and encryption
echo `date` "tarring /var/www/openemr and encrypting"
(cd /var/www; tar -c openemr/ | gpg -z6 --homedir "/home/openemr/.gnupg" -e -r "${YOURFULLNAME}" > "${WWWFILE}" )

# Burn the encrypted backup files to a R DVD via multi-session First,
# need to see if DVD is present and blank. The DVD command growisofs
# requires a -Z option if blank, and a -M option if contains any data.

echo `date` "Running growisofs dry run to see if the -Z option will be needed"
ISO_TEST=`growisofs -dry-run -M ${DVDDEVICE} ${GENISOFS_OPT} "${NOW}=${DIR}" 2>&1`

case "${ISO_TEST}" in
    ":-[ LOAD TRAY failed"*)
	echo "no DVD found??"
	;;

    *"use -Z option.") 
	echo "This is a blank dvd, so will use the -Z option"
        growisofs -Z ${DVDDEVICE} ${GENISOFS_OPT} "${NOW}=${DIR}"
	;;

    *)
	echo "This is not a blank dvd, so will use the -M option"
	growisofs -M ${DVDDEVICE} ${GENISOFS_OPT} "${NOW}=${DIR}"
	;;
esac

# clean out old files, then empty directories
if [ $KEEPDAYS -gt 0 ]; then
    find ${BASENAME} -type f -mtime ${KEEPDAYS} -exec rm {} \;
    find ${BASENAME} -type d -empty -exec rm {} \;
fi

echo "fullbackup4.sh finished"
echo "end time: " `date`
echo "============================================================="