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. 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

  1. #! /Bin/bash
  2. # Script: backup_mysql
  3. # Version: Release 1.0
  4. # Author: www.dba-china.com
  5. # Date: 2011-10-26

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

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

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

  30. Flag = 'echo $? '
  31. If [$ flag! = "0"]; then
  32. Echo "error: Can't connect mysql server! Backup stoped! "
  33. Exit;
  34. Else
  35. Echo "MySQL connect OK! Backup start ......"
  36. # Db = 'echo $ backup_database | cut-d', '-f1'
  37. Db = $ backup_database
  38. If [$ db = '']; then
  39. Echo "ERROR: no database to backup! "
  40. Else
  41. Echo "database $ db backup start ..."
  42. 'Mkdir-p $ backup_dir'
  43. 'Mysqldump-u $ mysql_user-p $ mysql_password $ db> $ backup_dir/$ db $ backup_time. SQL'
  44. Echo "database $ db backup success! "
  45. Fi

  46. Fi

Related Article

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.