-->
🏠 🔍
SHAREOLITE

Beginners Linux shell script to backup MySQL database


Below is a ready to use Linux shell script which can be used to backup MySQL database and store the backup file in archive. Simple copy paste the below code (highlighted in blue) and execute to get your backup working.

There are few variables provided to configure your required db user , db password , backup storage directory , host and DB ( those highlighted in bold )


#!/bin/sh
#################################################################################
# Configure the backup directory here.
# Ex. BACKUP_DIR="/Backup/"
#################################################################################
BACKUP_DIR="/Backup/"

#################################################################################

# Configure the DB user name here.
# Ex. DBUSER="scott"
#################################################################################
DBUSER="scott"

#################################################################################

# Configure the DB user password here.
# Ex. DBPASS="wsas"
#################################################################################
DBPASS="tiger"

#################################################################################

# Connfigure the hostname here.
# Ex. DBHOST="192.168.10.1" . For localhost enter 127.0.0.1
#################################################################################
DBHOST="127.0.0.1"

#################################################################################

# Configure database name.
# Ex. DB="WSAS"
#################################################################################
DB="EMPLOYEE"

#########################################################################################################


echo -e "`date` - DB Backup started"


mkdir -p $BACKUP_DIR


cd $BACKUP_DIR


mysqldump -h $DBHOST --routines --triggers -u $DBUSER -p$DBPASS --databases $DB > $DB-DBBkp-`date +%b-%a`.sql


tar -zcvf $DB-DBBkp-`date +%b-%a`.tar.gz $DB-DBBkp-`date +%b-%a`.sql --remove-files


echo "`date` - DB backup taken. File $BACKUP_DIR/$DB-DBBkp-`date +%b-%a`.tar.gz "


#########################################################################################################


Hope it helps to some beginners !!
Comments

–>