MySQL的备份还原(xtrabackup)xtrabackup简介xtrabackup是percona提供的MySQL数据库的备份工具,是唯一开源的能对innodb和xtradb数据库进行热备的工具 xtrabackup的特点
1.备份还原过程快、可靠 xtrabackup备份过程
xtrabackup安装xtrabackup在centos的EPEL源中,可以在配置完EPEL源后使用yum安装 [root@localhost ~]# yum install -y percona-xtrabackup
也可以去官网下载最新版本的xtrabackup xtrabackup使用xtrabackup [option] BACKUP-ROOT-DIR
xtrabackup完全,增量备份及还原
在完全备份之前需要创建一个备份的目录作为存放备份使用 [root@localhost ~]# mkdir /data/backup 数据库内存放的数据 [root@localhost ~]# mysql -e 'SHOW DATABASES;' +--------------------+ | Database | +--------------------+ | hellodb | | information_schema | | mysql | | performance_schema | | test | +--------------------+ 一、完全备份[root@localhost ~]# mariabackup --backup --target-dir=/data/backup --user=root --password='' 二、对数据库破坏[root@localhost ~]# rm -rf /data/mysql/* 三、恢复3.1在恢复数据库前需要先将数据库内数据清理,然后停止mysql服务 [root@localhost ~]# rm -rf /data/mysql/* [root@localhost ~]# service mysqld stop Stopping mysqld (via systemctl): [ OK ] 3.2对备份目录做整理 [root@localhost ~]# mariabackup --prepare --target-dir=/data/backup 3.3将整理好的备份数据还原至数据库 [root@localhost ~]# mariabackup --copy-back --target-dir=/data/backup 3.4此时还原回去的数据属性还有问题,需要将其修改 [root@localhost ~]# ll /data/mysql/ total 12320 -rw-r----- 1 root root 16384 May 6 21:28 aria_log.00000001 -rw-r----- 1 root root 52 May 6 21:28 aria_log_control drwx------ 2 root root 272 May 6 21:28 hellodb -rw-r----- 1 root root 942 May 6 21:28 ib_buffer_pool -rw-r----- 1 root root 12582912 May 6 21:28 ibdata1 drwx------ 2 root root 4096 May 6 21:28 mysql drwx------ 2 root root 20 May 6 21:28 performance_schema drwx------ 2 root root 20 May 6 21:28 test -rw-r----- 1 root root 527 May 6 21:28 xtrabackup_info [root@localhost ~]# chown -R mysql.mysql /data/mysql/ 四、启动服务测试[root@localhost ~]# service mysqld start Starting mysqld (via systemctl): [ OK ] [root@localhost ~]# mysql -e "SHOW DATABASES;SHOW TABLES FROM hellodb;" +--------------------+ | Database | +--------------------+ | hellodb | | information_schema | | mysql | | performance_schema | | test | +--------------------+ +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | +-------------------+
在备份之前先创建出完全备份的目录以及增量备份的目录 [root@localhost ~]# mkdir -pv /dataa/backup/{full,inc1,inc2} mkdir: created directory ‘/data/backup’ mkdir: created directory ‘/data/backup/full’ mkdir: created directory ‘/data/backup/inc1’ mkdir: created directory ‘/data/backup/inc2’ 数据库内所存放的数据 [root@localhost ~]# mysql -e 'SHOW DATABASES;' +--------------------+ | Database | +--------------------+ | hellodb | | information_schema | | mysql | | performance_schema | | test | +--------------------+ 一、对数据进行完全备份[root@localhost ~]# mariabackup --backup --target-dir=/data/backup/full --user=root --password='' 二、此时数据库内数据发生变化[root@localhost ~]# mysql -e "INSERT hellodb.teachers VALUE(5,'Li Xiaolong',30,'M');" [root@localhost ~]# mysql -e "SELECT * FROM hellodb.teachers WHERE tid>4;" +-----+-------------+-----+--------+ | TID | Name | Age | Gender | +-----+-------------+-----+--------+ | 5 | Li Xiaolong | 30 | M | +-----+-------------+-----+--------+ 三、对数据库进行增量备份[root@localhost ~]# mariabackup --user=root --password='' --backup --incremental-basedir=/data/backup/full --target-dir=/data/backup/inc1 四、数据库内数据再次发生变化[root@localhost ~]# mysql -e "INSERT hellodb.students(stuid,age,name) VALUE (26,35,'Sun Wukong');" [root@localhost ~]# mysql -e "SELECT * FROM hellodb.students WHERE stuid>25;" +-------+------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+------------+-----+--------+---------+-----------+ | 26 | Sun Wukong | 35 | F | NULL | NULL | +-------+------------+-----+--------+---------+-----------+ 五、再次对数据库做增量备份[root@localhost ~]# mariabackup --user=root --password='' --backup --incremental-basedir=/data/backup/inc1 --target-dir=/data/backup/inc2 六、破坏数据库[root@localhost ~]# rm -rf /data/mysql/* 七、恢复数据库7.1先对数据库做清理,并停止数据库服务 [root@localhost ~]# rm -rf /data/mysql/* [root@localhost ~]# service mysqld stop Stopping mysqld (via systemctl): [ OK ]
7.2对完全备份做预整理 [root@localhost ~]# mariabackup --prepare --apply-log-only --target-dir=/data/backup/full
7.3合并第一次的增量备份 [root@localhost backup]# mariabackup --prepare --apply-log-only --target-dir=/data/backup/full --incremental-dir=/data/backup/inc1
7.3合并第二次的增量备份 [root@localhost backup]# mariabackup --prepare --target-dir=/data/backup/full --incremental-dir=/data/backup/inc2 7.4整理完毕将备份复制回数据库 [root@localhost backup]# mariabackup --copy-back --target-dir=/data/backup/full 7.5将数据库内的属主和属组进行更改 [root@localhost backup]# chown -R mysql.mysql /data/mysql/ 八、重启服务测试 [root@localhost backup]# service mysqld start Starting mysqld (via systemctl): [ OK ] [root@localhost backup]# msyql MariaDB [hellodb]> select *from students where stuid>25; +-------+------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+------------+-----+--------+---------+-----------+ | 26 | Sun Wukong | 35 | F | NULL | NULL | +-------+------------+-----+--------+---------+-----------+ 1 row in set (0.00 sec) MariaDB [hellodb]> select *from teachers where tid>4; +-----+-------------+-----+--------+ | TID | Name | Age | Gender | +-----+-------------+-----+--------+ | 5 | Li Xiaolong | 30 | M | +-----+-------------+-----+--------+ 1 row in set (0.00 sec)
1.在数据恢复之前必须先要停止MySQL服务 (责任编辑:IT) |