clickHouse 数据库备份方案 (2)

# 2 备份复制到表的“ detached”目录中:

cp -rl /data/clickhouse/data/backup/20201016/shadow/1/data/caihao/ch_test_customer/* /data/clickhouse/data/data/caihao/test_restore_tab/detached/
chown clickhouse:clickhouse -R /data/clickhouse/data/data/caihao/test_restore_tab/detached/* 

# 3 将数据添加到表中 attach partition

echo \'alter table caihao.test_restore_tab attach partition 202010 \' | clickhouse-client
echo \'alter table caihao.test_restore_tab attach partition 202009 \' | clickhouse-client

要把所有分区都执行一遍,最终detached 目录下所有的分区,都移动到了上一目录

# 4 确认数据已还原:

echo \'select count() from caihao.test_restore_tab attach\' | clickhouse-client

clickhouse-01 :)  select count(*) from test_restore_tab;

SELECT count(*)
FROM test_restore_tab

┌─count()─┐
│ 8990020 │
└─────────┘

1 rows in set. Elapsed: 0.002 sec. 

clickhouse-01 :)  select count(*) from ch_test_customer;

SELECT count(*)
FROM ch_test_customer

┌─count()─┐
│ 8990020 │
└─────────┘

1 rows in set. Elapsed: 0.002 sec. 

数据量和原表一致

4. Clickhouse-Backup

# Clickhouse-Backup简介

https://github.com/AlexAkulov/clickhouse-backup

# 使用限制:

支持1.1.54390以上的ClickHouse

仅MergeTree系列表引擎

不支持备份Tiered storage或storage_policy 

云存储上的最大备份大小为5TB

AWS S3上的parts数最大为10,000

--安装方式1:二进制文件安装

# clickhouse-backup下载:

wget https://github.com/AlexAkulov/clickhouse-backup/releases/download/v0.6.0/clickhouse-backup.tar.gz 

# 解压即用

tar -xf clickhouse-backup.tar.gz 
cd clickhouse-backup / 
sudo cp clickhouse-backup  /usr/local/bin 

--安装方式2:rpm安装:

wget https://github.com/AlexAkulov/clickhouse-backup/releases/download/v0.6.0/clickhouse-backup-0.6.0-1.x86_64.rpm
rpm -ivh clickhouse-backup-0.6.0-1.x86_64.rpm

# 查看版本

[root@clickhouse-01 clickhouse-backup]# clickhouse-backup -v
Version:         0.6.0
Git Commit:      7d7df1e36575f0d94d330c7bfe00aef7a2100276
Build Date:      2020-10-02

# 编辑配置文件:

mkdir -p /etc/clickhouse-backup/
vi /etc/clickhouse-backup/config.yml

添加一些基本的配置信息

general:
  remote_storage: none
  backups_to_keep_local: 7
  backups_to_keep_remote: 31
clickhouse:
  username: default
  password: ""
  host: localhost
  port: 9000
  data_path: "/data/clickhouse/data"

# 查看全部默认的配置项

clickhouse-backup default-config

# 查看可备份的表

clickhouse-backup tables

# 创建备份

1. 全库备份

clickhouse-backup create

备份存储在中 $data_path/backup 下,备份名称默认为时间戳,可手动指定备份名称。例如:

clickhouse-backup create ch_bk_20201020

备份包含两个目录:

\'metadata\'目录: 包含重新创建所需的DDL SQL

\'shadow\'目录: 包含作为ALTER TABLE ... FREEZE操作结果的数据。

2. 单表备份

语法:

clickhouse-backup create [-t, --tables=<db>.<table>] <backup_name>

备份表caihao.ch_test_customer

clickhouse-backup create  -t caihao.ch_test_customer ch_test_customer 

3. 备份多个表

clickhouse-backup create  -t caihao.test_restore_tab,caihao.ch1 ch_bak_2tab

# 查看备份文件

[root@clickhouse-01 backup]# clickhouse-backup list
Local backups:
- \'test20201019\'        (created at 20-10-2020 14:18:40)
- \'ch_bk_20201020\'      (created at 20-10-2020 14:20:35)
- \'2020-10-20T06-27-08\' (created at 20-10-2020 14:27:08)
- \'ch_test_customer\'    (created at 20-10-2020 15:17:13)
- \'ch_bak_2tab\' (created at 20-10-2020 15:33:41)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zggfjd.html