MySQL automatic backup script in Linux-first version

Source: Internet
Author: User
Tags mysql connect mysql automatic backup mysql backup
I wrote a MySQL backup script in Linux. This is the first version. Only one database can be backed up. Every night at, I run the script with a timer and it will be automatically backed up. Coming soon

I wrote a MySQL backup script in Linux. This is the first version. Only one database can be backed up. Every night at, I run the script with a timer and it will be automatically backed up. Coming soon

I wrote a MySQL backup script in Linux. This is the first version. Only one database can be backed up. Every night at, I run the script with a timer and it will be automatically backed up. In the future, multiple databases can be backed up. If the backup fails, text messages will be sent to the mobile phone and expired backups will be cleared automatically.

MySQL automatic backup script in Linux-Version 2

  • #! /Bin/bash
  • # Script: backup_mysql
  • # Version: Release 1.0
  • # Author:
  • # Date: 2011-10-26

  • Backup_time = 'date + % Y % m % d % H % m' # define the backup detailed time
  • Backup_Ymd = 'date + % Y % m % d' # define the year, month, and day time in the Backup Directory
  • Backup_7ago = 'date-d' 7 days ago '+ % Y % m % d' # date before seven days
  • Backup_dir =/backup/mysql/$ backup_Ymd # Full backup folder path
  • Backup_database = "dba-china" # Name of the database to be backed up
  • Mysql_user = "root"
  • Mysql_password = "root"
  • # Mysql_host = "localhost"
  • # MySQL _ Port = "3306"

  • # Determine whether MYSQL is started. If mysql is not started, the backup will exit.
  • Mysql_ps = 'ps-ef | grep mysql | wc-l'
  • Mysql_listen = 'netstat-an | grep LISTEN | grep $ mysql_port | wc-l'
  • If [[$ mysql_ps = 0]-o [$ mysql_listen = 0]; then
  • Echo "ERROR: MySQL is not running! Backup stop! "
  • Exit;
  • Else
  • Echo "MySQL running OK! "
  • Fi

  • # Connect to the mysql database. If the connection fails, the backup will exit.
  • Mysql-u $ mysql_user-p $ mysql_password <
  • Use mysql;
  • Select host, user from user where user = 'root' and host = 'localhost ';
  • Exit
  • End

  • Flag = 'echo $? '
  • If [$ flag! = "0"]; then
  • Echo "error: Can't connect mysql server! Backup stoped! "
  • Exit;
  • Else
  • Echo "MySQL connect OK! Backup start ......"
  • # Db = 'echo $ backup_database | cut-d', '-f1'
  • Db = $ backup_database
  • If [$ db = '']; then
  • Echo "ERROR: no database to backup! "
  • Else
  • Echo "database $ db backup start ..."
  • 'Mkdir-p $ backup_dir'
  • 'Mysqldump-u $ mysql_user-p $ mysql_password $ db> $ backup_dir/$ db $ backup_time. SQL'
  • Echo "database $ db backup success! "
  • Fi

  • Fi
  • ,

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.