CentOS 7

Simple Backup Script for Apache Web Server /w Mysql DB

Share on LinkedIn

This script may not be as clean as others but it gets the job done.

The purpose of this script is to backup both your entire web directory and database to a compressed file with date stamps. What you do with it from there is up to you.

Target: Web Directory and specific DB Instance – MUST know database name for this script

Environment Settings
Create /temp and /backups directories in /

mkdir temp
mkdir backups

See what’s in the directory by running the ls command

ls

Output will be similar to this:

backups bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys temp tmp usr var

Create Script in / directory

vi My-Backup-Script.sh

The only items that need to be changed in this script are the items in ‘red‘.

#! /bin/bash
TIMESTAMP=$(date +"%F")
BACKUP_DIR=/temp/My-Backup-$TIMESTAMPMYSQL_USER="your-db-username"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="your-db-username-password"
MYSQLDUMP=/usr/bin/mysqldump
DATABASE=your-db-name

mkdir -p "$BACKUP_DIR/mysql"
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD $DATABASE | gzip > "$BACKUP_DIR/mysql/$DATABASE.gz"

mkdir -p "$BACKUP_DIR/web_dir"
SRCDIR=/var/www/html/
DESTDIR=$BACKUP_DIR/web_dir/
FILENAME=My-WWW-Backup-$TIMESTAMP.tgz
tar --create --gzip --file=$DESTDIR$FILENAME $SRCDIR

tar --create --gzip --file=/backups/My-Backup-$TIMESTAMP.tgz $BACKUP_DIR

rm -rf /temp/*

wait
echo "Backup of DB and Web Directory Complete!"

Make Script Executable

chmod +x My-Backup-Script.sh

Run cron job every day at 1130pm

sudo crontab -e

Add the following to your cronjob:

MAILTO=""
30 23 * * * /bin/bash /My-Backup-Script.sh

To Extract Backup files, you will need tar -zxvf for the main file and web directory file. You will need to use gzip -d for the database file.

The Relationship Between a Domain Name and a Web Host

A domain name represents a physical location on the Internet; an IP address. The Internet Corporation for Assigned Names and Numbers (ICANN) governs coordination of the links between IP addresses and domain names across the Internet. With this standardized coordination, you can find websites on the Internet by entering domain names instead of IP addresses into your Web browser.

Web hosting is a service that allocates space on a computer server to a customer’s web site. It’s that physical location on the Internet as mentioned above that is assigned an IP address. This is where your domain name points to.

What is a domain name?

A domain name represents a physical point on the Internet — an IP address. The Internet Corporation for Assigned Names and Numbers (ICANN) governs coordination of the links between IP addresses and domain names across the Internet. With this standardized coordination, you can find websites on the Internet by entering domain names instead of IP addresses into your Web browser.

In layman’s terms, you can type 74.125.224.72 (IP address of the host) into your browser to search the Internet or you can type in Google.com. Google.com is a domain name that points to the physical location or point on the Internet. In this case, 74.125.224.72.