gbertb
12/17/2014 - 12:04 AM

db_backup_script

#!/bin/sh

#----------------------------------------------------
# a simple mysql database backup script.
# version 2, updated March 26, 2011.
# copyright 2011 alvin alexander, http://devdaily.com
#----------------------------------------------------
# This work is licensed under a Creative Commons 
# Attribution-ShareAlike 3.0 Unported License;
# see http://creativecommons.org/licenses/by-sa/3.0/ 
# for more information.
#----------------------------------------------------

# (1) set up all the mysqldump variables
DBSERVER=127.0.0.1
DOMAIN=
DATABASE=
USER=
PASS=
EMAIL=
FILE=${DOMAIN}.sql.`date +"%Y%m%d"`

# (2) in case you run this more than once a day, remove the previous version of the file
unalias rm     2> /dev/null
rm db_backups/${FILE}     2> /dev/null
rm db_backups/${FILE}.gz  2> /dev/null

# (3) do the mysql database backup (dump)

# use this command for a database server on a separate host:
#mysqldump --opt --protocol=TCP --user=${USER} --password=${PASS} --host=${DBSERVER} ${DATABASE} > ${FILE}

# use this command for a database server on localhost. add other options if need be.
mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > db_backups/${FILE} | mail -s 'Backup of db finished -'${FILE} ${EMAIL}

# (4) gzip the mysql database dump file
gzip db_backups/$FILE

# (5) show the user the result
echo "${FILE}.gz was created:"
ls -l db_backups/${FILE}.gz