Xtrabackup Automatic Backup scripts

Source: Internet
Author: User
Tags mysql backup

#backup. Sh#!/bin/sh#on Xtrabackup 2.2.8# When it first executes it checks for a full backup or creates a full-library backup first # when you run it again, It will make incremental backups based on the previous full or incremental backup according to the settings in the script #[email protected]innobackupex_path=innobackupex # Innobackupex's command innobackupexfull=/usr/local/xtrabackup/bin/$INNOBACKUPEX _path #INNOBACKUPEX的命令路径 # MYSQL target server and username and password mysql_cmd= "--host=192.168.5.189--user=root--password=password--port=3306" mysql_up= "--user= Root--password= ' password '--port=3306 "#mysqladmin的用户名和密码TMPLOG ="/tmp/innobackupex.$$.log "my_cnf=/usr/local/ MYSQL/MY.CNF #mysql的配置文件MYSQL =/usr/bin/mysqlmysql_admin=/usr/bin/mysqladminbackup_dir=/backup # Backup Home directory Fullbackup_ dir= $BACKUP _dir/full # Full Library backup directory incrbackup_dir= $BACKUP _dir/incre # Incremental backup directory fullbackup_interval=86400 # The interval period of the full-library backup, Time: Seconds Keep_fullbackup=1 # Keep At least a few full-library backups logfiledate=backup. ' Date +%y%m%d%h%m '. txt# start time started_time= ' Date +%s ' ########### ################################################################### display error and exit ##################################### ####################################### #error () {echo]$ "1>&2 exit 1} # Check the execution environment if [!-X $INNOBACKUPEXFULL]; Then error "$INNOBACKUPEXFULL is not installed or is not linked to/usr/bin." fi if [!-D $BACKUP _dir]; Then error "Backup destination folder: $BACKUP _dir does not exist." Fi mysql_status= ' NETSTAT-NL | awk ' nr>2{if ($4 ~/.*:3306/) {print "yes"; Exit 0}} "if [" $mysql _status "! =" Yes "];then error" MySQL does not start running. " Fi if! ' Echo ' exit ' | $MYSQL-S $MYSQL _cmd '; Then error "The database user name or password provided is incorrect!" Fi # Backup Header information echo "----------------------------" Echoecho "$0:mysql backup Script" echo "starts at: ' Date +%f '%T '%w '" Echo #新建全备和差异备份的目录 Mkdir-p $FULLBACKUP _dirmkdir-p $INCRBACKUP _dir# Find the latest full backup latest_full_backup= ' Find $FULLBACKUP _dir-mindepth 1- MaxDepth 1-type d-printf "%p\n" | Sort-nr | Head-1 ' # Find the most recently modified backup time latest_full_backup_created_time= ' stat-c%Y $FULLBACKUP _dir/$LATEST _full_backup ' # Perform a full backup if the full backups are valid for an incremental backup if ["$LATEST _full_backup"-a ' expr $LATEST _full_backup_created_time + $FULLBACKUP _interval + 5 '-ge $STARTED _time]; then# if the latest full-time is not expired, name the new directory under the incremental backup directory with the latest full-file name echo-e "full backup $latest_full_backup notExpires, will be based on $latest_full_backup name as an incremental backup base directory name "echo" "new_incrdir= $INCRBACKUP _dir/$LATEST _full_backupmkdir-p $NEW _ incrdir# find out whether the latest incremental backup exists. Specify a backup path as the basis for an incremental backup latest_incr_backup= ' Find $NEW _incrdir-mindepth 1-maxdepth 1-type d-printf " %p\n "| Sort-nr | Head-1 ' if [! $LATEST _incr_backup]; thenincrbasedir= $FULLBACKUP _dir/$LATEST _full_backupecho-e "Incremental backup will be $incrbasedir as the backup base directory" echo "elseincrbasedir=$ INCRBACKUP_DIR/${LATEST_FULL_BACKUP}/${LATEST_INCR_BACKUP}ECHO-E "Incremental backup will use $incrbasedir as backup base directory" echo "" Fiecho "using $ Incrbasedir as the base directory for this incremental backup. " $INNOBACKUPEXFULL--defaults-file= $MY _cnf--use-memory=4g $MYSQL _cmd--incremental $NEW _incrdir-- Incremental-basedir $INCRBASEDIR > $TMPLOG 2>&1# Keep a detailed log of backups cat $TMPLOG >/backup/$logfiledateif [-Z ' tail -1 $TMPLOG | grep ' innobackupex:completed ok! ' `" ] ; then echo "$INNOBACKUPEX command execution failed:"; echo echo-e "----------$INNOBACKUPEX _path error----------" Cat $TMPLOG rm-f $TMPLOG exit 1fithisbackup= ' awk--"/backup CR Eated in directory/{split(\\\$0, p, \ "' \"); Print p[2]} "$TMPLOG ' Rm-f $TMPLOGecho-n" database successfully backed up to: $THISBACKUP "echo# hint should keep the backup file starting point latest_full_backup= ' Find $ Fullbackup_dir-mindepth 1-maxdepth 1-type d-printf "%p\n" | Sort-nr | Head-1 ' new_incrdir= $INCRBACKUP _dir/$LATEST _full_backuplatest_incr_backup= ' Find $NEW _incrdir-mindepth 1-maxdepth 1 -type d-printf "%p\n" | Sort-nr | Head-1 ' res_full_backup=${fullbackup_dir}/${latest_full_backup}res_incre_backup= ' dirname ${incrbackup_dir}/${ Latest_full_backup}/${latest_incr_backup} ' echoecho-e ' \e[31m Note:---------------------------------------------- -----------------------------------. \e[m ' #红色echo-e ' must keep $keep_fullbackup fully prepared ${res_full_backup} and ${res_incre_ Backup} for all incremental backups in the directory. " Echo-e ' \e[31m Note:---------------------------------------------------------------------------------. \e[m ' # Red Echoelseecho "*********************************" echo-e "performing a completely new full backup ... Please wait a moment ... "echo" ********************************* "$INNOBACKUPEXFULL--defaults-file= $MY _cnf--use-memory=4G $MYSQL _cmd $FULLBACKUP _dir > $TMPLOG 2>&1 #保留一份备份的详细日志cat $TMPLOG >/backup/$logfiledateif [-z] ' TAIL-1 $ Tmplog | grep ' innobackupex:completed ok! ' `" ] ; then echo "$INNOBACKUPEX command execution failed:"; echo echo-e "----------$INNOBACKUPEX _path error----------" Cat $TMPLOG rm-f $TMPLOG exit 1fi thisbackup= ' awk--"/backup C Reated in directory/{split (\\\$0, p, \ "' \"), Print P[2]} "$TMPLOG ' Rm-f $TMPLOGecho-n" database successfully backed up to: $THISBACKUP "echo# Backup file that should be retained latest_full_backup= ' find $FULLBACKUP _dir-mindepth 1-maxdepth 1-type d-printf "%p\n" | Sort-nr | Head-1 ' res_full_backup=${fullbackup_dir}/${latest_full_backup}echoecho-e ' \e[31m Note:-------------------------- -------------------------------------------------------. \e[m ' #红色echo-E "No incremental backup, you must keep $keep_fullbackup full standby ${res_ Full_backup}. " Echo-e ' \e[31m Note:---------------------------------------------------------------------------------. \e[m ' # Red echofi# Delete expired fully prepared ECHO-E "find expire backup file...........waiting ..." echo-e "Looking forExpired full file and delete ">>/backup/$logfiledatefor efile in $ (/usr/bin/find $FULLBACKUP _dir/-mtime +6) doif [-D ${efile}]; Thenrm-rf "${efile}" echo-e "Delete expired full file: ${efile}" >>/backup/$logfiledateelif [-f ${efile}]; Thenrm-rf "${efile}" echo-e "Delete expired full file: ${efile}" >>/backup/$logfiledatefi;d oneif [$?-eq "0"];then Echo Echo-  E "No expired full files can be deleted" Fiechoecho "completed at: ' Date +%f '%T '%w '" Exit 0-------------------------execution results such as: warning:using a password on The command line interface can insecure.----------------------------./backup.sh:mysql backup script started at: 2015-04-03 16:35:11 5* Performing a completely new full backup ... Please wait a moment ... ********************************* database successfully backed up to:/backup/full/2015-04-03_16-35-11 Note:------------------------- --------------------------------------------------------. No incremental backup, you must keep 1 full standby/backup/full/2015-04-03_16-35-11. Note:---------------------------------------------------------------------------------. find expire backup file ............ waiting ... Expired full files that can be deleted are not found to complete in: 2015-04-03 16:35:25 5./backup.sh:mysql Backup script started at: 2015-04-03 16:35:50 5 full backup 2015-04-03_16-35-11 not expired, will be based on 2015-04-03_ 16-35-11 name as an incremental backup base directory name <span style= "White-space:pre" ></span> incremental backup will be/backup/full/2015-04-03_ 16-35-11 as backup base directory <span style= "White-space:pre" ></span> use/backup/full/2015-04-03_ 16-35-11 as the base directory for this incremental backup. Database successfully backed up to:/backup/incre/2015-04-03_16-35-11/2015-04-03_16-35-50 Note:------------------- --------------------------------------------------------------. must retain 1 copies of fully prepared/backup/full/2015-04-03_16-35-11 and/ All incremental backups in the backup/incre/2015-04-03_16-35-11 directory. Note:---------------------------------------------------------------------------------. find expire backup file ............ waiting ... Expired full files that can be deleted are not found


Xtrabackup Automatic Backup scripts

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.