首页 > 数据库 > > GreatDB备份与恢复(4)


[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 -- general_log 默认是关闭的 GreatDB Cluster[(none)]> show variables like 'general%'; +------------------+-------------------------------+ | Variable_name | Value | +------------------+-------------------------------+ | general_log | OFF | | general_log_file | /greatdb/logs/general3306.log | +------------------+-------------------------------+ 2 rows in set (0.01 sec) -- 清空log日志文件 GreatDB Cluster[(none)]> system > /greatdb/logs/general3306.log -- 开启 general_log GreatDB Cluster[(none)]> set global general_log=ON; Query OK, 0 rows affected (0.00 sec) -- 执行备份任务,后面有介绍各种备份场景 -- 关闭 general_log GreatDB Cluster[(none)]> set global general_log=OFF; Query OK, 0 rows affected (0.00 sec)












[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> use test GreatDB Cluster[test]> show tables; +----------------+ | Tables_in_test | +----------------+ | t_global | | t_hash | | t_list | | t_normal | | t_range | +----------------+ 5 rows in set (0.00 sec) GreatDB Cluster[test]> select * from t_hash; +----+------+ | c1 | c2 | +----+------+ | 8 | 8 | | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | | 6 | 6 | | 7 | 7 | +----+------+ 8 rows in set (0.05 sec)
[root@greatdb1 ~]# greatsqldump -h192.168.0.81 -ugreatdb -pgreatdb \ --single-transaction \ --sql-log-bin \ --set-gtid-purged=OFF \ --flush-logs \ --master-data=2 \ --databases test > /tmp/db_test_bak.sql
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[test]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec) GreatDB Cluster[test]> drop database test; Query OK, 5 rows affected (0.42 sec) GreatDB Cluster[(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> source /tmp/db_test_bak.sql Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) ... ... Query OK, 0 rows affected (0.00 sec) GreatDB Cluster[test]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec) GreatDB Cluster[test]> use test Database changed GreatDB Cluster[test]> show tables; +----------------+ | Tables_in_test | +----------------+ | t_global | | t_hash | | t_list | | t_normal | | t_range | +----------------+ 5 rows in set (0.01 sec) GreatDB Cluster[test]> select * from t_hash; +----+------+ | c1 | c2 | +----+------+ | 8 | 8 | | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | | 6 | 6 | | 7 | 7 | +----+------+ 8 rows in set (0.01 sec)
[root@greatdb1 ~]# greatsqldump -h192.168.0.81 -ugreatdb -pgreatdb \ --single-transaction \ --sql-log-bin \ --set-gtid-purged=OFF \ --flush-logs \ --master-data=2 \ --databases test \ --tables t_hash > /tmp/table_t_hash_bak.sql
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> use test GreatDB Cluster[test]> show tables; +----------------+ | Tables_in_test | +----------------+ | t_global | | t_hash | | t_list | | t_normal | | t_range | +----------------+ 5 rows in set (0.00 sec) GreatDB Cluster[test]> drop table t_hash; Query OK, 0 rows affected (0.09 sec) GreatDB Cluster[test]> show tables; +----------------+ | Tables_in_test | +----------------+ | t_global | | t_list | | t_normal | | t_range | +----------------+ 4 rows in set (0.01 sec)
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> use test; GreatDB Cluster[(none)]> source /tmp/table_t_hash_bak.sql Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) ... ... Query OK, 0 rows affected (0.00 sec) GreatDB Cluster[test]> show tables; +----------------+ | Tables_in_test | +----------------+ | t_global | | t_hash | | t_list | | t_normal | | t_range | +----------------+ 5 rows in set (0.00 sec) GreatDB Cluster[test]> select * from t_hash; +----+------+ | c1 | c2 | +----+------+ | 8 | 8 | | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | | 6 | 6 | | 7 | 7 | +----+------+ 8 rows in set (0.00 sec)
[root@greatdb1 ~]# greatsqldump -h192.168.0.81 -ugreatdb -pgreatdb \ --single-transaction \ --sql-log-bin \ --set-gtid-purged=OFF \ --flush-logs \ --master-data=2 \ --no-create-info \ --databases test \ --tables t_hash \ --where='c1<4' > /tmp/row_t_hash_id4_bak.sql
注意:
GreatDB Cluster[(none)]> select * from test.t_hash; +----+------+ | c1 | c2 | +----+------+ | 8 | 8 | | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | | 6 | 6 | | 7 | 7 | +----+------+ 8 rows in set (0.01 sec) GreatDB Cluster[(none)]> delete from test.t_hash where c1<4; Query OK, 3 rows affected (0.01 sec) GreatDB Cluster[(none)]> select * from test.t_hash; +----+------+ | c1 | c2 | +----+------+ | 8 | 8 | | 4 | 4 | | 5 | 5 | | 6 | 6 | | 7 | 7 | +----+------+ 5 rows in set (0.00 sec)
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> use test; GreatDB Cluster[(none)]> source /tmp/row_t_hash_id4_bak.sql Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) ... ... Query OK, 0 rows affected (0.00 sec) GreatDB Cluster[test]> select * from test.t_hash; +----+------+ | c1 | c2 | +----+------+ | 8 | 8 | | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | | 6 | 6 | | 7 | 7 | +----+------+ 8 rows in set (0.03 sec)
[root@greatdb1 ~]# greatsqldump -h192.168.0.81 -ugreatdb -pgreatdb \ --single-transaction \ --compact \ --set-gtid-purged=OFF \ --flush-logs \ --master-data=2 \ --all-databases > /tmp/greatdb_full_bak.sql
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> use test GreatDB Cluster[test]> show tables; +----------------+ | Tables_in_test | +----------------+ | t_global | | t_hash | | t_list | | t_normal | | t_range | +----------------+ 5 rows in set (0.00 sec) GreatDB Cluster[test]> select * from t_hash; +----+------+ | c1 | c2 | +----+------+ | 8 | 8 | | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 5 | 5 | | 6 | 6 | | 7 | 7 | +----+------+ 8 rows in set (0.05 sec)
[root@greatdb1 ~]# greatsqlpump -h192.168.0.81 -ugreatdb -pgreatdb \ --default-parallelism=8 \ --parallel-schemas=4:test \ --default-character-set=utf8mb4 \ --set-gtid-purged=OFF \ --single-transaction \ --databases test > /tmp/db_test_pump.sql greatsqlpump: [Warning] Using a password on the command line interface can be insecure. Dump progress: 1/3 tables, 0/2005 rows Dump completed in 151
GreatDB Cluster[test]> drop database test;
GreatDB Cluster[(none)]> source /tmp/db_test_pump.sql
[root@greatdb1 ~]# greatsqlpump -h192.168.0.81 -ugreatdb -pgreatdb \ --default-parallelism=8 \ --parallel-schemas=4:test \ --parallel-schemas=2:mysql \ --default-character-set=utf8 \ --set-gtid-purged=OFF \ --single-transaction \ --compress-output=LZ4 \ --databases test mysql > /tmp/db_test_mysql_lz4_pump.sql greatsqlpump: [Warning] Using a password on the command line interface can be insecure. Dump progress: 1/1 tables, 0/0 rows Dump completed in 356
GreatDB Cluster[(none)]> show variables like 'secure_file_priv'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | secure_file_priv | | +------------------+-------+ 1 row in set (0.01 sec)
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> select * from test.t_hash into outfile '/tmp/t_hash_outfile.sql'; Query OK, 8 rows affected (0.03 sec) [root@greatdb1 ~]# cat /tmp/t_hash_outfile.sql 8 8 1 1 2 2 3 3 4 4 5 5 6 6 7 7
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> select * from test.t_hash where c1<4 into outfile '/tmp/t_hash_c14_outfile.sql'; Query OK, 8 rows affected (0.03 sec) [root@greatdb1 ~]# cat /tmp/t_hash_c14_outfile.sql 1 1 2 2 3 3
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> select * from test.t_hash into outfile '/tmp/t_hash_outfile_2.sql' fields terminated by ',' enclosed by '"' lines terminated by '\n'; Query OK, 8 rows affected (0.01 sec) [root@greatdb1 ~]# cat /tmp/t_hash_outfile_2.sql "8","8" "1","1" "2","2" "3","3" "4","4" "5","5" "6","6" "7","7"
GreatDB Cluster[test]> load data infile '/tmp/t_hash_outfile.sql' into table t_hash;
GreatDB Cluster[test]> load data infile '/tmp/t_hash_outfile_2.sql' into table t_hash fields terminated by ',' enclosed by '"' lines terminated by '\n' ;
LOAD DATA INFILE能被用来读取从外部来源获得的文件。例如,以sql或txt格式的文件将有由逗号分隔并用双引号包围的字段,需要设置选项fields terminated ,enclosed ,line terminated等选项来设置加载方式。
GreatDB 的物理备份需要提前初始化一个备份节点,备份节点是除 SqlNode 节点、DataNode 节点之外的第三个角色节点。其作用是进行集群全量和增量备份的数据物理文件的复制克隆。通过调用 SqlNode 节点程序,执行远程 SqlNode,DataNode 节点物理文件备份到backup_node 节点。备份节点可以同时添加多个,支持并发多节点备份。backup_node 支持全量物理备份和增量备份2种方式。

在 greatdb 集群中,采用延迟提交的技术获取一致性点。其基本操作流程为:
本次测试使用 192.168.0.82 作为备份一个节点。
在新的主机部署备份节点,使用集群安装包中的 greatdb_init 执行初始化,参数文件中必须指定 gdb_backup_dir 备份目录路径。
# 创建目录 [root@greatdb2 ~]# mkdir -p /greatdb/dbdata/backnode5501/{data,log,tmp} [root@greatdb2 ~]# chown -R greatdb:greatdb /greatdb/dbdata/backnode5501 # 创建配置文件 [root@greatdb2 ~]# cd /greatdb/conf [root@greatdb2 conf]# cp sqlnode3306.cnf backnode5501.cnf [root@greatdb2 conf]# sed -i "s/3306/5501/g" backnode5501.cnf [root@greatdb2 conf]# sed -i "s/sqlnode/backnode/g" backnode5501.cnf [root@greatdb2 conf]# sed -i "14i"gdb_backup_dir=/greatdb/dbbackup"" backnode5501.cnf # 初始化 backup_node 节点 [root@greatdb2 ~]# greatdb_init --defaults-file=/greatdb/conf/backnode5501.cnf \ --cluster-user=greatdb \ --cluster-host=% \ --cluster-password='greatdb' \ --node-type=backup_node

[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> call mysql.greatdb_add_backup_node('bk_node1','192.168.0.82',5501); Query OK, 1 row affected (4.37 sec) GreatDB Cluster[(none)]> select * from information_schema.GREATDB_BACKUP_NODES; +---------+-----------+--------------------------------------+--------------+------+--------+ | NODE_ID | NODE_NAME | UUID | HOST | PORT | STATUS | +---------+-----------+--------------------------------------+--------------+------+--------+ | 169 | bk_node1 | 72667064-987f-11ec-9256-000c290f05e6 | 192.168.0.82 | 5501 | ONLINE | +---------+-----------+--------------------------------------+--------------+------+--------+ 1 row in set (0.00 sec)
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> call mysql.greatdb_start_full_backup; +---------------------------------+ | current_backup_task_name | +---------------------------------+ | full_backup_20220301-032132_176 | +---------------------------------+ 1 row in set (39.90 sec) Query OK, 0 rows affected (39.90 sec)
登录到备份节点可以看到备份出来的文件
[root@greatdb2 ~]# cd /greatdb/dbbackup/ [root@greatdb2 dbbackup]# ll total 0 drwxr-x--- 7 greatdb greatdb 99 Feb 28 19:22 full_backup_20220301-032132_176 [root@greatdb2 dbbackup]# cd full_backup_20220301-032132_176/ [root@greatdb2 full_backup_20220301-032132_176]# ll total 0 drwxr-x--- 6 greatdb greatdb 81 Feb 28 19:22 inc_binlog drwxr-x--- 6 greatdb greatdb 180 Feb 28 19:21 shard-shard1 drwxr-x--- 6 greatdb greatdb 180 Feb 28 19:21 shard-shard2 drwxr-x--- 6 greatdb greatdb 180 Feb 28 19:21 shard-shard3 drwxr-x--- 5 greatdb greatdb 168 Feb 28 19:21 sqlnode
GreatDB 的增量备份是基于上次全量备份的基础之上,针对各个节点进行的 binlog 的拷贝备份。调用 SqlNode 过程 greatdb_start_inc_backup 启动增量备份任务。
执行步骤如下:
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> use test GreatDB Cluster[test]> show tables; +----------------+ | Tables_in_test | +----------------+ | t_global | | t_hash | | t_list | | t_normal | | t_range | +----------------+ 5 rows in set (0.00 sec) GreatDB Cluster[test]> select * from t_normal; Empty set (0.00 sec) GreatDB Cluster[test]> insert into t_normal values (1,1),(2,2),(3,3); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 GreatDB Cluster[test]> select * from t_normal; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +----+------+ 3 rows in set (0.00 sec)
-- 注意需要登录到备份节点的实例上查看 [root@greatdb2 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.82 -P5501 GreatDB Cluster[(none)]> select BACKUP_NAME,BACKUP_NODE_HOST,BACKUP_NODE_PORT,BACKUP_LEVEL,DATA_TYPE,SHARD_NAME,BACKUP_PATH,BACKUP_STAGE from information_schema.GREATDB_BACKUP_TASKS; +---------------------------------+------------------+------------------+--------------+-----------+------------+-------------------+--------------+ | BACKUP_NAME | BACKUP_NODE_HOST | BACKUP_NODE_PORT | BACKUP_LEVEL | DATA_TYPE | SHARD_NAME | BACKUP_PATH | BACKUP_STAGE | +---------------------------------+------------------+------------------+--------------+-----------+------------+-------------------+--------------+ | full_backup_20220301-032132_176 | 192.168.0.82 | 5501 | 0 | DATANODE | shard1 | /greatdb/dbbackup | COMPLETE | | full_backup_20220301-032132_176 | 192.168.0.82 | 5501 | 0 | DATANODE | shard2 | /greatdb/dbbackup | COMPLETE | | full_backup_20220301-032132_176 | 192.168.0.82 | 5501 | 0 | DATANODE | shard3 | /greatdb/dbbackup | COMPLETE | | full_backup_20220301-032132_176 | 192.168.0.82 | 5501 | 0 | SQLNODE | | /greatdb/dbbackup | COMPLETE | +---------------------------------+------------------+------------------+--------------+-----------+------------+-------------------+--------------+ 4 rows in set (0.00 sec)
GreatDB Cluster[test]> call mysql.greatdb_start_inc_backup('full_backup_20220301-032132_176'); +--------------------------------+ | current_backup_task_name | +--------------------------------+ | inc_backup_20220301-050855_183 | +--------------------------------+ 1 row in set (0.43 sec) Query OK, rows affected (0.44 sec)
[root@greatdb2 ~]# cd /greatdb/dbbackup/ [root@greatdb2 dbbackup]# ll total 0 drwxr-x--- 7 greatdb greatdb 99 Feb 28 19:22 full_backup_20220301-032132_176 drwxr-x--- 3 greatdb greatdb 24 Feb 28 21:08 inc_backup_20220301-050855_183 [root@greatdb2 dbbackup]# [root@greatdb2 dbbackup]# [root@greatdb2 dbbackup]# cd inc_backup_20220301-050855_183/ [root@greatdb2 inc_backup_20220301-050855_183]# ll total 0 drwxr-x--- 6 greatdb greatdb 81 Feb 28 21:08 inc_binlog [root@greatdb2 inc_backup_20220301-050855_183]# cd inc_binlog/ [root@greatdb2 inc_binlog]# ll total 0 drwxr-x--- 2 greatdb greatdb 37 Feb 28 21:08 shard-shard1 drwxr-x--- 2 greatdb greatdb 37 Feb 28 21:08 shard-shard2 drwxr-x--- 2 greatdb greatdb 37 Feb 28 21:08 shard-shard3 drwxr-x--- 2 greatdb greatdb 58 Feb 28 21:08 sqlnode
注意:需要特别注意的是,上述示例中,集群中只有一个备份节点,因此,所有的备份数据均在同一台机器同一个基础备份路径之下。如果存在多个备份节点,彼此在不同的机器中,则所有SqlNode, Shard的全量clone和增量binlog可能分布在不同的主机上,此时,需要在不同的备份节点机器上,查找对应的备份数据。
-- 在 sqlNode 实例上查询 [root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[test]> select * from mysql.greatdb_sqlnode_tasks; +---------+---------------+-------------+--------------------------------------+---------------------------------+-------------------------------------+---------------------+---------+ | task_id | task_type | task_status | task_owner | object_name | info | create_time | version | +---------+---------------+-------------+--------------------------------------+---------------------------------+-------------------------------------+---------------------+---------+ | 1 | SHARD_RECOVER | RUNNING | 58d09346-77d0-11eb-815f-000c293f8a6a | N/A | shard recover daemon task | 2021-02-26 09:17:50 | 8 | | 43 | MIGRATE_TABLE | COMPLETE | 5aa2cd7d-77d0-11eb-a8ff-000c29d936e4 | test.t_normal | test.t_normal from shard2 to shard1 | 2022-02-27 20:39:26 | 0 | | 148 | BACKUP | COMPLETE | 5aa2cd7d-77d0-11eb-a8ff-000c29d936e4 | full_backup_20220301-032132_176 | full_backup | 2022-03-01 03:21:32 | | | 155 | BACKUP | COMPLETE | 5aa2cd7d-77d0-11eb-a8ff-000c29d936e4 | inc_backup_20220301-050855_183 | inc_backup | 2022-03-01 05:08:55 | | +---------+---------------+-------------+--------------------------------------+---------------------------------+-------------------------------------+---------------------+---------+ 4 rows in set (0.00 sec)
-- 在备份节点的实例上查看 [root@greatdb2 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.82 -P5501 GreatDB Cluster[(none)]> select BACKUP_NAME,BACKUP_NODE_HOST,BACKUP_NODE_PORT,BACKUP_LEVEL,DATA_TYPE,SHARD_NAME,BACKUP_PATH,BACKUP_STAGE from information_schema.GREATDB_BACKUP_TASKS; +---------------------------------+------------------+------------------+--------------+-----------+------------+-------------------+--------------+ | BACKUP_NAME | BACKUP_NODE_HOST | BACKUP_NODE_PORT | BACKUP_LEVEL | DATA_TYPE | SHARD_NAME | BACKUP_PATH | BACKUP_STAGE | +---------------------------------+------------------+------------------+--------------+-----------+------------+-------------------+--------------+ | full_backup_20220301-032132_176 | 192.168.0.82 | 5501 | 0 | DATANODE | shard1 | /greatdb/dbbackup | COMPLETE | | full_backup_20220301-032132_176 | 192.168.0.82 | 5501 | 0 | DATANODE | shard2 | /greatdb/dbbackup | COMPLETE | | full_backup_20220301-032132_176 | 192.168.0.82 | 5501 | 0 | DATANODE | shard3 | /greatdb/dbbackup | COMPLETE | | full_backup_20220301-032132_176 | 192.168.0.82 | 5501 | 0 | SQLNODE | | /greatdb/dbbackup | COMPLETE | | inc_backup_20220301-050855_183 | 192.168.0.82 | 5501 | 1 | DATANODE | shard1 | /greatdb/dbbackup | COMPLETE | | inc_backup_20220301-050855_183 | 192.168.0.82 | 5501 | 1 | DATANODE | shard2 | /greatdb/dbbackup | COMPLETE | | inc_backup_20220301-050855_183 | 192.168.0.82 | 5501 | 1 | DATANODE | shard3 | /greatdb/dbbackup | COMPLETE | | inc_backup_20220301-050855_183 | 192.168.0.82 | 5501 | 1 | SQLNODE | | /greatdb/dbbackup | COMPLETE | +---------------------------------+------------------+------------------+--------------+-----------+------------+-------------------+--------------+ 8 rows in set (0.00 sec)
即使用备份数据进行集群回放重建的过程,恢复出完整的 GreatDB 集群,当然也可以按需缩减集群规模,如 SqlNode 原始3个,每个 Shard 中3个 DataNode,可在恢复时恢复1个 SqlNode,每个 Shard 中恢复一个 DataNode 即可。
分为 SqlNode 恢复和 Shard 中 DataNode 的恢复
SqlNode 和 DataNode 恢复过程相似,过程如下:

由于资源有限,本次恢复测试使用源环境。
# 所有 SqlNode 和 DataNode kill 掉所有进程 ps -ef |grep greatdb |grep -v grep |awk '{print $2}' |xargs kill -9 # 所有 SqlNode 和 DataNode 清空数据目录 rm -rf /greatdb/dbdata/*/*/*
# 备份节点复制 SqlNode 数据 scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/sqlnode/* 192.168.0.81:/greatdb/dbdata/sqlnode3306/data/ scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/sqlnode/* 192.168.0.82:/greatdb/dbdata/sqlnode3306/data/ scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/sqlnode/* 192.168.0.83:/greatdb/dbdata/sqlnode3306/data/ # 备份节点复制 DataNode 数据 scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/shard-shard1/* 192.168.0.81:/greatdb/dbdata/datanode4406/data/ scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/shard-shard1/* 192.168.0.82:/greatdb/dbdata/datanode4406/data/ scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/shard-shard1/* 192.168.0.83:/greatdb/dbdata/datanode4406/data/ scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/shard-shard2/* 192.168.0.81:/greatdb/dbdata/datanode4407/data/ scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/shard-shard2/* 192.168.0.82:/greatdb/dbdata/datanode4407/data/ scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/shard-shard2/* 192.168.0.83:/greatdb/dbdata/datanode4407/data/ scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/shard-shard3/* 192.168.0.81:/greatdb/dbdata/datanode4408/data/ scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/shard-shard3/* 192.168.0.82:/greatdb/dbdata/datanode4408/data/ scp -rp /greatdb/dbbackup/full_backup_20220301-032132_176/shard-shard3/* 192.168.0.83:/greatdb/dbdata/datanode4408/data/
# 所有 SqlNode 和 DataNode 修改数据文件的权限 chown -R greatdb:greatdb /greatdb/dbdata/*/data/* # 所有 SqlNode 和 DataNode 将 redo 文件和 undo 文件从 data 目录复制到 log 目录下 mv /greatdb/dbdata/sqlnode3306/data/{ib_logfile*,undo_*} /greatdb/dbdata/sqlnode3306/log mv /greatdb/dbdata/datanode4406/data/{ib_logfile*,undo_*} /greatdb/dbdata/datanode4406/log mv /greatdb/dbdata/datanode4407/data/{ib_logfile*,undo_*} /greatdb/dbdata/datanode4407/log mv /greatdb/dbdata/datanode4408/data/{ib_logfile*,undo_*} /greatdb/dbdata/datanode4408/log
# 恢复所有 SqlNode 和 DataNode 的配置文件,因为在原机恢复,此处省略,如果异机恢复,注意修改配置文件的IP等配置 [root@greatdb1 ~]# ll /greatdb/conf/ total 48 -rw-r--r--. 1 greatdb greatdb 8232 Feb 26 2021 datanode4406.cnf -rw-r--r--. 1 greatdb greatdb 8232 Feb 26 2021 datanode4407.cnf -rw-r--r--. 1 greatdb greatdb 8232 Feb 26 2021 datanode4408.cnf -rw-r--r--. 1 greatdb greatdb 8209 Feb 26 2021 sqlnode3306.cnf
/greatdb/sh/sqlnode3306.sh start /greatdb/sh/datanode4406.sh start /greatdb/sh/datanode4407.sh start /greatdb/sh/datanode4408.sh start
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> select * from information_schema.GREATDB_SQLNODES; GreatDB Cluster[(none)]> select * from information_schema.GREATDB_DATANODES;

需要在备份节点提前安装一个 MySQL 客户端,yum install -y mysql
# 在备份节点向 SqlNode 恢复全备里的增量 binlog cd /greatdb/dbbackup/full_backup_20220301-032132_176/inc_binlog/sqlnode greatsqlbinlog sqlnode-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 --init-command="set greatdb_is_restore_binlog_applier=on" greatsqlbinlog sqlnode-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.82 -P3306 --init-command="set greatdb_is_restore_binlog_applier=on" greatsqlbinlog sqlnode-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.83 -P3306 --init-command="set greatdb_is_restore_binlog_applier=on" # 在备份节点向 DataNode 恢复全备里的增量 binlog cd /greatdb/dbbackup/full_backup_20220301-032132_176/inc_binlog/shard-shard1 greatsqlbinlog shard-shard1-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.81 -P4406 greatsqlbinlog shard-shard1-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.82 -P4406 greatsqlbinlog shard-shard1-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.83 -P4406 cd /greatdb/dbbackup/full_backup_20220301-032132_176/inc_binlog/shard-shard2 greatsqlbinlog shard-shard2-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.81 -P4407 greatsqlbinlog shard-shard2-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.82 -P4407 greatsqlbinlog shard-shard2-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.83 -P4407 cd /greatdb/dbbackup/full_backup_20220301-032132_176/inc_binlog/shard-shard3 greatsqlbinlog shard-shard3-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.81 -P4408 greatsqlbinlog shard-shard3-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.82 -P4408 greatsqlbinlog shard-shard3-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.83 -P4408 # 在备份节点向 SqlNode 恢复增量备份 cd /greatdb/dbbackup/inc_backup_20220301-050855_183/inc_binlog/sqlnode # 这下面有两个文件 sqlnode-bin.000001 和 sqlnode-bin.000002 greatsqlbinlog sqlnode-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 --init-command="set greatdb_is_restore_binlog_applier=on" greatsqlbinlog sqlnode-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.82 -P3306 --init-command="set greatdb_is_restore_binlog_applier=on" greatsqlbinlog sqlnode-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.83 -P3306 --init-command="set greatdb_is_restore_binlog_applier=on" greatsqlbinlog sqlnode-bin.000002 |mysql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 --init-command="set greatdb_is_restore_binlog_applier=on" greatsqlbinlog sqlnode-bin.000002 |mysql -ugreatdb -pgreatdb -h192.168.0.82 -P3306 --init-command="set greatdb_is_restore_binlog_applier=on" greatsqlbinlog sqlnode-bin.000002 |mysql -ugreatdb -pgreatdb -h192.168.0.83 -P3306 --init-command="set greatdb_is_restore_binlog_applier=on" # 在备份节点向 DataNode 恢复增量备份 cd /greatdb/dbbackup/inc_backup_20220301-050855_183/inc_binlog/shard-shard1 greatsqlbinlog shard-shard1-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.81 -P4406 greatsqlbinlog shard-shard1-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.82 -P4406 greatsqlbinlog shard-shard1-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.83 -P4406 cd /greatdb/dbbackup/inc_backup_20220301-050855_183/inc_binlog/shard-shard2 greatsqlbinlog shard-shard2-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.81 -P4407 greatsqlbinlog shard-shard2-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.82 -P4407 greatsqlbinlog shard-shard2-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.83 -P4407 cd /greatdb/dbbackup/inc_backup_20220301-050855_183/inc_binlog/shard-shard3 greatsqlbinlog shard-shard3-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.81 -P4408 greatsqlbinlog shard-shard3-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.82 -P4408 greatsqlbinlog shard-shard3-bin.000001 |mysql -ugreatdb -pgreatdb -h192.168.0.83 -P4408
[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 # 恢复 SqlNode GreatDB Cluster[(none)]> call mysql.greatdb_restore_sqlnode_from_backup; Query OK, 1 row affected (3.94 sec) GreatDB Cluster[(none)]> select * from information_schema.GREATDB_SQLNODES; +--------------------------------------+--------------+------+--------+---------------+--------------------------+ | UUID | HOST | PORT | STATUS | EXECUTED_GTID | Received_transaction_set | +--------------------------------------+--------------+------+--------+---------------+--------------------------+ | dc0e4eaf-7a26-11eb-8c6d-000c29d936e4 | 192.168.0.81 | 3306 | ONLINE | | | +--------------------------------------+--------------+------+--------+---------------+--------------------------+ 1 row in set (0.00 sec) # 恢复 DataNode GreatDB Cluster[(none)]> call mysql.greatdb_restore_shard_from_backup('shard1','datanode1','192.168.0.81',4406,'NODE_MGR'); Query OK, 1 row affected (4.25 sec) GreatDB Cluster[(none)]> call mysql.greatdb_restore_shard_from_backup('shard2','datanode4','192.168.0.81',4407,'NODE_MGR'); Query OK, 1 row affected (4.82 sec) GreatDB Cluster[(none)]> call mysql.greatdb_restore_shard_from_backup('shard3','datanode7','192.168.0.81',4408,'NODE_MGR'); Query OK, 1 row affected (4.30 sec) GreatDB Cluster[(none)]> select NODE_ID,NODE_NAME,SHARD_ID,SHARD_NAME,HOST,PORT,NODE_TYPE,NODE_STATE from information_schema.GREATDB_DATANODES; +---------+-----------+----------+------------+--------------+------+-----------+--------------+ | NODE_ID | NODE_NAME | SHARD_ID | SHARD_NAME | HOST | PORT | NODE_TYPE | NODE_STATE | +---------+-----------+----------+------------+--------------+------+-----------+--------------+ | 185 | datanode1 | 36 | shard1 | 192.168.0.81 | 4406 | NODE_MGR | STATE_ACTIVE | | 186 | datanode4 | 64 | shard2 | 192.168.0.81 | 4407 | NODE_MGR | STATE_ACTIVE | | 187 | datanode7 | 92 | shard3 | 192.168.0.81 | 4408 | NODE_MGR | STATE_ACTIVE | +---------+-----------+----------+------------+--------------+------+-----------+--------------+ 3 rows in set (0.00 sec)

当前只恢复了一个 SqlNode,每个 Shard 中也只恢复一个 DataNode,如果还想加入其他 SqlNode 和 DataNode,参考如下。
# 添加 SqlNode GreatDB Cluster[(none)]> call mysql.greatdb_add_sqlnode('192.168.0.82',3306); Query OK, 0 rows affected (2.46 sec) GreatDB Cluster[(none)]> call mysql.greatdb_add_sqlnode('192.168.0.83',3306); Query OK, 0 rows affected (3.25 sec) GreatDB Cluster[(none)]> select * from information_schema.GREATDB_SQLNODES; +--------------------------------------+--------------+------+--------+---------------+--------------------------+ | UUID | HOST | PORT | STATUS | EXECUTED_GTID | Received_transaction_set | +--------------------------------------+--------------+------+--------+---------------+--------------------------+ | dc0e4eaf-7a26-11eb-8c6d-000c29d936e4 | 192.168.0.81 | 3306 | ONLINE | | | | de731ff9-7a26-11eb-935f-000c290f05e6 | 192.168.0.82 | 3306 | ONLINE | | | | e1f43eab-7a26-11eb-b240-000c293f8a6a | 192.168.0.83 | 3306 | ONLINE | | | +--------------------------------------+--------------+------+--------+---------------+--------------------------+ 3 rows in set (0.01 sec) # 添加 DataNode GreatDB Cluster[(none)]> call mysql.greatdb_add_datanode('shard1','datanode2','192.168.0.82',4406,'NODE_MGR'); Query OK, 1 row affected (3.26 sec) GreatDB Cluster[(none)]> call mysql.greatdb_add_datanode('shard1','datanode3','192.168.0.83',4406,'NODE_MGR'); Query OK, 1 row affected (3.01 sec) GreatDB Cluster[(none)]> call mysql.greatdb_add_datanode('shard2','datanode5','192.168.0.82',4407,'NODE_MGR'); Query OK, 1 row affected (3.22 sec) GreatDB Cluster[(none)]> call mysql.greatdb_add_datanode('shard2','datanode6','192.168.0.83',4407,'NODE_MGR'); Query OK, 1 row affected (2.52 sec) GreatDB Cluster[(none)]> call mysql.greatdb_add_datanode('shard3','datanode8','192.168.0.82',4408,'NODE_MGR'); Query OK, 1 row affected (3.20 sec) GreatDB Cluster[(none)]> call mysql.greatdb_add_datanode('shard3','datanode9','192.168.0.83',4408,'NODE_MGR'); Query OK, 1 row affected (3.05 sec) GreatDB Cluster[(none)]> select NODE_ID,NODE_NAME,SHARD_ID,SHARD_NAME,HOST,PORT,NODE_TYPE,NODE_STATE from information_schema.GREATDB_DATANODES; +---------+-----------+----------+------------+--------------+------+-----------+---------------+ | NODE_ID | NODE_NAME | SHARD_ID | SHARD_NAME | HOST | PORT | NODE_TYPE | NODE_STATE | +---------+-----------+----------+------------+--------------+------+-----------+---------------+ | 185 | datanode1 | 36 | shard1 | 192.168.0.81 | 4406 | NODE_MGR | STATE_ACTIVE | | 186 | datanode4 | 64 | shard2 | 192.168.0.81 | 4407 | NODE_MGR | STATE_ACTIVE | | 187 | datanode7 | 92 | shard3 | 192.168.0.81 | 4408 | NODE_MGR | STATE_ACTIVE | | 192 | datanode2 | 36 | shard1 | 192.168.0.82 | 4406 | NODE_MGR | STATE_STANDBY | | 193 | datanode3 | 36 | shard1 | 192.168.0.83 | 4406 | NODE_MGR | STATE_STANDBY | | 194 | datanode5 | 64 | shard2 | 192.168.0.82 | 4407 | NODE_MGR | STATE_STANDBY | | 195 | datanode6 | 64 | shard2 | 192.168.0.83 | 4407 | NODE_MGR | STATE_STANDBY | | 196 | datanode8 | 92 | shard3 | 192.168.0.82 | 4408 | NODE_MGR | STATE_STANDBY | | 197 | datanode9 | 92 | shard3 | 192.168.0.83 | 4408 | NODE_MGR | STATE_STANDBY | +---------+-----------+----------+------------+--------------+------+-----------+---------------+ 9 rows in set (0.02 sec)

[root@greatdb1 ~]# greatsql -ugreatdb -pgreatdb -h192.168.0.81 -P3306 GreatDB Cluster[(none)]> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed GreatDB Cluster[test]> show tables; +----------------+ | Tables_in_test | +----------------+ | t_global | | t_hash | | t_list | | t_normal | | t_range | +----------------+ 5 rows in set (0.00 sec) GreatDB Cluster[test]> select * from t_normal; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +----+------+ 3 rows in set (0.00 sec)
GreatDB生态兼容备份工具:
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946831/viewspace-3168261/ ,如需转载,请注明出处,否则将追究法律责任。