1. Home
  2. Knowledge Base
  3. ClickHouse
  4. ClickHouse Backup Strategies – Part 1
  1. Home
  2. Knowledge Base
  3. ClickHouse DBA
  4. ClickHouse Backup Strategies – Part 1

ClickHouse Backup Strategies – Part 1

Introduction

In ClickHouse, replication does not protect the system from accidental deletes on tables or some records on the tables. For that reason, backup and recovery strategies are crucial for companies.

These are some of the backup methods that can be used.

  • Export / Import
  • Duplication Source Table(s)
  • Filesystem Snapshots
  • Using Tool( clickhouse-copier)
  • Manipulations with Parts

In this article, we will discuss the “Export / Import” method for protecting table(s) from accidental deletions.

Environment

OS : Ubuntu Linux 18.04

DB: ClickHouse v22.2.2.1

 

Steps

Export Phase:

  • Use “customerTest” table with 10 records with the following columns:
ChistaDATA :) desc customerTest

DESCRIBE TABLE customerTest

Query id: 014edd8e-fa06-47d8-b42a-c36569ed2549

┌─name─────────────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ customerNumber   │ Int32  │              │                    │         │                  │                │
│ customerName     │ String │              │                    │         │                  │                │
│ contactLastName  │ String │              │                    │         │                  │                │
│ contactFirstName │ String │              │                    │         │                  │                │
└──────────────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘

4 rows in set. Elapsed: 0.002 sec. 
ChistaDATA :) select * from customerTest;

SELECT *
FROM customerTest

Query id: 18cb31c3-e2e1-4994-9ea9-2611c6c7f10e

┌─customerNumber─┬─customerName─────────────────┬─contactLastName─┬─contactFirstName─┐
│            103 │ Atelier graphique            │ Schmitt         │ Carine           │
│            112 │ Signal Gift Stores           │ King            │ Jean             │
│            114 │ Australian Collectors, Co.   │ Ferguson        │ Peter            │
│            119 │ La Rochelle Gifts            │ Labrune         │ Janine           │
│            121 │ Baane Mini Imports           │ Bergulfsen      │ Jonas            │
│            124 │ Mini Gifts Distributors Ltd. │ Nelson          │ Susan            │
│            125 │ Havel & Zbyszek Co           │ Piestrzeniewicz │ Zbyszek          │
│            128 │ Blauer See Auto, Co.         │ Keitel          │ Roland           │
│            129 │ Mini Wheels Co.              │ Murphy          │ Julie            │
│            131 │ Land of Toys Inc.            │ Lee             │ Kwai             │
└────────────────┴──────────────────────────────┴─────────────────┴──────────────────┘

10 rows in set. Elapsed: 0.004 sec.
  • Create directory for exporting table and give permission for access:
root@ubuntu1804:/# mkdir -p /clickhouse/export

root@ubuntu1804:/# chown -R clickhouse:clickhouse /clickhouse
  • Export data as CSV to given file
ChistaDATA :) select * from customerTest into outfile '/clickhouse/export/customers.csv' FORMAT CSV;

Import Phase:

  • Create symbolic link under “/var/lib/clickhouse/user_files” for accessing “customers.csv” file from ClickHouse:
root@ubuntu1804:/var/lib/clickhouse/user_files# ln -s /clickhouse/export/customers.csv customers.csv
root@ubuntu1804:/var/lib/clickhouse/user_files# ls -al
total 8
drwxr-x---  2 clickhouse clickhouse 4096 Jul 26 06:48 .
drwx------ 15 clickhouse clickhouse 4096 Jul 26 06:06 ..
lrwxrwxrwx  1 root       root         32 Jul 26 06:48 customers.csv -> /clickhouse/export/customers.csv
  • Query “customer.csv” file for access control: 
SELECT *
FROM file('customers.csv', 'CSV', '`customerNumber` Int32,`customerName` String, `contactLastName` String, `contactFirstName` String')
  • Create new table  from “customers.csv” file:
ChistaDATA :) create table backuptest.customers Engine=MergeTree()  ORDER BY customerNumber as select * from file ('customers.csv','CSV','`customerNumber` Int32,`customerName` String, `contactLastName` String, `contactFirstName` String');

CREATE TABLE backuptest.customers
ENGINE = MergeTree
ORDER BY customerNumber AS
SELECT *
FROM file('customers.csv', 'CSV', '`customerNumber` Int32,`customerName` String, `contactLastName` String, `contactFirstName` String')

Query id: fb82cfc3-4be7-40b6-a52a-361842776d87

Ok.

0 rows in set. Elapsed: 0.012 sec. 
  • Query restored table for controlling the records:
ubuntu1804.localdomain :) select * from backuptest.customers;

SELECT *
FROM backuptest.customers

Query id: 705d1356-fa4b-45a0-a545-753b061e4a07

┌─customerNumber─┬─customerName─────────────────┬─contactLastName─┬─contactFirstName─┐
│            103 │ Atelier graphique            │ Schmitt         │ Carine           │
│            112 │ Signal Gift Stores           │ King            │ Jean             │
│            114 │ Australian Collectors, Co.   │ Ferguson        │ Peter            │
│            119 │ La Rochelle Gifts            │ Labrune         │ Janine           │
│            121 │ Baane Mini Imports           │ Bergulfsen      │ Jonas            │
│            124 │ Mini Gifts Distributors Ltd. │ Nelson          │ Susan            │
│            125 │ Havel & Zbyszek Co           │ Piestrzeniewicz │ Zbyszek          │
│            128 │ Blauer See Auto, Co.         │ Keitel          │ Roland           │
│            129 │ Mini Wheels Co.              │ Murphy          │ Julie            │
│            131 │ Land of Toys Inc.            │ Lee             │ Kwai             │
└────────────────┴──────────────────────────────┴─────────────────┴──────────────────┘

10 rows in set. Elapsed: 0.003 sec.

 

Was this article helpful?

Related Articles

CHISTADATA IS COMMITTED TO OPEN SOURCE SOFTWARE AND BUILDING HIGH PERFORMANCE COLUMNSTORES

In the spirit of freedom, independence and innovation. ChistaDATA Corporation is not affiliated with ClickHouse Corporation 

Need Support?

Can't find the answer you're looking for?
Contact Support

ChistaDATA Inc. Knowledge base is licensed under the Apache License, Version 2.0 (the “License”)

Copyright 2022 ChistaDATA Inc

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.