#backup. SH #!/bin/sh #on xtrabackup 2.2.8 # When you first execute it, it checks for a full backup, or you first create a whole-Library backup # When you run it again, It is based on the settings in the script to perform an incremental backup of the previous full or incremental backup #ocpyang @126.com Innobackupex_path=innobackupex #INNOBACKUPEX的命令 innobackupexfull=/ usr/local/xtrabackup/bin/$INNOBACKUPEX _path #INNOBACKUPEX的命令路径 #mysql目标服务器以及用户名和密码 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/ MySQL mysql_admin=/usr/bin/mysqladmin backup_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 # Full library backup interval, time: sec keep_fullbackup=1 # Keep at least several whole-store backups logfiledate= Backup. ' Date +%y%m%d%h%m ' txt #开始时间 started_time= ' date +%s ' ####################################################### ###################### # Displays errors and exits ############################################################################# error () {echo "$" 1>&2 exit 1} # Check 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 ($ ~/.*:3306/) {print ' yes '; Exit 0}} ' if [' $mysql _status '!= ' yes '];then error ' MySQL does not start running. "F I if! ' Echo ' exit ' | $MYSQL-S $MYSQL _cmd '; Then error "The database user name or password provided is incorrect!" FI # header information echo----------------------------echo echo $0:mysql backup Script "echo" begins at: ' Date +%f '%T '%w ' echo #新建全备和差异备份的目录 mkdir-p $FULLBACKUP _dir mkdir-p $INCRBACKUP _dir #查找最新的完全备份 Latest_full_ Backup= ' Find $FULLBACKUP _dir-mindepth 1-maxdepth 1-type d-printf "%p\n" | Sort-nr | Head-1 ' # Find the most recent modified backup time Latest_full_backup_created_time= ' stat-c%Y $FULLBACKUP _dir/$LATEST _full_backup ' #如果全备有效进行 Incremental backups Otherwise perform a full backup if ["$LATEST _full_backup"-a ' expr $LATEST _full_backup_created_time + $FULLBACKUP _inteRval + 5 '-ge $STARTED _time];
Then # if the latest full name is not expired, name the new directory under the incremental backup directory echo-e The full backup $latest_full_backup does not expire, the base directory name will be backed up incrementally according to $latest_full_backup name echo "" New_incrdir= $INCRBACKUP _dir/$LATEST _full_backup mkdir-p $NEW _incrdir # Find out if the most recent incremental backup exists. Specifies the path of a backup as an incremental backup Basic latest_incr_backup= ' Find $NEW _incrdir-mindepth 1-maxdepth 1-type d-printf '%p\n ' | Sort-nr | Head-1 ' if [! $LATEST _incr_backup];
Then incrbasedir= $FULLBACKUP _dir/$LATEST _full_backup echo-e "Incremental backup will be $incrbasedir as the backup base directory" echo "Else incrbasedir= $INCRBACKUP _dir/${latest_full_backup}/${latest_incr_backup} echo-e "Incremental backup will be $incrbasedir as backup base" Echo
"Fi echo" uses $incrbasedir as the underlying directory for this incremental backup. " $INNOBACKUPEXFULL--defaults-file= $MY _cnf--use-memory=4g $MYSQL _cmd--incremental $NEW _incrdir- Incremental-basedir $INCRBASEDIR > $TMPLOG 2>&1 #保留一份备份的详细日志 cat $TMPLOG >/backup/$logfiledate If [-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 1 fi thisbackup= ' awk- -"/backup created in directory/{split (\\\$0, p, \" "); print p[2]}" $TMPLOG ' rm-f $TMPLOG echo-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_backup latest_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} ' echo echo-e ' \e[31m Note:---------------------------------------------- -----------------------------------. \e[m ' #红色 echo-e ' must be kept $keep_fullbackup, fully prepared ${res_full_backup} and ${res_incre_
All incremental backups in the backup} directory. " Echo-e ' \e[31m Note:---------------------------------------------------------------------------------. \e[m ' #红色 echo Else echo "*********************************" echo-e "Performing a completely new full backup ... Please wait a moment ... "echo" ********************************* "$INNOBACKUPEXFULL--defaults-file= $MY--use-memory=4g $MYSQL _cm _cnf D $FULLBACKUP _dir > $TMPLOG 2>&1 #保留一份备份的详细日志 cat $TMPLOG >/backup/$logfiledate If [-Z ' tail-1 $TMPL OG | grep ' innobackupex:completed ok! ' `" ] ; then echo "$INNOBACKUPEX command execution failed:"; echo echo-e "----------$INNOBACKUPEX _path error----------" Cat $TMPLOG rm-f $TMPLOG exit 1 fi thisbackup= ' AW K--"/backup created in directory/{split (\\\$0, p, \" "); print p[2]}" $TMPLOG ' rm-f $TMPLOG echo-n "database successfully backed up to : $THISBACKUP "echo # hint should keep the backup file start 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} echo echo-e ' \e[31m Note:-------------------------- -------------------------------------------------------. \e[m ' #红色 echo-e ' without incremental backups, you must keep the $keep_fullbackup full standby ${res_full_backup}. "
Echo-e ' \e[31m Note:---------------------------------------------------------------------------------. \e[m ' #红色 echo fi #删除过期的全备 echo-e "find expire backup file...........waiting ..." echo-e "Looking for expired full files and deleting" >>/backu p/$logfiledate for Efile in $ (/usr/bin/find $FULLBACKUP _dir/-mtime +6) do if [-D ${efile}]; Then RM-RF "${efile}" echo-e "Delete expired full file: ${efile}" >>/backup/$logfiledate elif [f ${efile}];
Then RM-RF "${efile}" echo-e "Delete expired full file: ${efile}" >>/backup/$logfiledate fi; Done if [$?-eq "0"];then Echo echo-e "did not find an expired full file to delete" Fi echo Echo Completed: ' Date +%f '%T '%w ' "Exit 0-
------------------------execution results such as: warning:using a password on the command line interface can is insecure. ----------------------------./backup.sh:mysql backup script started in: 2015-04-03 16:35:11 5 ********************************* is executing full New full backup ... Please wait a moment ... **********Database successfully backed up to:/backup/full/2015-04-03_16-35-11 Note:-----------------------------------------
----------------------------------------.
Without an incremental backup, you must reserve 1 copies of the fully-prepared/backup/full/2015-04-03_16-35-11.
Note:---------------------------------------------------------------------------------.
Find expire Backup file...........waiting ..... An expired full file that could be deleted was not found completed in: 2015-04-03 16:35:25 5./backup.sh:mysql backup script started: 2015-04-03 16:35:50 5 full backup 2015-04-03_16-35-11 not expired , the <span style= "White-space:pre" > </span> incremental backup will be based on the 2015-04-03_16-35-11 name as an incremental backup of the underlying directory name/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 basis for this increment
The underlying directory for the backup. Database successfully backed up to:/backup/incre/2015-04-03_16-35-11/2015-04-03_16-35-50 Note:-------------------------------------------
--------------------------------------.
All incremental backups in the/BACKUP/FULL/2015-04-03_16-35-11 and/BACKUP/INCRE/2015-04-03_16-35-11 directories must be retained for 1 copies of the full standby. Note:---------------------------------------------------------------------------------.
Find expire Backup file...........waiting .....
No expired full files were found that can be deleted