mysql备份工具之xtrabackup

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

1.备份xtrabackup工具安装(只能备份数据,不能备份表结构)
http://tongcheng.blog.51cto.com/6214144/1562538


2.xtrabackup基本参数

--defaults-file      --数据库的默认配置文件

--target-dir         --备份文件存放的目录

--backup             --备份到--target-dir目录中

--prepare            --备份文件恢复前的准备

--use-memory         --分配多大内存用于恢复

--throttle           --限制使用的IO个数

--databases          --备份指定的数据库

--compress           --压缩备份

--incremental-basedir    --增量备份的目录


3.数据库完全备份

[root@tong1 ~]# mysql -u root -p -e "select * from tong.t1"     --查看数据库中的数据
Enter password: 
+------+
| q    |
+------+
|    9 |
|    8 |
+------+

[root@tong1 ~]# xtrabackup  --default-file=/etc/my.cnf --backup --datadir=/usr/local/mysql-5.6.22/data --target-dir=/opt/all/      --备份所有库
[root@tong1 ~]# ll /opt/all/
total 12300
-rw-r-----. 1 root root 12582912 Apr 24 15:35 ibdata1
drwx------. 2 root root     4096 Apr 24 15:35 tong
-rw-r-----. 1 root root       89 Apr 24 15:35 xtrabackup_checkpoints
-rw-r-----. 1 root root     2560 Apr 24 15:35 xtrabackup_logfile
[root@tong1 ~]#


4.删除数据后恢复
[root@tong1 ~]# mysql -u root -p -e "delete from tong.t1"     --删除数据
Enter password: 
[root@tong1 ~]# mysql -u root -p -e "select * from tong.t1"     --查看数据是否还存在
Enter password: 
[root@tong1 ~]# cd /usr/local/mysql-5.6.22/data/tong
[root@tong1 tong]# cp -a /opt/all/tong/*  .         --将备份的数据覆盖现在的数据文件
cp: overwrite `./t1.ibd'? y
cp: overwrite `./t.ibd'? y
[root@tong1 tong]# chown mysql:mysql *       --修改权限
[root@tong1 tong]# ll
total 220
-rw-r--r--. 1 mysql mysql    61 Apr 24 14:37 db.opt
-rw-r--r--. 1 mysql mysql  8554 Apr 24 14:37 t1.frm
-rw-r-----. 1 mysql mysql 98304 Apr 24 15:35 t1.ibd
-rw-r--r--. 1 mysql mysql  8554 Apr 24 14:37 t.frm
-rw-r-----. 1 mysql mysql 98304 Apr 24 15:35 t.ibd
[root@tong1 tong]# /etc/init.d/mysqld  restart        --重启服务
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@tong1 data]# mysql -u root -p -e "select * from tong.t1" 
Enter password: 
+------+
| q    |
+------+
|    9 |
|    8 |          --数据回来了
+------+
[root@tong1 data]# 


5.增量备份

[root@tong1 data]# mysql -u root -p -e "insert into tong.t1 values(7),(6),(5)" 
Enter password: 
[root@tong1 data]# mysql -u root -p -e "select * from tong.t1" 
Enter password: 
+------+
| q    |
+------+
|    9 |
|    8 |
|    7 |
|    6 |
|    5 |
+------+
[root@tong1 tong]# xtrabackup  --default-file=/etc/my.cnf --backup --datadir=/usr/local/mysql-5.6.22/data --target-dir=/opt/incr/ --incremental-basedir=/opt/all/ 
[root@tong1 tong]# ll /opt/incr/
total 176
-rw-r-----. 1 root root 163840 Apr 24 15:47 ibdata1.delta
-rw-r-----. 1 root root     44 Apr 24 15:47 ibdata1.meta
drwx------. 2 root root   4096 Apr 24 15:47 tong
-rw-r-----. 1 root root     93 Apr 24 15:47 xtrabackup_checkpoints
-rw-r-----. 1 root root   2560 Apr 24 15:47 xtrabackup_logfile
[root@tong1 tong]# 


6.删除数据后恢复数据

[root@tong1 data]# mysql -u root -p -e "delete from tong.t1" 
Enter password: 
[root@tong1 data]# mysql -u root -p -e "select * from tong.t1" 
Enter password: 
[root@tong1 data]# xtrabackup  --default-file=/etc/my.cnf --prepare  --target-dir=/opt/all/ 

[root@tong1 data]# xtrabackup  --default-file=/etc/my.cnf --prepare  --target-dir=/opt/all/ --incremental-dir=/opt/incr/

[root@tong1 data]# cd /opt/all/tong/
[root@tong1 tong]# cp -a * /usr/local/mysql-5.6.22/data/tong
cp: overwrite `/usr/local/mysql-5.6.22/data/tong/t1.ibd'? y
cp: overwrite `/usr/local/mysql-5.6.22/data/tong/t.ibd'? y
[root@tong1 tong]# cd /usr/local/mysql-5.6.22/data/tong
[root@tong1 tong]# chown  mysql:mysql *
[root@tong1 tong]# /etc/init.d/mysqld  restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@tong1 tong]# mysql -u root -p -e "select * from tong.t1"
Enter password: 
+------+
| q    |
+------+
|    9 |
|    8 |
|    7 |
|    6 |
|    5 |
+------+
[root@tong1 tong]# 










本文转自 z597011036 51CTO博客,原文链接:http://blog.51cto.com/tongcheng/1638022,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
30天前
|
关系型数据库 MySQL
elasticsearch对比mysql以及使用工具同步mysql数据全量增量
elasticsearch对比mysql以及使用工具同步mysql数据全量增量
21 0
|
2月前
|
关系型数据库 MySQL 数据库
rds迁移数据迁移工具选择
rds迁移数据迁移工具选择
69 3
|
3月前
|
关系型数据库 MySQL 数据库
rds备份与恢复
rds备份与恢复
57 3
|
3月前
|
关系型数据库 MySQL 数据库
Python tk dos命令备份mysql数据库
Python tk dos命令备份mysql数据库
25 0
|
2月前
|
SQL 关系型数据库 MySQL
mysql怎么备份
mysql怎么备份
197 7
|
1月前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
86 1
|
17天前
|
SQL 存储 关系型数据库
mysql数据库备份与恢复
mysql数据库备份与恢复
|
2月前
|
关系型数据库 MySQL Linux
Linux环境下定时备份mysql数据库
Linux环境下定时备份mysql数据库
|
2月前
|
存储 关系型数据库 MySQL
mysql怎么备份
mysql怎么备份
22 7
|
2月前
|
监控 容灾 安全
规划阿里云RDS跨区迁移并构建容灾与备份策略
规划阿里云RDS(Relational Database Service)跨区迁移并构建容灾与备份策略
114 2