This is a script I have developed to back up my hosting environment, and it actually consists of two. One to be started by cron, and then it starts the other. This allows for a modular setup between your environments (apache/nginx/etc).

The scripts:

backup.sh

#!/bin/bash
# Web Host Backup
# Cron Version
# Jim McKibben
# 2015-10-12
# Version .1
# Nova-Labs Export

# Timestamp Function
timestamp() {
  date +"%Y-%m-%d_%H-%M-%S"
}
# Get the Hostname
HOST=`cat /etc/hostname`
# Make the File Stamp
VARFILESTAMP=$HOST"-"$(timestamp)

echo "Starting backup for "$VARFILESTAMP

# Maintenance Mode stuff
echo "Put Magento into maintenance mode"
echo "Magento is in maintenance mode"
mv /var/www/dc/maintenance.flag /var/www/dc/www/
rm -rf /var/www/dc/www/var/*

cd /root/backup/
. /root/backup/lampbackup.sh

echo "Compressing Sites Data to "$VARFILESTAMP"-sites.tar.bz2"
nice tar cfj "/var/"$VARFILESTAMP"-sites.tar.bz2" /var/www/

# Undo Maintenance Mode
echo "Put Magento back in production"
mv /var/www/dc/www/maintenance.flag /var/www/dc/

# Hash those files
md5sum /var/$VARFILESTAMP-sites.tar.bz2 > /var/$VARFILESTAMP-sites.md5

echo /var/$VARFILESTAMP-sites.md5

echo "Done"

lampbackup.sh

#!/bin/sh
# LAMP Prod Export
# Cron Version
# Jim McKibben
# 2015-08-03
# Version .1
# Takes most common LAMP setup files including DBs and exports them
# into usefully named .gz files with hostname and timestamp data

# Localize Path
WORKDIR="/root/backup/"

# Timestamp Function
timestamp() {
  date +"%Y-%m-%d_%H-%M-%S"
}
# Get the Hostname
HOST=`cat /etc/hostname`
# Make the File Stamp
FILESTAMP=$WORKDIR""$HOST"-"$(timestamp)

# Set the MySQL Admin Root
MYSQLUSER='root'

# Get the MySQL Admin Password
#read -s -p "Enter Password: " MYSQLPASSWORD
# Set the MySQL Admin Password
MYSQLPASSWORD='XXXXXXXXX'

# Get a list of current databases
DBS=`sudo mysql -u $MYSQLUSER -p$MYSQLPASSWORD -A -e "show databases;"`

# Set list of admin databases
ADMINDBS="mysql columns_priv db host servers tables_priv user"

# Remove service related databases
DBS=${DBS//mysql}
DBS=${DBS//Database}
DBS=${DBS//information_schema}
DBS=${DBS//performance_schema}

echo "Exporting MySQL Prod Data to "$FILESTAMP"-mysqlproddata.sql.gz"
nice mysqldump -u $MYSQLUSER -p$MYSQLPASSWORD --databases $DBS | gzip > $FILESTAMP"-mysqlproddata.sql.gz"

echo "Exporting MySQL Admin Data to "$FILESTAMP"-mysqladmindata.sql.gz"
nice mysqldump -u $MYSQLUSER -p$MYSQLPASSWORD $ADMINDBS | gzip > $FILESTAMP"-mysqladmindata.sql.gz"

echo "Exporting MySQL Config Data to "$FILESTAMP"-mysqlconf.tar.gz"
tar cfz $FILESTAMP"-mysqlconf.tar.gz" /etc/mysql/

echo "Exporting Apache Sites Available Data to "$FILESTAMP"-apachesiteav.tar.gz"
tar cfz $FILESTAMP"-apachesiteav.tar.gz" /etc/apache2/sites-available/

echo "Exporting Apache Sites Enabled Data to "$FILESTAMP"-apachesiteen.tar.gz"
tar cfz $FILESTAMP"-apachesiteen.tar.gz" /etc/apache2/sites-enabled/

echo "Exporting Apache Mods Enabled Data to "$FILESTAMP"-apachesiteen.tar.gz"
tar cfz $FILESTAMP"-apachemoden.tar.gz" /etc/apache2/mods-enabled/

echo "Exporting SSL Data to "$FILESTAMP"-ssl.tar.gz"
tar cfz $FILESTAMP"-ssl.tar.gz" /etc/ssl/private/ /etc/ssl/crt/

echo "Compressing Prod DB and Apache Data to "$FILESTAMP"-backup.tar.bz2"
nice tar cfj $FILESTAMP"-dbandconfbackup.tar.bz2" /root/backup/

nice md5sum $FILESTAMP-dbandconfbackup.tar.bz2 > $FILESTAMP-dbandconfbackup.md5

echo "Done"

crontab

MAILTO="myemail@hostname.com"
0 1 * * 0 /root/backup/backup.sh

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.