mysqlbackup 恢复特定表

mysqlbackup使用TTS恢复指定表.

*************************************************************
4.恢复特定表
*************************************************************

--4.1新建测试环境

CREATE DATABASE `wind`  DEFAULT CHARACTER SET gbk ;

use wind;

create table t1
(
sid int not null ,
sname varchar(100)  not null
)engine=innodb charset=gbk  ; 

DELIMITER //
create PROCEDURE proc1()
BEGIN
    DECLARE i int DEFAULT 0;
    set i=1 ;
set autocommit=0;
WHILE i<=1000000 DO
INSERT INTO t1 values(i,'mysql测试');
set i=i+1;
END WHILE;
commit;
set autocommit=1;
END
//
DELIMITER ;

call proc1;

create table t2
as
select * from t1;

--4.2.全备

# rm -rf /backup && mkdir /backup

#mysqlbackup --defaults-file=/usr/local/mysql/my.cnf  --host=127.0.0.1   --port=3306  --protocol=tcp  --user=root  --password=123  --use-tts --include-tables='wind.t2' --with-timestamp   --backup-dir=/backup   --log-bin-index=/usr/local/mysql/mysql_logs/binary_log/mysql-bin.index backup-and-apply-log 

# cat /backup/2015-04-02_12-41-45/meta/backup_variables.txt  | grep 'end'
end_lsn=138866623

--4.3 增量备份

mysql> select count(*) from t1;
+----------+
| count(*) |
+----------+
|  1000000 |
+----------+
1 row in set (0.75 sec)

mysql> select count(*) from t2;
+----------+
| count(*) |
+----------+
|  1000000 |
+----------+
1 row in set (0.68 sec)

mysql> delete from t2  limit 10;
Query OK, 10 rows affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select count(*) from t2;
+----------+
| count(*) |
+----------+
|   999990 |
+----------+
1 row in set (0.80 sec)

# rm -rf /backupinc && mkdir /backupinc

--第一次增量备份

#mysqlbackup --defaults-file=/usr/local/mysql/my.cnf  --host=127.0.0.1   --port=3306  --protocol=tcp  --user=root  --password=123  --use-tts --include-tables='wind.t2' --with-timestamp  --log-bin-index=/usr/local/mysql/mysql_logs/binary_log/mysql-bin.index --incremental --start-lsn=138866623  --incremental-backup-dir=/backupinc backup   

# cat /backupinc/2015-04-02_12-44-02/meta/backup_variables.txt  | grep 'end'
end_lsn=138868639

--第二次增量备份

mysql> select count(*) from wind.t2;
+----------+
| count(*) |
+----------+
|   999990 |
+----------+
1 row in set (0.83 sec)

mysql> desc t2
    -> ;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| sid   | int(11)      | NO   |     | NULL    |       |
| sname | varchar(100) | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> update t2 set sname='ocpyang mysql test!' limit 5000;
Query OK, 5000 rows affected (0.24 sec)
Rows matched: 5000  Changed: 5000  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select count(*) from wind.t2 where sname='ocpyang mysql test!';
+----------+
| count(*) |
+----------+
|     5000 |
+----------+
1 row in set (0.86 sec)

#mysqlbackup --defaults-file=/usr/local/mysql/my.cnf  --host=127.0.0.1   --port=3306  --protocol=tcp  --user=root  --password=123  --use-tts --include-tables="wind.t2" --with-timestamp  --log-bin-index=/usr/local/mysql/mysql_logs/binary_log/mysql-bin.index --incremental --start-lsn=138868639  --incremental-backup-dir=/backupinc  backup 

# cat /backupinc/2015-04-02_12-46-48/meta/backup_variables.txt  | grep end
end_lsn=139571560

--4.4 合并增量备份到全备

ls /backupinc/
2015-04-02_12-44-02  2015-04-02_12-46-48

ls /backup

2015-04-02_12-41-45

mysqlbackup --backup-dir=/backup/2015-04-02_12-41-45/ --incremental-backup-dir=/backupinc/2015-04-02_12-44-02 --log-bin-index=/usr/local/mysql/mysql_logs/binary_log/mysql-bin.index apply-incremental-backup 

mysqlbackup --backup-dir=/backup/2015-04-02_12-41-45 --incremental-backup-dir=/backupinc/2015-04-02_12-46-48 --log-bin-index=/usr/local/mysql/mysql_logs/binary_log/mysql-bin.index apply-incremental-backup 

--4.5  模拟删除指定表

#cat /usr/local/mysql/my.cnf |grep datadir
datadir=/usr/local/mysql/data

mysql> select count(*) from t2;
+----------+
| count(*) |
+----------+
|   999990 |
+----------+
1 row in set (0.80 sec)

mysql> select count(*) from wind.t2 where sname='ocpyang mysql test!';
+----------+
| count(*) |
+----------+
|     5000 |
+----------+
1 row in set (0.86 sec)

mysql> drop table t2;

--4.6 还原指定表(使用mysql用户)

chown -R mysql /backup
chgrp -R mysql /backup

chown -R mysql /usr/local/mysql
chgrp -R mysql /usr/local/mysql

[[email protected] ~]$ whoami   #避免权限问题
mysql

/***********************NOTE:权限问题出现的错误 

150402 13:29:26 mysqlbackup: INFO: Importing table: wind.t2.
 mysqlbackup: ERROR: mysql query: 'ALTER TABLE wind.t2 IMPORT TABLESPACE':
 Internal error: Cannot reset LSNs in table '"wind"."t2"' : Tablespace not found
 mysqlbackup: ERROR: Failed to import tablespace wind.t2.

mysqlbackup failed with errors!

*************************************************/

mysqlbackup --host=127.0.0.1   --port=3306  --protocol=tcp  --user=root  --password=123  --datadir=/usr/local/mysql/data/   --log-bin-index=/usr/local/mysql/mysql_logs/binary_log/mysql-bin.index --backup-dir=/backup/2015-04-02_12-41-45/ --include-tables='wind\.t2$' copy-back

[[email protected] ~]$ mysqlbackup --host=127.0.0.1   --port=3306  --protocol=tcp  > --user=root  --password=123  > --datadir=/usr/local/mysql/data/   > --log-bin-index=/usr/local/mysql/mysql_logs/binary_log/mysql-bin.index > --backup-dir=/backup/2015-04-02_12-41-45/ > --include-tables='wind\.t2$' > copy-back
MySQL Enterprise Backup version 3.12.0 Linux-2.6.18-194.el5-x86_64 [2015/03/10]
Copyright (c) 2003, 2015, Oracle and/or its affiliates. All Rights Reserved.

 mysqlbackup: INFO: Starting with following command line ...
 mysqlbackup --host=127.0.0.1 --port=3306 --protocol=tcp --user=root
        --password=xxx --datadir=/usr/local/mysql/data/
        --log-bin-index=/usr/local/mysql/mysql_logs/binary_log/mysql-bin.index
        --backup-dir=/backup/2015-04-02_12-41-45/ --include-tables=wind\.t2$
        copy-back 

 mysqlbackup: INFO:
IMPORTANT: Please check that mysqlbackup run completes successfully.
           At the end of a successful 'copy-back' run mysqlbackup
           prints "mysqlbackup completed OK!".

150402 13:38:25 mysqlbackup: INFO: MEB logfile created at /backup/2015-04-02_12-41-45/meta/MEB_2015-04-02.13-38-25_copy_back.log

 mysqlbackup: INFO: MySQL server version is '5.6.23-enterprise-commercial-advanced-log'.
 mysqlbackup: INFO: Got some server configuration information from running server.

--------------------------------------------------------------------
                       Server Repository Options:
--------------------------------------------------------------------
  datadir = /usr/local/mysql/data/
  innodb_data_home_dir = /usr/local/mysql/innodb_data
  innodb_data_file_path = ibdata1:800M;ibdata2:800M:autoextend
  innodb_log_group_home_dir = /usr/local/mysql/mysql_logs/innodb_log/
  innodb_log_files_in_group = 3
  innodb_log_file_size = 2147483648
  innodb_page_size = 16384
  innodb_checksum_algorithm = innodb
  innodb_undo_directory = .
  innodb_undo_tablespaces = 0
  innodb_undo_logs = 128

--------------------------------------------------------------------
                       Backup Config Options:
--------------------------------------------------------------------
  datadir = /backup/2015-04-02_12-41-45/datadir
  innodb_data_home_dir = /backup/2015-04-02_12-41-45/datadir
  innodb_data_file_path = ibdata1:800M;ibdata2:800M:autoextend
  innodb_log_group_home_dir = /backup/2015-04-02_12-41-45/datadir
  innodb_log_files_in_group = 3
  innodb_log_file_size = 2147483648
  innodb_page_size = 16384
  innodb_checksum_algorithm = innodb

 mysqlbackup: INFO: Creating 14 buffers each of size 16777216.
150402 13:38:25 mysqlbackup: INFO: Copy-back operation starts with following threads
		1 read-threads    1 write-threads
 mysqlbackup: INFO: Could not find binlog index file. binlogs will not be copied for this backup.
 Point-In-Time-Recovery will not be possible.
 If this is online backup then server may not have started with --log-bin.
 You may specify its location with --log-bin-index option.
150402 13:38:25 mysqlbackup: INFO: Creating table: wind.t2.
150402 13:38:25 mysqlbackup: INFO: Copying /backup/2015-04-02_12-41-45/datadir/wind/t2.ibd.
150402 13:38:26 mysqlbackup: INFO: Completing the copy of all non-innodb files.
150402 13:38:27 mysqlbackup: INFO: Importing table: wind.t2.
150402 13:38:28 mysqlbackup: INFO: Analyzing table: wind.t2.
150402 13:38:29 mysqlbackup: INFO: Copy-back operation completed successfully.
150402 13:38:29 mysqlbackup: INFO: Finished copying backup files to '/usr/local/mysql/data'

mysqlbackup completed OK!

# ls -ll /usr/local/mysql/data/wind/
total 57360
-rw-rw----. 1 mysql mysql       61 Apr  2 13:26 db.opt
-rw-rw----. 1 mysql mysql     8590 Apr  2 13:38 t2.frm
-rw-rw-r--. 1 mysql mysql 58720256 Apr  2 13:38 t2.ibd

/*******

ALTER TABLE t2 discard TABLESPACE;

ALTER TABLE t2 IMPORT TABLESPACE;

*********/

--4.7验证表恢复情况

mysql> use wind;
Database changed
mysql> show tables;
+----------------+
| Tables_in_wind |
+----------------+
| t2             |
+----------------+
1 row in set (0.00 sec)

mysql> select count(*) from t2;
+----------+
| count(*) |
+----------+
|   999990 |
+----------+
1 row in set (1.34 sec)

mysql> select count(*) from wind.t2 where sname='ocpyang mysql test!';
+----------+
| count(*) |
+----------+
|     5000 |
+----------+
1 row in set (0.78 sec)
时间: 2024-10-11 07:11:37

mysqlbackup 恢复特定表的相关文章

PLSQL使用SCN码恢复误删表数据

#查询数据库当前的SCN码select current_scn from v$database 1250494 #将当前的SCN码减小后,根据SCN码查询误删数据表的数据情况#直至找到被删的数据为止select * from gg_user as of scn 1210000; #使用flashback 语句根据SCN码恢复数据表的数据.flashback table gg_user to scn 1210000; #再次查询数据表的数据就是被删之前的数据select * from gg_use

oracle查找特定表的引用

select * from user_source t where upper(t.TEXT) like upper('%table1%') 第二种方法类似于eclipse中的file search select * from user_dependencies g where g.referenced_name like upper('%table1%'); 第一种方法类似于eclipse中的reference oracle查找特定表的引用

如何恢复SQLServer表级数据详解

 最近几天,公司的技术维护人员频繁让我恢复数据库,因为他们总是少了where条件,导致update.delete出现了无法恢复的后果,加上那些库都是几十G.恢复起来少说也要十几分钟.为此,找了一些资料和工作总结,给出一下几个方法,用于快速恢复表,而不是库,但是切记,防范总比亡羊补牢好.上章分享了如何使用SQLCMD在SQLServer执行多个脚本详解需要的朋友可以看下. 在生产环境或者开发环境,往往都有某些非常重要的表.这些表存放了核心数据.当这些表出现数据损坏时,需要尽快还原.但是,正式环

SQLServer 2012 新建对特定表的只读账户

建立只读账户,且只能读取某些特定表.现在就把操作的步骤给大家分享一下.这个操作的前提是数据库比较大,有众多的数据表,需要选出自己只读的数据表.新建账户的操作应该都不陌生了,就给大家截图不在赘言. 在左侧User Mapping选项卡中,设置你要指定的数据库,并选择新建的账户以及架构.这样就新建账户成功,找到你需要只读的数据库的数据表,右键--属性. 在左侧Permission选项卡中设置你要添加的账户,并赋予Select权限. 这样操作是为了在众多数据表中选择你要只读的数据表,否则你需要一个个删

RMAN 备份恢复 删除表空间后控制文件丢失

先备份一个控制文件 RMAN> backup current controlfile tag='bak_ctlfile' format='/home/oracle/backup/bak_ctl_%U_%T'; drop一个表空间 SQL> drop tablespace xds including contents and datafiles; Tablespace dropped. 删除控制文件 rm /u01/app/oracle/oradata/orcl/control01.ctl rm

如何使用 RMAN 异机恢复部分表空间

在oracle 数据库的日常维护和使用期间难免会遇到误删数据(drop,delete, truncate)当我们使用常规手段(flashback query ,flashback drop)也无法恢复数据时,我们可以使用最近的逻辑备份,在异机使用dmp 来恢复相应的表,但是如果没有这些逻辑备份,但是有一个最近的rman 全备,那么我们就可以利用这个备份来恢复被误删的表空间,从而实现数据的恢复,这里我以NBU 的备份环境为例简单描述下如何来回复部分 表空间: -------在nomount 状态

备份恢复-----system表空间损坏

无法进行关库,报错如下 SQL> shutdown immediate ORA-01122: database file 1 failed verification checkORA-01110: data file 1: '/u01/app/oracle/oradata/cuug/system01.dbf'ORA-01210: data file header is media corruptSQL> 只能启动到mount状态 SQL> startup ORACLE instance

通过binlog日志文件恢复单表【小技巧】

场景:某天执行了delete from t1操作忘加where条件,我们需要通过昨天的全量备份+误操作之前的binlog增量备份,加以恢复. 在通过mysqlbinlog解析时,需要用sed命令去过滤出t1表的insert.delete.update操作,如果binlog文件很多,并且默认是1G的大小,用这种方法费时费力,很麻烦. 这里,介绍一个小技巧,通过授权账户去恢复. 我这里创建一个用户hcy,并只授予t1表的select.insert.delete.update权限,这样该用户针对其他表

mysqldump备份中恢复单张表

⒈mysqldump备份中导出单张表很多时候我们需要从mysqldump备份文件中恢复出一张表,通常的做法可能是先把sql 文件恢复到一个测试数据库,然后再使用mysqldump导出一张表,再恢复到生产环境,这样,如果数据量不大这方法是可行的,但是你依然需要有一个测试机器或者临时创建一个库,不是很方便,可以利用awk/sed解决相关问题:①在原数据库中使用show tables;②利用sed或者awk,将数据导入文件中(注意表之间的排序)# awk '/^-- Table structure f