Mysql Automatic backup Script

Source: Internet
Author: User
Tags mysql automatic backup mysql backup

Turn from:

Mysql Automatic Backup Script 2--Iteye technology website
http://kangh.iteye.com/blog/2309091

Backup scenarios:

Backup Host: 192.168.10.11

Database server: 192.168.10.22

Backup content: Remote backup of XXXX and YYYY libraries in MySQL server, daily 2:30, each library backed up as a standalone. sql file, and then compressed into. tar.gz format, the file name embedded in the date and time the backup was performed.

Work Preparation:

1, ensure that the network between the backup host and the database server to maintain a smooth connection, there can be no network link failure, firewalls and other obstacles.

2, the MySQL service must allow the backup host remote access, and authorized users to query the XXXX and YYYY library.

Here you can create a dedicated database backup account "KangHui" Allow the backup host 192.168.10.11 to connect to the MySQL database and authorize read access to the XXXX and YYYY libraries, and when using the logical Backup tool mysqldump, you need to set select and lock and tables permissions on the library.

Java code
    1. Mysql>grant select,lock tables on xxxx.*to ' KangHui ' @' 192.168.10.11 ' identified by ' 123456 ';
    2. Mysql>grant select,lock tables on yyyy.*to ' KangHui ' @' 192.168.10.11 ' identified by ' 123456 ';

After you set access permissions, test database access on the backup server, and query authorization is valid.

Java code
    1. [Root@localhost ~] #mysqldump-u root-p123456-h 192.168. 10.22--databases xxxx > ceshi.sql
    2. [Root@localhost ~] #ls-lh ceshi.sql

Below is a MySQL backup script that can be modified to suit your needs.

Java code
  1. [Root@localhost ~] #mkdir-P/opt/mysql_bak/
  2. [Root@localhost ~] #cd/opt/mysql_bak/
  3. [Root@localhost mysql_bak]# vim mysql_bak.sh
  4. #!/bin/bash
  5. #auto Backup MySQLdb
  6. #by authors KangHui 2016-06-
  7. #这是一个简单化到MYSQL数据库逻辑备份脚本
  8. #1. Define database links, Target library information
  9. my_user="Root"
  10. my_pass="123456"
  11. my_host="192.168.10.22"
  12. my_conn="-u $MY _user-p$my_pass-h $MY _host"
  13. my_db1="xxxx"
  14. my_db2="yyyy"
  15. #2. Define backup directory, tool, time, file name
  16. bf_dir="/opt/mysql_bak"
  17. bf_cmd="/usr/bin/mysqldump"
  18. Bf_time="Date +%y%m%d-%h%m"
  19. name_1="$MY _db1-$BF _time"
  20. name_2="$MY _db2-$Bf _time"
  21. #3 pilot out for. SQL script, then compress (remove source files after packaging)
  22. CD $BF _dir
  23. $BF _cmd $MY _conn--databases $MY _db1 > $name _1.sql
  24. $BF _cmd $MY _conn--databases $MY _db2 > $name _2.sql
  25. /bin/tar zcf $name _1.tar.gz $name _1.sql--remove &>/dev/null
  26. /bin/tar zcf $name _2.tar.gz $name _2.sql--remove &>/dev/null

Set Permissions and test:

Java code
    1. [Root@localhost Mysql_bak] #chmod +x mysql_bak.sh
    2. [Root@localhost mysql_bak]#./mysql_bak.sh
    3. [Root@localhost Mysql_bak] #ls-lh/opt/mysql_bak/*.gz

Finally, add the script to the scheduled task.

Java code
    1. [Root@localhost ~] #crontab-e
    2. 2 * * * /opt/mysql_bak/mysql_bak.sh

Mysql Automatic backup Script

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.