Back up MySQL Databases with a Simple Bash Script | How To Use A Simple Bash Script To Backup MySQL Database | linux mint mysql auto backup bash script

How To Use A Simple Bash Script To Backup MySQL Database
A simple bash script to take a MySQL database backup, archive it, and store it locally.

In .deb based os we can do this as follows:

Bash script

######################**START**#####################
#!/bin/bash

export PATH=/bin:/usr/bin:/usr/local/bin
TODAY=`date +"%d%b%Y"`

################## Update below values  ########################

DB_BACKUP_PATH='/backup/dbbackup'
MYSQL_HOST='192.168.1.235'
MYSQL_PORT='3306'
MYSQL_USER='root'
MYSQL_PASSWORD='1234'
DATABASE_NAME='dbname'

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


mkdir -p ${DB_BACKUP_PATH}/${TODAY}
echo "Backup started for database - ${DATABASE_NAME}"



mysqldump -h ${MYSQL_HOST} \
-P ${MYSQL_PORT} \
-u ${MYSQL_USER} \
-p${MYSQL_PASSWORD} \
${DATABASE_NAME} | gzip > ${DB_BACKUP_PATH}/${TODAY}/${DATABASE_NAME}-${TODAY}.sql.gz

if [ $? -eq 0 ]; then
  echo "Database backup successfully completed"
else
  echo "Error found during backup"
  exit 1

fi

######################**END**#######################

Use Terminal, Finally run the script to achieve this.:

Cron allows you to schedule this script to run regularly.


Post a Comment

0 Comments