The perfect MySQL backup script

Source: Internet
Author: User
Tags mysql backup

Transferred from: https://www.cnblogs.com/leffss/p/7832047.html

#!/bin/bash# fully-prepared, generally on-board execution, for small-medium MySQL database # Delete 15 days ago Backup #fafu_li# time: 2015.08.10source/etc/profile #加载系统环境变量source ~/. Bash_profile #加载用户环境变量set-o nounset #引用未初始化变量时退出 #set-o errexit #执行shell命令遇到错误时退出user = "root" pas Sword= "123456" host= "localhost" port= "3306" #需备份的数据库, array db= ("test") #备份时加锁方式, #MyISAM为锁表--lock-all-tables,# InnoDB for lock line--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 "#建立备份目录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>&1echo "Start Backup database: ${db[*]}" #备份并压缩backup_sql () {dbname=$1 backup_name= "${dbname}_${date}.sql" #-r Backup stored procedures, functions, triggers 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 $backup _name.tar.gz $backup _name size=$ (du $backup _name.tar.gz-sh | awk ' {print $} ') RM-RF $backup _name echo "$date backup $dbname ($size) Successful" Else CD $backup _path RM-RF $backup _name EC Ho "$date backup $dbname Failed" fi} #循环备份length =${#db [@]}for ((i = 0; i < $length; i++));  Do Backup_sql ${db[$i]} >> $backup _log 2>&1doneecho "Backup end, results view $backup _log" du $backup _path/* $date *-sh | awk ' {print ' file: ' $ $ ', size: ' $ '

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