#backup. Sh
#!/bin/sh
#
# The first time it is executed it checks for a full backup, otherwise a full-library backup is created first
# When you run it again, it will be based on the settings in the script to make incremental backups from the previous full-library backup
#[email protected]
Innobackupex_path=innobackupex #INNOBACKUPEX的命令
innobackupexfull=/usr/bin/$INNOBACKUPEX _path #INNOBACKUPEX的命令路径
#mysql目标服务器以及用户名和密码
mysql_cmd= "--host=192.168.2.188--user=system--password=password--port=3306"
mysql_up= "--user=system--password=password-ppassword" #mysql的用户名和密码
Tmplog= "/tmp/innobackupex.$$.log"
MY_CNF=/USR/LOCAL/MYSQL/MY.CNF #mysql的配置文件
Mysql=/usr/bin/mysql
Mysql_admin=/usr/bin/mysqladmin
Backup_dir=/backup # home Directory for backup
fullbackup_dir= $BACKUP _dir/full # full-Library backup directory
incrbackup_dir= $BACKUP _dir/incre # Incremental backup directory
FULLBACKUP_INTERVAL=86400 # Full Library backup interval period, time: seconds
Keep_fullbackup=1 # Keep At least a few full-library backups
Logfiledate=backup. ' Date +%y%m%d%h%m '. txt
#开始时间
Started_time= ' Date +%s '
#############################################################################
# Show Errors 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
If [-Z ' $MYSQL _admin $MYSQL _up Status | grep ' Uptime ' "]; 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 "----------------------------"
Echo
echo "$0:mysql backup Script"
echo "started 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 recently modified backup
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
# Create a new directory under the incremental backup directory with the latest full-file name if the latest full-time non-expired
Echo-e "full backup $latest_full_backup not expired, will be named as an incremental backup directory based on $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 the basis for an incremental backup
Latest_incr_backup= ' Find $NEW _incrdir-mindepth 1-maxdepth 1-type D | Sort-nr | Head-1 '
if [! $LATEST _incr_backup]; Then
incrbasedir= $FULLBACKUP _dir/$LATEST _full_backup
ECHO-E "Incremental backup will take $incrbasedir as backup basis"
echo ""
Else
incrbasedir= $LATEST _incr_backup
ECHO-E "Incremental backup will take $incrbasedir as backup basis"
echo ""
Fi
echo "uses $incrbasedir as the basis for a new incremental backup."
$INNOBACKUPEXFULL--defaults-file= $MY _cnf--use-memory=4g $MYSQL _cmd--incremental $NEW _incrdir-- Incremental-basedir $INCRBASEDIR > $TMPLOG 2>&1
Else
echo "*********************************"
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
Fi
#保留一份备份的详细日志
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
# hints should be kept for backup file starting point
Latest_full_backup= ' Find $FULLBACKUP _dir-mindepth 1-maxdepth 1-type d-printf "%p\n" | Sort-nr | Head-1 '
Echo-e "must keep all incremental backups of $keep_fullbackup full and fully prepared $latest_full_backup."
#删除过期的全备
Echo-e "Look for expired full files and delete" >>/backup/$logfiledate
For Efile in $ (/usr/bin/find $FULLBACKUP _dir/-mtime +6)
Do
If [-D $efile]; Then
RM-RF $efile
ECHO-E "Delete expired full files: $efile" >>/backup/$logfiledate
Elif [-F $efile]; Then
RM-RF $file
ECHO-E "Delete expired full files: $efile" >>/backup/$logfiledate
Fi
Done
If [$?-eq "0"];then
Echo
Echo-e "could not find expired full files that can be deleted"
Fi
Echo
echo "Completed: ' Date +%f '%T '%w '"
Exit 0