說(shuō)明
· MySQL v5.7.24
· OS Ubuntu 16.04
· Tools Xtrabackup
配置文件
# mysql 用戶名
user=op
# mysql 密碼
password=PASSWD
# 備份路勁
full_backup_dir=/home/op/db_bak/full
incr_backup_dir=/home/op/db_bak/incr
# percona-xtrabackup 備份軟件路徑
xtrabackup_dir=/usr/bin/innobackupex
# mysql配置文件
mysql_conf_file=/etc/my.cnf
# 錯(cuò)誤日志文件(根據(jù)此文件知道備份是否成功)
# format:
# {dir:full/2019-02-14_00-00-00,date:2019-02-14}
error_log=/var/db_script/log/mysql_increment_hot_backup.err
##log文件
log_dir=/var/db_script/log
全量備份
#!/usr/bin/env bash
# 讀取配置文件中的所有變量值, 設(shè)置為全局變量
# 配置文件
conf_file="/var/db_script/conf/mysql_increment_hot_backup.conf"
# mysql 用戶
user=`sed '/^user=/!d;s/.*=//' $conf_file`
# mysql 密碼
password=`sed '/^password=/!d;s/.*=//' $conf_file`
# mysql 備份目錄
full_backup_dir=`sed '/^full_backup_dir=/!d;s/.*=//' $conf_file`
incr_backup_dir=`sed '/^incr_backup_dir=/!d;s/.*=//' $conf_file`
# percona-xtrabackup 備份軟件路徑
xtrabackup_dir=`sed '/^xtrabackup_dir=/!d;s/.*=//' $conf_file`
#文件目錄下創(chuàng)建的文件
deleted_full_dir=`find $full_backup_dir -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -n | head -1`
deleted_incr_dir=`find $incr_backup_dir -mindepth 1 -maxdepth 1 -type d -mtime +6`
# 創(chuàng)建相關(guān)目錄
log_dir=`sed '/^log_dir=/!d;s/.*=//' $conf_file`
# mysql 配置文件
mysql_conf_file=`sed '/^mysql_conf_file=/!d;s/.*=//' $conf_file`
# 錯(cuò)誤日志文件
error_log=`sed '/^error_log=/!d;s/.*=//' $conf_file`
# 備份日期
backup_date=`date +%F_%H-%M-%S`
if [ ! -d $full_backup_dir ] > $log_dir/${backup_date}.log 2>&1;
then
mkdir -p $full_backup_dir
fi
if [ ! -d $incr_backup_dir ] > $log_dir/${backup_date}.log 2>&1;
then
mkdir -p $incr_backup_dir
fi
if [ ! -d $log_dir ] > $log_dir/${backup_date}.log 2>&1;
then
mkdir -p $log_dir
fi
# 全量備份
function full_backup() {
mkdir -p $full_backup_dir/$backup_date
sudo $xtrabackup_dir \
--defaults-file=$mysql_conf_file \
--user=$user \
--password=$password \
--no-timestamp \
$full_backup_dir/$backup_date >> $log_dir/${backup_date}.log 2>&1
return $?
}
# 生成可用備份
function completed_backup () {
sudo chown -R op:op $full_backup_dir
latest_dir=`find $full_backup_dir -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`
$xtrabackup_dir --apply-log --redo-only $full_backup_dir/$latest_dir >> $log_dir/${backup_date}.log 2>&1
return $?
if [ $? != 0 ];
then
echo "$?"
fi
}
# 刪除過(guò)期的備份(一般在全備完成后使用)
function delete_before_backup() {
cd $full_backup_dir
sudo rm -rf $deleted_full_dir
incr_count=`ls $incr_backup_dir | wc -w`
if [ "$incr_count" != "0" ];
then
sudo rm -r $incr_backup_dir/$deleted_incr_dir
fi
}
# 記錄 錯(cuò)誤消息到文件
function logging_backup_err() {
echo " dir:${1}_${backup_date}, \
date:${backup_date}}" >> $error_log
}
# 測(cè)試配置文件正確性
function test_conf_file() {
# 判斷每個(gè)變量是否在配置文件中有配置躬翁,沒(méi)有則退出程序
if [ ! -n "$user" ]; then echo 'fail: configure file user not set'; exit 2; fi
if [ ! -n "$password" ]; then echo 'fail: configure file password not set'; exit 2; fi
if [ ! -n "$full_backup_dir" ]; then echo 'fail: configure file full_backup_dir not set'; exit 2; fi
if [ ! -n "$mysql_conf_file" ]; then echo 'fail: configure file mysql_conf_file not set'; exit 2; fi
if [ ! -n "$error_log" ]; then echo 'fail: configure file error_log not set'; exit 2; fi
}
# 執(zhí)行
function run() {
# 檢測(cè)配置文件值
test_conf_file
#全量備份
full_backup
#生成可用備份
completed_backup
# 清除過(guò)期的備份
delete_before_backup
}
run
增量備份
#!/usr/bin/env bash
# 讀取配置文件中的所有變量值, 設(shè)置為全局變量
# 配置文件
conf_file="/var/db_script/conf/mysql_increment_hot_backup.conf"
# mysql 用戶
user=`sed '/^user=/!d;s/.*=//' $conf_file`
# mysql 密碼
password=`sed '/^password=/!d;s/.*=//' $conf_file`
# mysql 備份目錄
full_backup_dir=`sed '/^full_backup_dir=/!d;s/.*=//' $conf_file`
incr_backup_dir=`sed '/^incr_backup_dir=/!d;s/.*=//' $conf_file`
# percona-xtrabackup 備份軟件路徑
xtrabackup_dir=`sed '/^xtrabackup_dir=/!d;s/.*=//' $conf_file`
#文件目錄下最新創(chuàng)建的文件
latest_full_dir=`sudo find $full_backup_dir -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`
# 創(chuàng)建相關(guān)目錄
log_dir=`sed '/^log_dir=/!d;s/.*=//' $conf_file`
# mysql 配置文件
mysql_conf_file=`sed '/^mysql_conf_file=/!d;s/.*=//' $conf_file`
# 備份錯(cuò)誤日志文件
error_log=`sed '/^error_log=/!d;s/.*=//' $conf_file`
# 備份日期
backup_date=`date +%F_%H-%M-%S`
if [ ! -d $incr_backup_dir ] ;
then
mkdir -p $incr_backup_dir
fi
# 增量備份
function increment_backup() {
count=`ls $incr_backup_dir | wc -w`
if [ "$count" == "0" ];
then
sudo $xtrabackup_dir \
--defaults-file=$mysql_conf_file \
--user=$user \
--password=$password \
--no-timestamp \
--incremental \
$incr_backup_dir/$backup_date \
--incremental-basedir=$full_backup_dir/$latest_full_dir > $log_dir/${backup_date}.log 2>&1
return $?
else
latest_incr_dir=`sudo find $incr_backup_dir -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`
sudo $xtrabackup_dir \
--defaults-file=$mysql_conf_file \
--user=$user \
--password=$password \
--no-timestamp \
--incremental \
$incr_backup_dir/$backup_date \
--incremental-basedir=$incr_backup_dir/$latest_incr_dir > $log_dir/${backup_date}.log 2>&1
return $?
fi
}
#生成可用備份
function completed_backup () {
sudo chown -R op:op $incr_backup_dir
latest_incr_dir=`sudo find $incr_backup_dir -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`
sudo $xtrabackup_dir --apply-log --redo-only $full_backup_dir/$latest_full_dir --incremental-dir=$incr_backup_di
r/$latest_incr_dir >> $log_dir/${backup_date}.log 2>&1
return $?
}
# 記錄 錯(cuò)誤消息到文件
function logging_backup_err() {
echo "{dir:${1}_${backup_date} \
date:${backup_date}}" >> $error_log
}
# 測(cè)試配置文件正確性
function test_conf_file() {
# 判斷每個(gè)變量是否在配置文件中有配置妆棒,沒(méi)有則退出程序
if [ ! -n "$user" ]; then echo 'fail: configure file user not set'; exit 2; fi
if [ ! -n "$password" ]; then echo 'fail: configure file password not set'; exit 2; fi
if [ ! -n "$mysql_conf_file" ]; then echo 'fail: configure file mysql_conf_file not set'; exit 2; fi
if [ ! -n "$incr_backup_dir" ]; then echo 'fail: configure file incr_backup_dir not set'; exit 2; fi
if [ ! -n "$error_log" ]; then echo 'fail: configure file error_log not set'; exit 2; fi
}
# 執(zhí)行
function run() {
# 檢測(cè)配置文件值
test_conf_file
increment_backup
completed_backup
logging_backup_err
}
run