12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
mysql数据库 时间:2024-12-25 09:55:56
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
使用 mysqldump 逻辑备份还原单个数据库/表非常方便。但是,当数据量比较大的时候,对逻辑备份进行恢复的时间太长了。这个时候选择物理备份的方式能大大缩短其中时间。使用 xtr
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
使用 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#bygeamoveron2017-12-26#lastupdatedon2018-05-08#ThisisforbackuponeDBtotheortherDBonthesameserverbyxtrabackup.BK_DIR=/data/backup/`date+%F`/$1#备份路径MT=$BK_DIR/$1.sql#元数据DT=/data/mysql/data/#数据目录SK=/tmp/mysql.sock#mysql.sockPW=123456#mysql密码if[$#-ne2];thenecho-e"\e[1;31mUsage:\e[0m$0source_DBdestination_DB"exit1fimysql-uroot-p"$PW"-e"use$1;"&>/dev/nullif[$?!=0];thenecho"Thereisnodatabase$1,pleasecheck!"exit1fiecho"Itwilldropdatabae$2,areyousurecontinue?"echo-e"\e[1;31my\e[0mforcontinue,anyortherforexit:"readinputif[$input=="y"];thenecho"Goon!"elseecho"Cancled!"exitfiif[-d$BK_DIR];thenrm-rf$BK_DIR/*elsemkdir-p$BK_DIRfiecho"From$1to$2"|tee-axtra.logecho`date"+%F%T"`"Backingupdatabae$1to$BK_DIR"|tee-axtra.logxtrabackup--backup-uroot-p"$PW"--socket=$SK--databases="$1"--parallel=8--target-dir=$BK_DIR&>>xtra.logJD=`tail-n1xtra.log|awk'{print$3$4}'`if[$JD=="completedOK!"];thenecho`date"+%F%T"`"Backupdatabae$1to$BK_DIRcompletedOK!"elseecho`date"+%F%T"`"Backupdatabae$1to$BK_DIRfailed!"exit1fiecho`date"+%F%T"`"Preparethebackup."|tee-axtra.logxtrabackup--prepare--export--parallel=8--target-dir=$BK_DIR&>>xtra.logJD=`tail-n1xtra.log|awk'{print$3$4}'`if[$JD=="completedOK!"];thenecho`date"+%F%T"`"Prepareupdatabae$1to$BK_DIRcompletedOK!"elseecho`date"+%F%T"`"Prepareupdatabae$1to$BK_DIRfailed!"exit1fiecho`date"+%F%T"`"Dumpthemetadataof$1to$MT."#--set-gtid-purged=OFF如果开启GTID请指定该选项导出元数据mysqldump-uroot-p"$PW"-d$1>$MTecho"Dropdatabase$2ifexists.Thencreateitandimportmetadata."mysql-uroot-p"$PW"-e"Dropdatabaseifexists$2;createdatabase$2;use$2;source$MT;"&>/dev/nullforiin$(grep"CREATETABLE"$MT|awk'{print$3}'|sed"s#\`##g")domysql-uroot-p"$PW"-e"setforeign_key_checks=0;altertable$2.$idiscardtablespace;"&>/dev/nullcp-f$BK_DIR/$1/$i.cfg$BK_DIR/$1/$i.ibd$BK_DIR/$1/$i.frm$DT$2/chown-Rmysql:mysql$DT$2/mysql-uroot-p"$PW"-e"setforeign_key_checks=0;altertable$2.$iimporttablespace;analyzetable$2.$i;"&>/dev/nulldoneecho`date"+%F%T"`"From$1to$2done."|tee-axtra.log#deletethebackuprm-rf$BK_DIR
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19