Skip to content

Instantly share code, notes, and snippets.

@shoman4eg
Last active October 19, 2017 20:49
Show Gist options
  • Save shoman4eg/1cdec19b2d4be3b3daf1801aea94441d to your computer and use it in GitHub Desktop.
Save shoman4eg/1cdec19b2d4be3b3daf1801aea94441d to your computer and use it in GitHub Desktop.
#!/bin/sh
#----------------------------------------------------------
# a simple mysql database backup script.
# version 2, updated March 26, 2011.
# copyright 2011 alvin alexander, http://alvinalexander.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
BACKPATH=/home/user/backup
DATE=`date +"%Y%m%d-%H:%M"`
DATABASE=$1
DBSERVER=localhost
USER=root
PASS=Fdfnfhujh55
SQLFILE=$BACKPATH/$DATABASE-${DATE}.sql
# (2) in case you run this more than once a day, remove the previous version of the file
unalias rm 2> /dev/null
rm ${SQLFILE} 2> /dev/null
rm ${SQLFILE}.gz 2> /dev/null
# (3) do the mysql database backup (dump)
find $BACKPATH -type f -mtime +30 -exec rm {} +
# 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 --user=${USER} --password=${PASS} ${DATABASE} > ${SQLFILE}
# (4) gzip the mysql database dump file
gzip $SQLFILE
# (5) show the user the result
echo "${SQLFILE}.gz was created:"
ls -l ${SQLFILE}.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment