Skip to content

Instantly share code, notes, and snippets.

@AlexBezuska
Last active April 30, 2016 04:37
Show Gist options
  • Save AlexBezuska/8552188 to your computer and use it in GitHub Desktop.
Save AlexBezuska/8552188 to your computer and use it in GitHub Desktop.
Server Backup script not mine, credit goes to Joshua R Jones
#!/bin/bash
# A Simple Shell Script to Backup Red Hat / CentOS / Fedora / Debian / Ubuntu Apache Webserver and SQL Database
# Path to backup directories
DIRS="/www"
ETC="/etc"
HOME="/home"
# Store todays date
NOW=$(date +"%F")
# Store backup path
STATICBACKUP="/backup/Backup$NOW/static"
MYSQLBACKUP="/backup/Backup$NOW/mysql"
# Backup file name hostname.time.tar.gz
BFILE="Static-www.$(date +'%T').tar.gz"
BETCFILE="Static-etc.$(date +'%T').tar.gz"
BHOMEFILE="Static-home.$(date +'%T').tar.gz"
MFILE="MySQL.$(date +'%T').mysql.sq.gz"
# Specify databases
MDBVARNAMEHERE1="mysqldatabasename.$(date +'%T').mysql.sq.gz"
MDBVARNAMEHERE2="mysqldatabasename2.$(date +'%T').mysql.sq.gz"
# Set MySQL username and password
MYSQLUSER="MYSQL-USER-NAME"
MYSQLPASSWORD="MYSQL-PASSWORD"
# Remote SSH server setup - FOR A LATER DATE
#SSHSERVER="backup.example.com" # your remote ssh server
#SSHUSER="" # username
#SSHDUMPDIR="/backup/remote" # remote ssh server directory to store dumps
# Paths for binary files
TAR="/bin/tar"
MYSQLDUMP="/usr/bin/mysqldump"
GZIP="/bin/gzip"
# SCP="/usr/bin/scp"
# SSH="/usr/bin/ssh"
LOGGER="/usr/bin/logger"
# make sure backup directory exists
[ ! -d $STATICBACKUP ] && mkdir -p ${STATICBACKUP}
[ ! -d $MYSQLBACKUP ] && mkdir -p ${MYSQLBACKUP}
# Log backup start time in /var/log/messages
$LOGGER "$0: *** Backup started @ $(date) ***"
# Backup websever dirs
$TAR -zcvf ${STATICBACKUP}/${BFILE} "${DIRS}"
$TAR -zcvf ${STATICBACKUP}/${BETCFILE} "${ETC}"
$TAR -zcvf ${STATICBACKUP}/${BHOMEFILE} "${HOME}"
# Full MySQL Backup
$MYSQLDUMP -u ${MYSQLUSER} -h localhost -p${MYSQLPASSWORD} --all-databases | $GZIP -9 > ${MYSQLBACKUP}/${MFILE}
# Individual MySQL Backups
$MYSQLDUMP -u ${MYSQLUSER} -h localhost -p${MYSQLPASSWORD} mysqldatabasename | $GZIP -9 > ${MYSQLBACKUP}/${MDBVARNAMEHERE1}
$MYSQLDUMP -u ${MYSQLUSER} -h localhost -p${MYSQLPASSWORD} mysqldatabasename2 | $GZIP -9 > ${MYSQLBACKUP}/${MDBVARNAMEHERE2}
# Dump all local files to failsafe remote UNIX ssh server / home server - FOR A LATER DATE
# $SSH ${SSHUSER}@${SSHSERVER} mkdir -p ${SSHDUMPDIR}/${NOW}
# $SCP -r ${STATICBACKUP}/* ${SSHUSER}@${SSHSERVER}:${SSHDUMPDIR}/${NOW}
# $SCP -r ${MYSQLBACKUP}/* ${SSHUSER}@${SSHSERVER}:${SSHDUMPDIR}/${NOW}
# Log backup end time in /var/log/messages
$LOGGER "$0: *** Backup Ended @ $(date) ***"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment