<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
進行備份前需要先建立備份使用者,直接使用 root 使用者進行備份也行,但是這樣不太規範。
create user backup@'localhost' identified by '123456'; grant reload,process,lock tables,replication client on *.* to backup@localhost;
備份整個庫,使用的是備份使用者,備份檔案存放地址為 /backup/
innobackupex --defaults-file=/etc/my.cnf --user=backup --password=123456 /backup/
指定為增量備分,使用的是備份使用者,增量的基礎為上一次的全備,已經使用 --incremental-basedir 進行指定了,備份後存放的檔案為 /backup/
innobackupex --defaults-file=/etc/my.cnf --user=backup --password=123456 --incremental --incremental-basedir=/backup/2021-06-01_14-44-54 /backup/
回滾未提交的事務及同步已經提交的事務至資料檔案使資料檔案處於一致性狀態
innobackupex --apply-log --redo-only /backup/2021-06-01_14-44-54/
在恢復前,需要確保 MySQL 的資料目錄為已經刪除了。
innobackupex --copy-back --datadir=/usr/local/mysql/data /backup/2021-06-01_14-44-54/
恢復後,需要對 MySQL 的data 目錄進行重新賦權:
chown -R mysql:mysql data/
到這恢復就完成了。
backup.sh
#!/bin/bash # 獲得程式路徑名 program_dir=`dirname $0`/.. # 讀取組態檔中的所有變數值, 設定為全域性變數 # 組態檔 conf_file="$program_dir/conf/backup.conf" # mysql 使用者 user=`sed '/^user=/!d;s/.*=//' $conf_file` # mysql 密碼 password=`sed '/^password=/!d;s/.*=//' $conf_file` # mysql 備份目錄 backup_dir=`sed '/^backup_dir=/!d;s/.*=//' $conf_file` # mysql 備份壓縮打包目錄 gzip_dir=`sed '/^gzip_dir=/!d;s/.*=//' $conf_file` # percona-xtrabackup命令xtrabackup路徑 xtrabackup_bin=`sed '/^xtrabackup_bin=/!d;s/.*=//' $conf_file` # mysql 全備字首標識 full_backup_prefix=`sed '/^full_backup_prefix=/!d;s/.*=//' $conf_file` # mysql 增量備字首標識 increment_prefix=`sed '/^increment_prefix=/!d;s/.*=//' $conf_file` # 備份錯誤紀錄檔檔案 error_log=$program_dir/var/`sed '/^error_log=/!d;s/.*=//' $conf_file` # 備份索引檔案 index_file=$program_dir/var/`sed '/^index_file=/!d;s/.*=//' $conf_file` # 備份日期 backup_date=`date +%F` # 備份時間 backup_time=`date +%H-%M-%S` # 備份時的周幾 backup_week_day=`date +%u` # 建立相關目錄 log_dir=$program_dir/log/backup var_dir=$program_dir/var mkdir -p $backup_dir mkdir -p $log_dir mkdir -p $var_dir mkdir -p $gzip_dir # 全量備份 function full_backup() { backup_folder=${full_backup_prefix}_${backup_date}_${backup_time}_${backup_week_day} mkdir -p $backup_dir/$backup_folder $xtrabackup_bin --user=$user --password=$password --backup --target-dir=$backup_dir/$backup_folder > $log_dir/${backup_folder}.log 2>&1 return $? } # 增量備份 function increment_backup() { backup_folder=${increment_prefix}_${backup_date}_${backup_time}_${backup_week_day} incr_base_folder=`sed -n '$p' $index_file | awk -F '[, {}]*' '{print $3}' | awk -F ':' '{print $2}'` mkdir -p $backup_dir/$backup_folder $xtrabackup_bin --user=$user --password=$password --backup --target-dir=$backup_dir/$backup_folder --incremental-basedir=$backup_dir/$incr_base_folder > $log_dir/${backup_folder}.log 2>&1 return $? } # 刪除之前的備份(一般在全備完成後使用) function delete_before_backup() { cat $index_file | awk -F '[, {}]*' '{print $3}' | awk -v backup_dir=$backup_dir -F ':' '{if($2!=""){printf("rm -rf %s/%sn", backup_dir, $2)}}' | /bin/bash cat $index_file | awk -F '[, {}]*' '{print $3}' | awk -v gzip_dir=$gzip_dir -F ':' '{if($2!=""){printf("rm -rf %s/%sn", gzip_dir, $2)}}' | /bin/bash cat $index_file | awk -F '[, {}]*' '{print $3}' | awk -v log_dir=$log_dir -F ':' '{if($2!=""){printf("rm -rf %s/%s.logn", log_dir, $2)}}' | /bin/bash } # 備份索引檔案 function backup_index_file() { cp $index_file ${index_file}_$(date -d "1 day ago" +%F) } # 備份索引檔案 function send_index_file_to_remote() { # ./expect_scp ip地址 賬號 密碼 ${index_file} 目標伺服器存放的資料夾 埠號 echo 'send index file ok' } # 新增索引, 索引記錄了當前最新的備份 function append_index_to_file() { echo "{week_day:$backup_week_day, dir:${1}_${backup_date}_${backup_time}_${backup_week_day}, type:${1}, date:${backup_date}}" >> $index_file } # 記錄錯誤訊息到檔案 function logging_backup_err() { echo "{week_day:$backup_week_day, dir:${1}_${backup_date}_${backup_time}_${backup_week_day}, type:${1}, date:${backup_date}}" >> $error_log } # 清空索引 function purge_index_from_file() { > $index_file } # 清空錯誤紀錄檔資訊 function purge_err_log() { > $error_log } # 打包備份 function tar_backup_file() { cd $backup_dir tar -jcf ${gzip_dir}/${1}_${backup_date}_${backup_time}_${backup_week_day}.tar.bz2 ${1}_${backup_date}_${backup_time}_${backup_week_day} cd - > /dev/null rm -rf ${backup_dir}/${1}_${backup_date}_${backup_time}_${backup_week_day} } # 傳送備份到遠端 function send_backup_to_remote() { # ./expect_scp ip地址 賬號 密碼 ${gzip_dir}/${1}_${backup_date}_${backup_time}_${backup_week_day}.tar.bz2 目標伺服器存放的資料夾 埠號 echo "send $1 remote ok" } # 判斷是應該全備還是增量備份 # 0:full, 1:incr function get_backup_type() { backup_type=0 if [ 1 -eq `date +%H` ]; then backup_type=0 else backup_type=1 fi touch $index_file if [ ! -n "`cat $index_file`" ]; then backup_type=0 fi return $backup_type } # 測試組態檔正確性 function test_conf_file() { # 判斷每個變數是否在組態檔中有設定,沒有則退出程式 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 "$backup_dir" ]; then echo 'fail: configure file backup_dir not set'; exit 2; fi if [ ! -n "$gzip_dir" ]; then echo 'fail: configure file backup_dir not set'; exit 2; fi if [ ! -n "$full_backup_prefix" ]; then echo 'fail: configure file full_backup_prefix not set'; exit 2; fi if [ ! -n "$increment_prefix" ]; then echo 'fail: configure file increment_prefix not set'; exit 2; fi if [ ! -n "$error_log" ]; then echo 'fail: configure file error_log not set'; exit 2; fi if [ ! -n "$index_file" ]; then echo 'fail: configure file index_file not set'; exit 2; fi } # 執行 function main() { # 檢測組態檔值 test_conf_file # 判斷是執行全備還是增量備份 get_backup_type backup_type=$? case $backup_type in 0 ) # 全量備份 full_backup backup_ok=$? if [ 0 -eq "$backup_ok" ]; then # 全備成功 # 打包最新備份 tar_backup_file $full_backup_prefix # # 將tar備份傳送到遠端 send_backup_to_remote $full_backup_prefix # 備份索引檔案 backup_index_file # 清除之前的備份 delete_before_backup # 清除索引檔案 purge_index_from_file # 新增索引, 索引記錄了當前最新的備份 append_index_to_file $full_backup_prefix # 傳送索引檔案到遠端 send_index_file_to_remote else # 全備失敗 # 刪除備份目錄 rm -rf ${backup_dir}/${full_backup_prefix}_${backup_date}_${backup_time}_${backup_week_day} # 記錄錯誤紀錄檔 logging_backup_err $full_backup_prefix fi ;; 1 ) # 增量備份 increment_backup backup_ok=$? if [ "$backup_ok" -eq 0 ]; then # 增量備份成功 # 打包最新備份 tar_backup_file $increment_prefix # # 將tar備份傳送到遠端 send_backup_to_remote $increment_prefix # 新增索引, 索引記錄了當前最新的備份 append_index_to_file $increment_prefix # # 傳送索引檔案到遠端 send_index_file_to_remote else # 增量備份失敗 # 刪除備份目錄 rm -rf ${backup_dir}/${increment_prefix}_${backup_date}_${backup_time}_${backup_week_day} # 記錄錯誤紀錄檔 logging_backup_err $increment_prefix fi ;; esac } main
backup.conf
# mysql 使用者名稱 user=backup # mysql 密碼 password=123456 # 備份路徑 backup_dir=/data/backup # 備份壓縮打包目錄 gzip_dir=/data/backups/backups_zip # innobackupex 命令路徑 xtrabackup_bin=/opt/xtrabackup/bin/xtrabackup # 全量備資訊名稱 字首 full_backup_prefix=full # 增量備資訊名稱 字首 increment_prefix=incr # 錯誤紀錄檔檔案(根據此檔案知道備份是否成功) # format: # {week_day:1,dir:full/incr_2015-12-29_00-00-00_7,type:full/incr,date:2015-12-30} error_log=mysql_increment_hot_backup.err # 索引檔案 # format: # {week_day:1,dir:full/incr_2015-12-29_00-00-00_7,type:full/incr,date:2015-12-30} index_file=mysql_increment_hot_backup.index
restore.sh
#!/bin/bash # 獲得程式路徑名 program_dir=`dirname $0`/.. # 讀取組態檔中的所有變數值, 設定為全域性變數 # 組態檔 conf_file="$program_dir/conf/restore.conf" # MySQL 資料資料夾 data_dir=`sed '/^data_dir=/!d;s/.*=//' $conf_file` # 備份索引檔案路徑 backup_index_file=`sed '/^backup_index_file=/!d;s/.*=//' $conf_file` # percona-xtrabackup命令xtrabackup路徑 xtrabackup_bin=`sed '/^xtrabackup_bin=/!d;s/.*=//' $conf_file` # 備份檔案目錄 backup_restore_dir=`sed '/^backup_restore_dir=/!d;s/.*=//' $conf_file` # 檢查組態檔正確性 function exam_conf_file() { # 判斷每個變數是否在組態檔中有設定,沒有則退出程式 if [ ! -n "$data_dir" ]; then echo 'fail: configure file data_dir not set'; exit 2; fi if [ ! -n "$backup_index_file" ]; then echo 'fail: configure file backup_index_file not set'; exit 2; fi if [ ! -n "$xtrabackup_bin" ]; then echo 'fail: configure file xtrabackup_bin not set'; exit 2; fi if [ ! -n "$backup_restore_dir" ]; then echo 'fail: configure file backup_restore_dir not set'; exit 2; fi } # 檢查備份檔案是否是壓縮格式 function exam_backup_restore_file(){ file_backup_restore_name_arr=`ls $backup_restore_dir` for file_name in $file_backup_restore_name_arr;do if [ "${file_name##*.}"x = "bz2"x ];then tar -jxf $backup_restore_dir/$file_name -C $backup_restore_dir rm -rf $backup_restore_dir/$file_name fi done } # 檢查 MySQL 是否停止 function exam_mysql_is_stop(){ if [ 0 -eq `ps -ef | grep mysql | grep -v grep | wc -l` ]; then echo "MySQL 服務已停止" else /etc/init.d/mysqld stop echo "正在停止 MySQL 服務" sleep 3 echo "已停止 MySQL 服務" fi } # 檢查 MySQL data 檔案是否刪除 function exam_data_is_del(){ if [ -d $data_dir ];then echo "正在刪除 MySQL 的data檔案" rm -rf $data_dir else echo "MySQL 的資料檔案已刪除 " fi } # 讀取備份索引檔案 function read_backup_index() { cat $backup_index_file | awk '{print $2}' | awk -F: '{print $2}' | awk '{sub(/.$/,"")}1' } # 準備全備檔案 function ready_full(){ full_file_name=`echo ${1} | awk '{print $1}'` $xtrabackup_bin/innobackupex --apply-log --redo-only $backup_restore_dir/$full_file_name echo "全備檔案已準備好" } # 準備增備檔案 function ready_incr(){ backup_index=$(read_backup_index) full_file_name=`echo $backup_index | awk '{print $1}'` for file_name in $backup_index;do if [ 1 -eq `echo "$file_name" | grep incr | wc -l` ]; then $xtrabackup_bin/innobackupex --apply-log --redo-only $backup_restore_dir/$full_file_name --incremental-dir=$backup_restore_dir/$file_name fi done echo "增備檔案已準備好" } # 執行備份恢復 function exec_backup_restore(){ echo "開始進行備份恢復" full_file_name=`echo ${1} | awk '{print $1}' ` $xtrabackup_bin/innobackupex --copy-back --datadir=$data_dir $backup_restore_dir/$full_file_name } # 執行 function main() { # 檢查組態檔正確性 exam_conf_file # 檢查備份檔案是否是壓縮格式 exam_backup_restore_file # 檢查 MySQL 是否停止 exam_mysql_is_stop # 檢查 MySQL data 檔案是否刪除 exam_data_is_del # 讀取索引檔案 backup_index=$(read_backup_index) # 準備全備檔案 ready_full $backup_index # 準備增備檔案 ready_incr # 執行備份恢復 exec_backup_restore $backup_index # 對資料檔案進行賦權 echo "重新對資料目錄賦權" chown -R mysql:mysql $data_dir echo "正在啟動MySQL" /etc/init.d/mysqld start echo "備份恢復成功" } main
restore.conf
# MySQL 資料資料夾 data_dir=/opt/mysql/data #備份索引檔案路徑 backup_index_file=/opt/xtrabackup/backup/var/mysql_increment_hot_backup.index #xtrabackup bin 的目錄 xtrabackup_bin=/opt/xtrabackup/bin # 備份檔案目錄 backup_restore_dir=/data/backups/backups_zip
以上就是MySQL如何使用使用Xtrabackup進行備份和恢復的詳細內容,更多關於MySQL 用Xtrabackup備份和恢復的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45