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.

CentOS 7

How To Change Mysql Root Password

Share on LinkedIn

To change or update the root mysql user password from thispw, you can set the new password to thatpw by running this command:

mysqladmin -u root -p'thispw' password 'thatpw'

That’s it.

Windows Snipping Tool Gone

Share on LinkedIn

Somehow, my very useful Windows Snipping tool had disappeared from my computer. No trace of it. I know there are others and there will be others so if you find yourself in a similar situation, just follow these steps:

1. Create a shortcut on your desktop. You can do this by doing a right-mouse click on your desktop and selecting “New” -> “Shortcut”

2. When prompted to type the location of the item, copy and paste this into the location:    %windir%\system32\SnippingTool.exe

3. Click Next, name your new shortcut (I made mine Snipping Tool) and then Finish.

4. From there, you can click and drag it to your start menu where you can pin it to your task bar and so on.

CentOS 7

Bash Shell Script to Backup a Directory

Share on LinkedIn

Here’s a simple little script to backup your web directory. This works on CentOS 7 with no problem. This script backs up the /var/www/html directory.

Create scripts directory

mkdir /home/scripts

Create directory to store backup files

mkdir /home/backups

Create the script

vi /home/scripts/www-backup.sh
#!/bin/bash
SRCDIR="/var/www/html/"
DESTDIR="/home/backups/"
FILENAME=www-backup-$(date +%-Y%-m%-d).tgz
tar --create --gzip --file=$DESTDIR$FILENAME $SRCDIR

Description of script elements
Source Directory
Destination Directory
Filename to include date stamp
Create and Compress file using tar command

Ensure execute permissions are set on the script

chmod +x www-backup.sh

Execute the script
To run the script, just type the following from within the /home/scripts directory

./www-backup.sh

This will create a file similar to www-backup-2015317.tgz in your /home/backups directory.

To extract or unzip from backup
Navigate to /home/backups and run the following tar command to extract:

tar -zxvf www-backup-2015317.tgz