• Home
  • Services
    • DBA Support
    • DBA Consultancy Services
    • PostgreSQL Support
    • Website Maintenance
  • Courses

    About Courses

    • List Of Courses
    • Become an Instructor
    Greenplum Database

    Greenplum Database

    $2,000.00 $1,500.00
    Read More
  • Company
    • FAQs
    • About Us
    • Contact
  • Events
  • Portfolio
  • Blogs
    • Blog – RayaFeeL
    • Blog – PostgreSQL Support
    • Blog – PostgreSQL Migration
    • Blog – All DB’s
    • Blog – Linux
    • Blog – Medical Coding
      • Cart

        0

    Have any question?
    (+91)8838953252
    ITsupport@rayafeel.com
    RegisterLogin
    RayaFeeL
    • Home
    • Services
      • DBA Support
      • DBA Consultancy Services
      • PostgreSQL Support
      • Website Maintenance
    • Courses

      About Courses

      • List Of Courses
      • Become an Instructor
      Greenplum Database

      Greenplum Database

      $2,000.00 $1,500.00
      Read More
    • Company
      • FAQs
      • About Us
      • Contact
    • Events
    • Portfolio
    • Blogs
      • Blog – RayaFeeL
      • Blog – PostgreSQL Support
      • Blog – PostgreSQL Migration
      • Blog – All DB’s
      • Blog – Linux
      • Blog – Medical Coding
        • Cart

          0

      Blog

      • Home
      • Blog
      • Blog
      • MariaDB Backup Script Mail Notifications Alert

      MariaDB Backup Script Mail Notifications Alert

      • Posted by 2ndnijam
      • Categories Blog
      • Date May 12, 2019
      • Comments 0 comment

      Try following script , This script will take mysql database backup automatically  and remove the old backup file which is over 3 days and once backup is completed means it will send mail confirmation

      If you know the mysql root password you can use following script , if you don’t know the mysql root password you can use the second script

      Before running this script please create a directory for backup location as ” backup ”

      First script with ROOT password

      #!/bin/bash
      # Shell script to backup MySQL database
      
      # Set these variables
      MyUSER="root"	# DB_USERNAME
      MyPASS="12345"	# DB_PASSWORD
      MyHOST="localhost"	# DB_HOSTNAME
      
      # Backup Dest directory
      DEST="/root/backup" # 
      
      # Email for notifications
      EMAIL="nijamutheen060@gmail.com"
      
      # How many days old files must be to be removed
      DAYS=3
      
      # Linux bin paths
      MYSQL="$(which mysql)"
      MYSQLDUMP="$(which mysqldump)"
      GZIP="$(which gzip)"
      
      # Get date in dd-mm-yyyy format
      NOW="$(date +"%d-%m-%Y_%s")"
      
      # Create Backup sub-directories
      MBD="$DEST/$NOW/mysql"
      install -d $MBD
      
      # DB skip list
      SKIP="information_schema
      another_one_db"
      
      # Get all databases
      DBS="$($MYSQL -h $MyHOST -u $MyUSER -p$MyPASS -Bse 'show databases')"
      
      # Archive database dumps
      for db in $DBS
      do
          skipdb=-1
          if [ "$SKIP" != "" ];
          then
      		for i in $SKIP
      		do
      			[ "$db" == "$i" ] && skipdb=1 || :
      		done
          fi
       
          if [ "$skipdb" == "-1" ] ; then
          	FILE="$MBD/$db.sql"
      	$MYSQLDUMP -h $MyHOST -u $MyUSER -p$MyPASS $db > $FILE
          fi
      done
      
      # Archive the directory, send mail and cleanup
      cd $DEST
      tar -cf $NOW.tar $NOW
      $GZIP -9 $NOW.tar
      
      echo "MySQL backup is completed! Backup name is $NOW.tar.gz" | mail -s "MySQL backup" $EMAIL
      rm -rf $NOW
      
      # Remove old files
      find $DEST -mtime +$DAYS -exec rm -f {} \;

      Without MySQL ROOT Password :

      #!/bin/bash
      # Shell script to backup MySQL database
      
      # Set these variables
      MyUSER="root"	   # DB_USERNAME
      MyHOST="localhost" # DB_HOSTNAME
      
      # Backup Dest directory
      DEST="/root/backup" # 
      
      # Email for notifications
      EMAIL="nijamutheen060@gmail.com"
      
      # How many days old files must be to be removed
      DAYS=3
      
      # Linux bin paths
      MYSQL="$(which mysql)"
      MYSQLDUMP="$(which mysqldump)"
      GZIP="$(which gzip)"
      
      # Get date in dd-mm-yyyy format
      NOW="$(date +"%d-%m-%Y_%s")"
      
      # Create Backup sub-directories
      MBD="$DEST/$NOW/mysql"
      install -d $MBD
      
      # DB skip list
      SKIP="information_schema
      another_one_db"
      
      # Get all databases
      DBS="$($MYSQL -h $MyHOST -u $MyUSER  -Bse 'show databases')"
      
      # Archive database dumps
      for db in $DBS
      do
          skipdb=-1
          if [ "$SKIP" != "" ];
          then
      		for i in $SKIP
      		do
      			[ "$db" == "$i" ] && skipdb=1 || :
      		done
          fi
       
          if [ "$skipdb" == "-1" ] ; then
          	FILE="$MBD/$db.sql"
      	$MYSQLDUMP -h $MyHOST -u $MyUSER  $db > $FILE
          fi
      done
      
      # Archive the directory, send mail and cleanup
      cd $DEST
      tar -cf $NOW.tar $NOW
      $GZIP -9 $NOW.tar
      
      echo "MySQL backup is completed! Backup name is $NOW.tar.gz" | mail -s "MySQL backup" $EMAIL
      rm -rf $NOW
      
      # Remove old files
      find $DEST -mtime +$DAYS -exec rm -f {} \;

       

      Note : while running this script may  get below error

      mysqldump: Got error: 1142: "SELECT, LOCK TABLES command denied to user 'root'@'localhost' for table 'accounts'" when using LOCK TABLES

      Solution :

      Do not dump any tables of performance_schema, information_schema or mysql. The first two are not ‘real’ tables, and they cannot be reloaded anyway.

      95% of Production servers are not helped by, or even hurt by, turning on the Query cache. Please elaborate on why you are even attempting to use the QC.

      so you have add it those databases in skipping list , so script should be below shape

      #!/bin/bash
      # Shell script to backup MySQL database
      
      # Set these variables
      MyUSER="root"      # DB_USERNAME
      MyHOST="localhost" # DB_HOSTNAME
      
      # Backup Dest directory
      DEST="/root/backup" #
      
      # Email for notifications
      EMAIL="nijamutheen060@gmail.com"
      
      # How many days old files must be to be removed
      DAYS=3
      
      # Linux bin paths
      MYSQL="$(which mysql)"
      MYSQLDUMP="$(which mysqldump)"
      GZIP="$(which gzip)"
      
      # Get date in dd-mm-yyyy format
      NOW="$(date +"%d-%m-%Y_%s")"
      
      # Create Backup sub-directories
      MBD="$DEST/$NOW/mysql"
      install -d $MBD
      
      # DB skip list
      SKIP="information_schema
      performance_schema"
      
      # Get all databases
      DBS="$($MYSQL -h $MyHOST -u $MyUSER  -Bse 'show databases')"
      
      # Archive database dumps
      for db in $DBS
      do
          skipdb=-1
          if [ "$SKIP" != "" ];
          then
                 for i in $SKIP
                 do
                      [ "$db" == "$i" ] && skipdb=1 || :
                 done
          fi
      
          if [ "$skipdb" == "-1" ] ; then
              FILE="$MBD/$db.sql"
              $MYSQLDUMP -h $MyHOST -u $MyUSER  $db > $FILE
          fi
      done
      
      # Archive the directory, send mail and cleanup
      cd $DEST
      tar -cf $NOW.tar $NOW
      $GZIP -9 $NOW.tar
      
      echo "MySQL backup is completed! Backup name is $NOW.tar.gz" | mail -s "MySQL backup" $EMAIL
      rm -rf $NOW
      
      # Remove old files
      find $DEST -mtime +$DAYS -exec rm -f {} \;

       

      Tag:backup, mail notification, MariaDB, script

      • Share:
      2ndnijam

      Previous post

      Mail Alert Notification for MariaDB Service is Down
      May 12, 2019

      Next post

      PostgreSQL Health checkup script with mail notification
      May 15, 2019

      Leave A Reply Cancel reply

      You must be logged in to post a comment.

      Login with:

      Login with Google Login with Twitter Login with LinkedIn Login with Microsoft


      Search

      ADVERTISEMENT

      Latest Posts

      PostgreSQL Patching version 9, 10,11
      10Oct2019
      Tools for PostgreSQL
      16Sep2019
      Postgres user creation and restrict DDL & database access
      13Sep2019
      PostgreSQL SSL Setup
      07Sep2019
      How to DELETE current XLOG / WAL LOG in postgresql database ?
      19Aug2019

      Latest Courses

      PostgreSQL Database

      PostgreSQL Database

      $600.00 $500.00
      Greenplum Database

      Greenplum Database

      $2,000.00 $1,500.00

      Preview Course

      Free

      Recent Forum Topics

      • thought behind whiteboard activity
      • Are you going to take your first ste
      • How to start working on an application?
      • please let me know pre requirements to increase work_mem
      • how to copy some data in one table to another table in postgres

      2ndquadrant.in

      (+91) 8838953252

      ITsupport@rayafeel.com

      Company

      • About Us
      • Contact
      • Our Team
      • Blog

      COURSES

      • List Of Course
      • Become An Instructor
      • Events
      • Postgres Support Blog

      Support

      • DBA Support
      • Consultancy Services
      • Postgres Migration Blogs
      • Forum

      Recommend

      • Groups
      • Login
      • FAQs
      • SignUp

      IT Services by rayafeel.com. Powered by Rayafeel Technologies Pvt Ltd.

      • Privacy
      • Terms

      Become An Instructor?

      Join thousand of instructors and earn money hassle free!

      Get Started Now

      Login with:

      Login with Google Login with Twitter Login with LinkedIn Login with Microsoft

      Login with your site account

      Lost your password?

      Not a member yet? Register now

      Register a new account

      Are you a member? Login now