admin 管理员组

文章数量: 1184232

我们评估一款数据库产品,除了稳定性和易用性外,数据安全也十分重要,备份与恢复往往是最后一道安全门。

但如果备份策略不完善、恢复手段无效,一旦发生数据误删除就真的抢救无效-扑街了。

目前Clickhouse的备份方式有以下几种:

  • 文本文件导入导出

  • 表快照

  • ALTER TABLE…FREEZE

  • 备份工具Clickhouse-Backup

  • Clickhouse-Copier

下面就逐个试试吧。

# 数据备份 概述

https://clickhouse.tech/docs/en/operations/backup/


1. 文本文件导入导出

# 测试数据

MySQL中源数据6.70G,表数据量899万

--测试表数据量899万
--MySQL中源数据6.70G
0 rows in set. Elapsed: 71.482 sec. Processed 8.99 million rows, 6.70 GB (125.77 thousand rows/s., 93.71 MB/s.)

# 导出

clickhouse-client --query="select * from caihao.ch_test_customer" > /data/clickhouse/tmp/caihao.ch_test_customer.tsv

# 导入 (注意FORMAT后面大写) 多个文件可以用 ch_test*

cat /data/clickhouse/tmp/caihao.ch_test_customer.tsv | clickhouse-client --query="insert into caihao.ch_test_customer FORMAT TSV"

速度:导入需要20多秒

# CH文件磁盘占用  368MB

368     ch_test_customer

# 备份文件3.5G 压缩后139MB

[root@clickhouse-01 tmp]# du -hsm *
3539    caihao.ch_test_customer.tsv
[root@clickhouse-01 tmp]# gzip caihao.ch_test_customer.tsv
[root@clickhouse-01 tmp]# du -hsm *
139     caihao.ch_test_customer.tsv.gz

# 对比下占用空间:

  • MySQL -- 6.7G

  • ClickHouse -- 368M

  • 导出文本 -- 3.5G

  • 压缩后 -- 139M

2. CTAS表快照

# 1 本地复制表

clickhouse-01 :) create table ch1  as ch_test_customer ;

CREATE TABLE ch1 AS ch_test_customer
Ok.

0 rows in set. Elapsed: 0.006 sec. 

clickhouse-01 :) insert into table ch1 select * from ch_test_customer ;

INSERT INTO ch1 SELECT *
FROM ch_test_customer
Ok.

0 rows in set. Elapsed: 18.863 sec. Processed 8.99 million rows, 6.70 GB (476.59 thousand rows/s., 355.13 MB/s.)

# 2 远程复制表

https://clickhouse.tech/docs/en/sql-reference/table-functions/remote/

-# 语法
remote('addresses_expr', db, table[, 'user'[, 'password']])
remote('addresses_expr', db.table[, 'user'[, 'password']])

-# 例子:
dba-docker :) insert into table ch1 select * from remote ('10.222.2.222','caihao.ch_test_customer','ch_app','qwerty_123');

INSERT INTO ch1 SELECT *
FROM remote('10.222.2.222', 'caihao.ch_test_customer', 'ch_app', 'qwerty_123')

Ok.

0 rows in set. Elapsed: 17.914 sec. Processed 8.99 million rows, 6.70 GB (501.85 thousand rows/s., 373.95 MB/s.)

3. ALTER TABLE…FREEZE

语法:

ALTER TABLE table_name FREEZE [PARTITION partition_expr]

该操作为指定分区创建一个本地备份。

如果 PARTITION 语句省略,该操作会一次性为所有分区创建备份。整个备份过程不需要停止服务

注意:FREEZE PART

本文标签: 备份 clickhouse