Mysql backup script (mysqldump) in CentOS)

Source: Internet
Author: User
Tags mysql backup

Mysql backup script (mysqldump) in CentOS)

#! /Bin/bash # Full backup mode, generally executed on the slave machine, suitable for small and medium mysql Databases # Delete backups 15 days ago # Author: fafu_li # Time: 2015.08.10source/etc/profile # load the system environment variable source ~ /. Bash_profile # load the user environment variable set-o nounset # reference the uninitialized variable and exit # set-o errexit # exit user = "root" password = "123456" when an error occurs during shell Command Execution" host = "localhost" port = "3306" # database to be backed up, array db = ("test") # lock mode during backup, # MyISAM is the lock table -- lock-all-tables, # InnoDB locks rows -- single-transactionlock = "-- single-transaction" mysql_path = "/usr/local/mysql" backup_path = "$ {mysql_path}/backup" date = $ (date + % Y-% m-% d _ % H-% M-% S) day = 15backup_log = "$ {mysql_path}/backup. log "# create a backup directory if [! -E $ backup_path]; then mkdir-p $ backup_pathfi # Delete the previous backup find $ backup_path-type f-mtime + $ day-exec rm-rf {}\;> /dev/null 2> & 1 echo "start to back up the database: $ {db [*]}" # back up and compress backup_ SQL () {dbname = $1 backup_name = "$ {dbname }_$ {date }. SQL "#-R backup stored procedures, functions, trigger mysqldump-h $ host-P $ port-u $ user-p $ password $ lock -- default-character-set = utf8 -- flush-logs-R $ dbname> $ backup_path/ $ backup_name if [[$? = 0]; then cd $ backup_path tar zcpvf unzip backup_name.tar.gz $ backup_name size = $ (du your backup_name.tar.gz-sh | awk '{print $1 }') rm-rf $ backup_name echo "$ date backup $ dbname ($ size) "else cd $ backup_path rm-rf $ backup_name echo" $ date backup $ dbname failed "fi} # cyclic backup length =$ {# db [@]} for (I = 0; I <$ length; I ++); do backup_ SQL $ {db [$ I] }>>$ backup_log 2> & 1 doneecho "backup is complete, result: View $ backup_log "du $ backup_path/* $ date *-sh | awk '{print" file: "$2", size: "$1 }'

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.