#!/bin/sh
#備份主機
remote_ip=100.0.132.160
master_ip=100.20.132.158
vip=100.20.132.166
#備份使用者
user='root'
#密碼password='00000'
# 返回年月日
backup_date=`date +%f`
# 返回時分秒
backup_time=`date +%h-%m-%s`
# 返回今天是這週的第幾天
backup_week_day=`date +%u`
backup_ok=0
#備份目錄
socket=/data/mysql/mysql.sock
# 備份路徑
backup_dir=/data/xtrabackup
backup_dir_local=/data/xtrabackup_local
# 資料目錄
datadir=/data/mysql
# percona-xtrabackup 備份軟體路徑
xtrabackup_dir=/usr/bin
# 全備是在一周的第幾天
full_backup_week_day=6
#週期性全量增量開始日期
# 全量備資訊名稱字首
full_backup_prefix=full
# 增量備資訊名稱字首
increment_prefix=incr
# mysql配置檔案
mysql_conf_file=/etc/my.cnf
cycle=$backup_date
cycle_record=$backup_dir/cycle_record.txt
if [ ! -f $backup_dir/cycle_record.txt ];then
index=$backup_date
else
if [ "$full_backup_week_day" -eq `date +%u` ]; then
index=$backup_date
else
index=`cat $backup_dir/cycle_record.txt`
fifiindex_file=$backup_dir/backup_$index.index
index_file_local=$backup_dir_local/backup_$index.index
log_dir=$backup_dir/log
if [ ! -d "$backup_dir" ];then
mkdir -p $backup_dir
fiif [ ! -d "$backup_dir_local" ];then
mkdir -p $backup_dir_local
fiif [ ! -d "$log_dir" ];then
mkdir -p $log_dir
fi echo "_$_$_$, \
type:$, \
date:$}" >> $index_file
} echo "_$_$_$, \
type:$, \
date:$}" >> $index_file_local
}# 判斷是應該全備還是增量備份
# 0:full, 1:incr
function get_backup_type()
#推送遠端全量備份
function full_backup ()_$_$_$
if [ ! -d $backup_dir_local/$cycle/ ]; then
mkdir $backup_dir/$cycle/
fiecho $cycle>$backup_dir/cycle_record.txt
ssh $user@$remote_ip "if [ ! -d $backup_dir/$cycle/ ];then mkdir -p $backup_dir/$cycle; fi "
innobackupex --defaults-file=$mysql_conf_file --no-timestamp --user=$user \
--password=$password --host=$ --port=3306 --extra-lsndir=$backup_dir/$cycle/$backup_folder --compress \
--stream=xbstream $backup_dir | ssh $user@$remote_ip "gzip ->$backup_dir/$cycle/$backup_folder.tar.gz"
if [ $? -eq 0 ];then
log_info 0 full
else
log_info 1 full
fi}#推送遠端增量備份
incremental ()_$_$_$
incr_record=`cat $backup_dir/cycle_record.txt`
echo $backup_dir/$incr_record
#cd $backup_dir/$incr_record
incr_base_folder=`sed -n '$p' $index_file | \
awk -f '[, {}]*' '' | \
awk -f ':' ''`
echo $backup_dir/$incr_record/$
innobackupex --defaults-file=$mysql_conf_file \
--no-timestamp --user=$user --password=$password --host=$ --port=3306 \
--stream=xbstream --compress --extra-lsndir=$backup_dir/$incr_record/$backup_folder \
--incremental backup_folder --incremental-basedir=$backup_dir/$incr_record/$ \
|ssh $user@$remote_ip "gzip ->$backup_dir/$incr_record/$backup_folder.tar.gz" \
if [ $? -eq 0 ];then
log_info 0 incr
else
log_info 1 incr
fi}function full_backup_local ()_$_$_$
if [ ! -d $backup_dir_local/$cycle/ ]; then
mkdir $backup_dir_local/$cycle/
fiecho $cycle>$backup_dir_local/cycle_record.txt
innobackupex --defaults-file=$mysql_conf_file --no-timestamp --user=$user \
--password=$password --host=$ --port=3306 $backup_dir_local/$cycle/$backup_folder
if [ $? -eq 0 ];then
echo "全量備份成功"
log_info_local 0 full
else
log_info_local 1 full
fi}#本地增量備份
incremental_local ()_$_$_$
incr_record=`cat $backup_dir_local/cycle_record.txt`
echo $backup_dir_local/$incr_record
#cd $backup_dir/$incr_record
incr_base_folder=`sed -n '$p' $index_file_local | \
awk -f '[, {}]*' '' | \
awk -f ':' ''`
echo $incr_base_folder
echo $backup_dir_local/$incr_record/$
innobackupex --defaults-file=$mysql_conf_file \
--no-timestamp --user=$user --password=$password --host=$ --port=3306 \
--incremental $backup_dir_local/$incr_record/$backup_folder --incremental-basedir=$backup_dir_local/$incr_record/$incr_base_folder
if [ $? -eq 0 ];then
log_info_local 0 incr
else
log_info_local 1 incr
fi}function log_info ()
function log_info_local ()
function run_auto() "
;;esac
}function run_manual() "
;;esac
}function run_auto_local() "
;;esac
}function run_manual_local() "
;;esac
}run_auto
#run_auto_local
#run_manual_local $1
#run_manual $1
Xtrabackup 全量備份指令碼
bin bash 備份檔案的名字為當前主機的ip位址 tar.gz,例如172.16.103.1.tar.gz,且每次備份成功之後都會清空本地的備份目錄。相關目錄 mkdir p xtrabackup mysql back dir xtrabackup mysql backfilename ip a...
Xtrabackup全量備份 增量備份指令碼
一 全量備份指令碼 1.全量備份指令碼 1 2 3 4 5 6 7 8 9 10 11 12 13 bin bash description xtrabackup complete author created by michael 2017 08 07 v0.1 user root passwd ...
運維實戰 Xtrabackup備份指令碼
如果是星期天進行完全備份 如果是周一執行第一次增量,參考 完全 如果是周二到週六執行第1 n次增量,參考上一次增量 備份檔案理應不能與資料檔案放在同一塊磁碟,備份之後可通過rsync至遠端備份伺服器 什麼時候是業務低谷期 保留多長時間 備份失敗記錄日誌 需要實現自動備份 需要有日誌功能 需要能實現主...