标签:xtrabackup 单库还原
使用 mysqldump 逻辑备份还原单个数据库/表非常方便。但是,当数据量比较大的时候,对逻辑备份进行恢复的时间太长了。这个时候选择物理备份的方式能大大缩短其中时间。使用 xtrabackup 进行全库物理备份还原非常简单,无须对其表空间(tablespace)进行重新导入。
xtrabackup 全库备份还原步骤:
创建备份
$ xtrabackup --backup --target-dir=/data/backups/
准备备份
$ xtrabackup --prepare --target-dir=/data/backups/
还原备份
$ xtrabackup --copy-back --target-dir=/data/backups/
$ chown -R mysql:mysql /var/lib/mysql
单库/表备份还原步骤:
创建备份
xtrabackup --backup --datadir=/var/lib/mysql --target-dir=/data/backups/ --tables="^test[.]. * "
准备备份
$ xtrabackup --prepare --target-dir=/data/backups/
导出表结构
$ mysqldump -d ^test > test.sql
导入表结构
$ mysql test < test.sql
释放表空间
# mysql -e "set foreign_key_checks=0;alter table test.* discard tablespace;"
复制文件到数据目录
# cp -f /data/backups/*.cfg /data/backups/*.ibd /data/backups/*.frm /var/lib/mysql/test
修改属主
# chown -R mysql:mysql /var/lib/mysql/test
导入表空间
# mysql -e "set foreign_key_checks=0;alter table test.* import tablespace;analyze table test.*;"
如果两个实例都具有 GA(通用可用性)状态并且它们的版本在同一个系列中,则从另一个 MySQL 服务器实例导入表空间文件将起作用。 否则,该文件必须已在与其导入的服务器实例相同的服务器实例上创建。
以下是一个通过 xtrabackup 对单库进行备份还原的脚本
#!/bin/bash # by geamover on 2017-12-26 # last updated on 2018-05-08 # This is for backup one DB to the orther DB on the same server by xtrabackup. BK_DIR=/data/backup/`date +%F`/$1 # 备份路径 MT=$BK_DIR/$1.sql # 元数据 DT=/data/mysql/data/ # 数据目录 SK=/tmp/mysql.sock # mysql.sock PW=123456 # mysql 密码 if [ $# -ne 2 ]; then echo -e "\e[1;31mUsage:\e[0m $0 source_DB destination_DB" exit 1 fi mysql -u root -p"$PW" -e "use $1;" &> /dev/null if [ $? != 0 ]; then echo "There is no database $1, please check!" exit 1 fi echo "It will drop databae $2, are you sure continue?" echo -e "\e[1;31my\e[0m for continue, any orther for exit: " read input if [ $input == "y" ]; then echo "Go on!" else echo "Cancled!" exit fi if [ -d $BK_DIR ];then rm -rf $BK_DIR/* else mkdir -p $BK_DIR fi echo "From $1 to $2" | tee -a xtra.log echo `date "+%F %T"`" Backing up databae $1 to $BK_DIR" | tee -a xtra.log xtrabackup --backup -u root -p"$PW" --socket=$SK --databases="$1" --parallel=8 --target-dir=$BK_DIR &>> xtra.log JD=`tail -n 1 xtra.log | awk '{print $3$4}'` if [ $JD == "completedOK!" ]; then echo `date "+%F %T"`" Back up databae $1 to $BK_DIR completed OK!" else echo `date "+%F %T"`" Back up databae $1 to $BK_DIR failed!" exit 1 fi echo `date "+%F %T"`" Prepare the backup."| tee -a xtra.log xtrabackup --prepare --export --parallel=8 --target-dir=$BK_DIR &>> xtra.log JD=`tail -n 1 xtra.log | awk '{print $3$4}'` if [ $JD == "completedOK!" ]; then echo `date "+%F %T"`" Prepare up databae $1 to $BK_DIR completed OK!" else echo `date "+%F %T"`" Prepare up databae $1 to $BK_DIR failed!" exit 1 fi echo `date "+%F %T"`" Dump the metadata of $1 to $MT." # --set-gtid-purged=OFF 如果开启 GTID 请指定该选项导出元数据 mysqldump -u root -p"$PW" -d $1 > $MT echo "Drop database $2 if exists.Then create it and import metadata." mysql -u root -p"$PW" -e "Drop database if exists $2;create database $2;use $2;source $MT;" &> /dev/null for i in $(grep "CREATE TABLE" $MT | awk '{print $3}' | sed "s#\`##g") do mysql -u root -p"$PW" -e "set foreign_key_checks=0;alter table $2.$i discard tablespace;" &> /dev/null cp -f $BK_DIR/$1/$i.cfg $BK_DIR/$1/$i.ibd $BK_DIR/$1/$i.frm $DT$2/ chown -R mysql:mysql $DT$2/ mysql -u root -p"$PW" -e "set foreign_key_checks=0;alter table $2.$i import tablespace;analyze table $2.$i;" &> /dev/null done echo `date "+%F %T"`" From $1 to $2 done." | tee -a xtra.log # delete the backup rm -rf $BK_DIR
标签:xtrabackup 单库还原
原文地址:http://blog.51cto.com/geamover/2113966