#backup .sh#!/bin/sh#on xtrabackup 2.2.8# It checks for a full backup the first time it executes, otherwise it creates a full-library backup # when you run it again, It is based on the settings in the script to make incremental backups on the basis of the previous full or incremental backup #[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 user name and password tmplog= "/tmp/innobackupex.$$.log" my_cnf=/usr/local/mysql/my.cnf #mysql的配置文件MYSQL =/usr/ bin/mysqlmysql_admin=/usr/bin/mysqladminbackup_dir=/backup # Backup's home directory fullbackup_dir= $BACKUP _dir/full # Catalog incrbackup_dir= $BACKUP _dir/incre # Incremental backup fullbackup_interval=86400 # Full-Library backup interval, 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 execution Environment if [ ! -x $INNOBACKUPEXFULL ]; then error "$INNOBACKUPEXFULL not installed or 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&NBSP; " started 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 ' #如果全备有效进行增量备份否则执行完全备份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 not expired, will be based on the Latest_full_backup name as incremental backup base directory name "echo " "new_incrdir= $INCRBACKUP _dir/$LATEST _full_ backupmkdir -p $NEW _incrdir# to find out whether the latest incremental backup exists. Specifies the path of a backup 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 be Incrbasedir as the backup base directory "echo " "fiecho " uses $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 created 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 be kept $keep_ Fullbackup all the incremental backups in the directory of ${res_full_backup} and ${res_incre_backup} are fully prepared. echo -e ' \e[31m note:-------------------------------------------------------------------------- -------. \e[m ' #红色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&nBsp thisbackup= ' awk -- '/backup created 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 ' res_full_backup=${fullbackup_dir}/${latest_full_backup}echoecho -e ' \e[31m note:---------------------------------------------------------------------------------. \e[m ' # Red echo -e "No incremental backup, you must keep $keep_fullbackup full standby ${res_full_backup}." echo -e ' \e[31m note:-------------------------------------------------------------------------- -------. \e[m ' #红色echofi # Delete expired full echo -e "Find expire backup file ..... Waiting ... "echo -e " Look for expired full files 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 "could not find expired full files that 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 be 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 were not found complete in: 2015-04-03 16:35:25 5./backup.sh: mysql backup script started on: 2015-04-03 16:35:50 5 full backup 2015-04-03_16-35-11 is not expired and will be based on 2015-04-03_16-35-11 name as the incremental backup base directory name <span style= "White-space: Pre ></span> incremental backup will use/BACKUP/FULL/2015-04-03_16-35-11 as the backup base directory <span style= White-space:pre "></span> uses/backup/full/2015-04-03_16-35-11 as the base directory for this incremental backup. The database was 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/backup/incre/2015-04-03_ All incremental backups in the 16-35-11 directory. note:---------------------------------------------------------------------------------. find expire backup file...........waiting ..... Expired full files that can be deleted are not found
This article is from "Record bit!" "Blog, be sure to keep this provenance http://ocpyang.blog.51cto.com/3401739/1630406
Xtrabackup Automatic Backup scripts