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 }'