• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

MySQL backup bash script

September 6, 2013 by Sharad Chhetri 1 Comment

This is a simple mysql backup script.The script can be used manually as well as in crontab.
Edit the below given script and provide the information required in the script.
The script will take the backup in .sql format later it will compress the .sql file and in end send the mail about the status of mysql backup.

Requirement

(1) For getting mail from Server install mail command [mailx(in Red Hat/CentOS) or mail-utils in Ubuntu/Debian]

yum install mailx   (For Red Hat and CentOS)

OR

apt-get install mail-utils (For Debian and Ubuntu)

(2) Create directory /root/database-backup/

mkdir -p /root/database-backup/

(3)Below given is the MySQL backup script

Note: Edit the values in bash script as per requirement

Create a file called db-backupscript.sh

vi db-backupscript.sh

And paste the below contents

#!/bin/bash
# mysql backup script
# Author: sharad
# 4-Sept-2013
#

MYSQLUSER=Mysql-user
PASSWORD=Mysql-user-Passw0rd
DBNAME=Database-Name

### Give MYSQL Server IP address or FQDN, For eg. 127.0.0.1 or localhost
MYSQLHOST=127.0.0.1

BACKUPPATH=/root/database-backup/$DBNAME-`date +%F-%H%M%S`

#You can give multiple email id in MAILTO variable by using comma (,) for eg. MAILTO=abc@example.com,xyz@example.com
MAILTO=your-emailid@example.com

mysqldump -h$MYSQLHOST -u$MYSQLUSER -p$PASSWORD $DBNAME > $BACKUPPATH

### compressing the file 
gzip $BACKUPPATH

FILESIZE=$( du -sh $BACKUPPATH.gz )

if [ ! -f  $BACKUPPATH.gz ]; then
    echo "$BACKUPPATH.gz File not found!, Database Name: $DBNAME" | mail -s "$DBNAME backup failed" $MAILTO
else
    echo "$BACKUPPATH.gz File found,Database Name: $DBNAME, Actual size after compression is $FILESIZE " | mail -s "DBNAME database backup is done" $MAILTO
fi
 

(4) Give execute permission to script and should be executable and access by owner of file

chmod 700 db-backupscript.sh

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. send email after mysql backup through bash script in simple way
  2. postgres database backup script using database user password inside
  3. Cassandra backup script on linux system
  4. Find mysql commands history without mysql server login
  5. Xen Server License Nagios Plugin (bash script)
  6. Nagios plugin check_mailfromd bash script
  7. How to Use Expect In Bash Script
  8. How To Use opt In Bash Script
  9. Set and reset user password by bash script
  10. Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

Filed Under: Linux Tagged With: mysql

Reader Interactions

Comments

  1. Leo says

    November 15, 2013 at 11:20 am

    Nice script, Instead of the variable name backuppath, you can give it dbname , it becomes less confusing later

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

sed: -e expression #1, char 24: Invalid range end

Convert hyphen to underscore in between all filenames shell script

How to configure htdigest in nagios server

How to zip directory in linux explained with examples

How to find when Operating system was installed in linux CentOS and Red Hat

WordPress host IP changed not able to open wp-admin and site page looks scattered

error command ‘gcc’ failed with exit status 1 in Ubuntu

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy