3 easy steps for scheduling regular backup of your MySql database: Be safe

Apart from my previous article on safety features ,it is a well known fact , that it is extremely important to take backup of your MySql database of your live website.

.

Just follow below 3 steps and everything is under control :

Step 1: Configure backup directory on server

mkdir /var/lib/mysqlbackup
cd /var/lib/mysqlbackup

.

Step 2: Make the backup script

    vi dbbackup.sh

.

Paste the content as below and change the username, password and dbname as required

#!/bin/sh
# Username to access the MySQL server
USERNAME="username"
# Username to access the MySQL server
PASSWORD="password"
# List of DBNAMES for Backup
DBNAME="dbname"
#date timestamp for log message
DATE=`/bin/date +%Y-%m-%d_%Hh%Mm`
#output file
OUTDIR="/var/lib/mysqlbackup/"
OUTFILE="ip_bindass".$DATE."sql.gz"
#working directory
DIR="/var/lib/mysqlbackup/"
#cd $DIR
# MySQL Backup
/usr/bin/mysqldump --database $DBNAME --opt --single-transaction -u$USERNAME -p$PASSWORD | /usr/bin/gzip -9 > $OUTDIR$OUTFILE

.

Change the permission of backup script

chmod +x dbbackup.sh

.

Step3: Schedule the backup script in crontab

crontab -e

.

Add below like to take backup everyday at 3.20 pm

20 15 * * * /var/lib/mysqlbackup/dbbackup.sh

.

All done!
Cheers!!

Scroll to Top