Backup Script For Website and MySQL – Linux

So you have LAMP-based website and now you are looking for a Backup Script For Website and MySQL, that can Automatically Back Up Your Web Site Every Night?

Ok clear.. here I am sharing my super simple Backup Script For Website and MySQL, this Linux Backup Script will allow you to backup your website source files as well as your MySQL database, furthermore at the end backup files will be transferred on remote ftp server.

`


Website & MySQL Backup Script
Website & MySQL Backup Script

This guide assumes you have:
  1. A LAMP-based web site (Linux, Apache, MySQL and PHP/Perl/Python).
  2. Command line access to your web server via SSH
  3. Knowledge to make new directories and chmod permissions on files.
  4. Directory location of your website contents hosted on web server and MySQL database name & Credentials
  5. Access to remote FTP server

Everything in hand? Let’s get started

Scenario:-

For example your website contents are hosted on /var/www/html/broexperts.com directory and this website is having MySQL database called db_broexperts.

Now modify below script by keeping above example in mind:

My Super Simple Website and MySQL Backup Script

Now we will copy and paste below showing contents into web-backup.sh file. Follow marked settings in below script and modify as per your requirements

[BroExperts@lxweb ~]$ vi web-backup.sh
#!/bin/bash
#Purpose = Website Source & Database Backup
#Created on 04-11-2018
#Author = Hafiz Haider
#Version 1.0
################### SCRIPT START #####################
## 1: TIME STAMP
TIME=`date +{1c918b003a0fec779e46518dd4d8df22f3dc554de918030f5a1a0cfd93cb28be}b-{1c918b003a0fec779e46518dd4d8df22f3dc554de918030f5a1a0cfd93cb28be}d-{1c918b003a0fec779e46518dd4d8df22f3dc554de918030f5a1a0cfd93cb28be}y`
## 2: YOUR WEBSITE NAME
WEBSITE=BroExperts.com
## 3: HERE I DEFINE WEBSITE BACKUP FILE NAME FORMAT
FILENAME=$WEBSITE-backup-$TIME.tar.gz
## 4:LOCATION OF WEBSITE SOURCE CODE DIRECTORY ON WEB SERVER
WEBDIRECTORY=/var/www/html/broexperts
## 5: DESTINATION OF BACKUP FILE ON LOCAL SERVER
BACKUPDIR=/home/BroExperts/mybackup
## 6: MySQL DATABASE CREDENTIALS
DBUSER=db_user
DBPASS=db_password
DB=db_broexperts
## 7: WEBSITE BACKUP COMMAND
tar -cpzf $BACKUPDIR/$FILENAME $WEBDIRECTORY
## 8: DATABASE BACKUP COMMAND
mysqldump -u $DBUSER -p${DBPASS} $DB | gzip > $BACKUPDIR/dbbackup_${DB}_${TIME}.bak.gz
## 9: FINAL COMMAND TO GENERATE SINGLE ZIP FILE CONTAINING WEB AND DATABASE BACKUP
zip -rm $BACKUPDIR/Full_Backup_${WEBSITE}_${TIME}.zip $BACKUPDIR/*.gz
## 10: TRANSFER FILES TO REMOTE FTP SERVER
HOST=192.168.2.132
USER=ftpuser
PASSWORD=password
ftp -inv $HOST <<EOF
user $USER $PASSWORD
lcd $BACKUPDIR
mput Full_Backup_${WEBSITE}_${TIME}.zip
bye
EOF
################### SCRIPT END #####################
Script Action

This script will zips up your website data including database and save one copy of your website backup on local and one copy on remote ftp server.

Also read other articles related to Linux Backup

Script Explanation

To implement this script in your environment you just need to follow the comments written inside the script (each line started with ‘##1’

Execute Script

To make this script executable, you must enter following command:

[BroExperts@lxweb ~]$ chmod +x web-backup.sh 

Run Backup Script Automatically Using Cron Jobs

Let’s schedule script execution using crontab. To run it at 1:01 am every morning, your crontab would look like this:

[BroExperts@lxweb ~]$ crontab -e
1       1       *       *       *      /home/BroExperts/web-backup.sh

Using this script and automation technique, all of your website data will be regularly backed up and stored in a secure separate location. No matter the disaster – human error, hacking, corrupt files, hardware or software issues – you’ll be ready to restore your files anytime, from backups.That’s All.

If you need any help regarding this guide, please feel free to post in comments section. Thanks

Similar Posts