Forked from AlexBezuska/Server Backup Shell Script
Last active
April 30, 2016 05:35
-
-
Save heriniaina/dab41f22a9127030bff1d575eb30d496 to your computer and use it in GitHub Desktop.
Server Backup script not mine, credit goes to Joshua R Jones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A Simple Shell Script to Backup Red Hat / CentOS / Fedora / Debian / Ubuntu Apache Webserver and SQL Database | |
# Path to backup directories | |
USER="hery" | |
HOME="/home" | |
# Store only day of month | |
DOM=$(date +"%d") | |
# Temporary backup name | |
# this will be created under /tmp | |
BACKUPDIR="/backup" | |
# Backup file name hostname.time.tar.gz | |
HOMEFILE="$USER-home-$DOM.tar.gz" | |
MYSQLFILE="$USER-mysql-$DOM.tar.gz" | |
# Specify databases | |
DBNAME1="hery_db" | |
# 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 ssh server directory to store dumps | |
SSHPORT=22 | |
# 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" | |
#security reason | |
BACKUPDIR=/tmp${BACKUPDIR} | |
# make sure backup directory exists | |
[ ! -d $BACKUPDIR ] && mkdir -p ${BACKUPDIR} | |
#clean backup dir | |
rm -rf ${BACKUPDIR}/* | |
# Log backup start time in /var/log/messages | |
$LOGGER "$0: *** Backup started @ $(date) ***" | |
# Backup websever dirs | |
$TAR -zcvf ${BACKUPDIR}/${HOMEFILE} "${HOME}" | |
# Individual MySQL Backups | |
$MYSQLDUMP -u ${MYSQLUSER} -h localhost -p${MYSQLPASSWORD} ${DBNAME1} | $GZIP -9 > ${BACKUPDIR}/${MYSQLFILE} | |
# Dump all local files to failsafe remote UNIX ssh server / home server - FOR A LATER DATE | |
$SSH -p ${SSHPORT} ${SSHUSER}@${SSHSERVER} mkdir -p ${SSHDUMPDIR} | |
$SCP -r ${BACKUPDIR}/* ${SSHUSER}@${SSHSERVER}:${SSHDUMPDIR} | |
# 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