Cassandra 文件

版本

您正在檢視預發行版本的說明文件。

備份

Apache Cassandra 將資料儲存在不可變的 SSTable 檔案中。Apache Cassandra 資料庫中的備份是儲存為 SSTable 檔案的資料庫資料備份副本。備份用於下列幾個目的,包括

  • 儲存資料副本以確保耐用性

  • 如果由於節點/分割區/網路故障而遺失表格資料,則能夠還原表格

  • 能夠將 SSTable 檔案傳輸到不同的機器;以利攜帶性

備份類型

Apache Cassandra 支援兩種備份策略。

  • 快照

  • 增量備份

快照是表格 SSTable 檔案在特定時間的副本,透過硬式連結建立。建立表格的 DDL 也會儲存。快照可以由使用者建立或自動建立。cassandra.yaml 檔案中的 snapshot_before_compaction 設定會決定是否在每次壓縮之前建立快照。預設情況下,snapshot_before_compaction 設定為 false。可以在 cassandra.yaml 中將 auto_snapshot 設定為 true(預設值)來設定在截斷鍵值空間或刪除表格之前自動建立快照。自動快照可能會導致截斷延遲,而 cassandra.yaml 中的另一個設定會決定協調器應該等待截斷完成多久。預設情況下,Cassandra 會等待 60 秒讓自動快照完成。

增量備份是表格 SSTable 檔案的副本,當記憶表沖刷到磁碟中作為 SSTable 時,會透過硬式連結建立。增量備份通常會與快照配對,以減少備份時間並減少磁碟空間。增量備份並未預設啟用,必須在 cassandra.yaml(使用 incremental_backups 設定)或使用 nodetool 明確啟用。啟用後,Cassandra 會在鍵值空間資料的 backups/ 子目錄中建立到每個在本地沖刷或串流的 SSTable 的硬式連結。也會建立系統表格的增量備份。

資料目錄結構

Cassandra 資料的目錄結構包含不同目錄,用於儲存鍵空間和資料檔案所在的表格目錄中的表格。特定表格的目錄備份和快照,分別用於儲存備份和快照,也儲存在表格目錄中。Cassandra 的目錄結構如圖 1 所示。

Data directory structure for backups

圖 1. Cassandra 資料的目錄結構

設定備份和快照範例表格

在本節中,我們將建立一些範例資料,可用於示範增量備份和快照。我們使用了三個節點的 Cassandra 集群。首先,建立鍵空間。然後在鍵空間中建立表格,並新增表格資料。我們使用了兩個鍵空間 cqlkeyspacecatalogkeyspace,每個鍵空間中包含兩個表格。

建立鍵空間 cqlkeyspace

CREATE KEYSPACE cqlkeyspace
   WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};

cqlkeyspace 鍵空間中建立兩個表格 tt2

USE cqlkeyspace;
CREATE TABLE t (
   id int,
   k int,
   v text,
   PRIMARY KEY (id)
);
CREATE TABLE t2 (
   id int,
   k int,
   v text,
   PRIMARY KEY (id)
);

將資料新增至表格

INSERT INTO t (id, k, v) VALUES (0, 0, 'val0');
INSERT INTO t (id, k, v) VALUES (1, 1, 'val1');

INSERT INTO t2 (id, k, v) VALUES (0, 0, 'val0');
INSERT INTO t2 (id, k, v) VALUES (1, 1, 'val1');
INSERT INTO t2 (id, k, v) VALUES (2, 2, 'val2');

查詢表格以列出資料

SELECT * FROM t;
SELECT * FROM t2;

結果為

id | k | v
----+---+------
 1 | 1 | val1
 0 | 0 | val0

 (2 rows)


id | k | v
----+---+------
 1 | 1 | val1
 0 | 0 | val0
 2 | 2 | val2

 (3 rows)

建立第二個鍵空間 catalogkeyspace

CREATE KEYSPACE catalogkeyspace
   WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};

catalogkeyspace 中建立兩個表格 journalmagazine

USE catalogkeyspace;
CREATE TABLE journal (
   id int,
   name text,
   publisher text,
   PRIMARY KEY (id)
);

CREATE TABLE magazine (
   id int,
   name text,
   publisher text,
   PRIMARY KEY (id)
);

將資料新增至表格

INSERT INTO journal (id, name, publisher) VALUES (0, 'Apache Cassandra Magazine', 'Apache Cassandra');
INSERT INTO journal (id, name, publisher) VALUES (1, 'Couchbase Magazine', 'Couchbase');

INSERT INTO magazine (id, name, publisher) VALUES (0, 'Apache Cassandra Magazine', 'Apache Cassandra');
INSERT INTO magazine (id, name, publisher) VALUES (1, 'Couchbase Magazine', 'Couchbase');

查詢表格以列出資料

SELECT * FROM catalogkeyspace.journal;
SELECT * FROM catalogkeyspace.magazine;

結果為

id | name                      | publisher
----+---------------------------+------------------
 1 |        Couchbase Magazine |        Couchbase
 0 | Apache Cassandra Magazine | Apache Cassandra

 (2 rows)

id | name                      | publisher
----+---------------------------+------------------
 1 |        Couchbase Magazine |        Couchbase
 0 | Apache Cassandra Magazine | Apache Cassandra

 (2 rows)

快照

在本節中,我們示範如何建立快照。用於建立快照的指令為 nodetool snapshot,用法為

$ nodetool help snapshot

結果為

NAME
       nodetool snapshot - Take a snapshot of specified keyspaces or a snapshot
       of the specified table

SYNOPSIS
       nodetool [(-h <host> | --host <host>)] [(-p <port> | --port <port>)]
               [(-pp | --print-port)] [(-pw <password> | --password <password>)]
               [(-pwf <passwordFilePath> | --password-file <passwordFilePath>)]
               [(-u <username> | --username <username>)] snapshot
               [(-cf <table> | --column-family <table> | --table <table>)]
               [(-kt <ktlist> | --kt-list <ktlist> | -kc <ktlist> | --kc.list <ktlist>)]
               [(-sf | --skip-flush)] [(-t <tag> | --tag <tag>)] [--] [<keyspaces...>]

OPTIONS
       -cf <table>, --column-family <table>, --table <table>
           The table name (you must specify one and only one keyspace for using
           this option)

       -h <host>, --host <host>
           Node hostname or ip address

       -kt <ktlist>, --kt-list <ktlist>, -kc <ktlist>, --kc.list <ktlist>
           The list of Keyspace.table to take snapshot.(you must not specify
           only keyspace)

       -p <port>, --port <port>
           Remote jmx agent port number

       -pp, --print-port
           Operate in 4.0 mode with hosts disambiguated by port number

       -pw <password>, --password <password>
           Remote jmx agent password

       -pwf <passwordFilePath>, --password-file <passwordFilePath>
           Path to the JMX password file

       -sf, --skip-flush
           Do not flush memtables before snapshotting (snapshot will not
           contain unflushed data)

       -t <tag>, --tag <tag>
           The name of the snapshot

       -u <username>, --username <username>
           Remote jmx agent username

       --
           This option can be used to separate command-line options from the
           list of argument, (useful when arguments might be mistaken for
           command-line options

       [<keyspaces...>]
           List of keyspaces. By default, all keyspaces

設定快照

為了示範如何在命令列上使用 Nodetool 建立快照,我們已將 cassandra.yaml 檔案中的 auto_snapshots 設定設為 false

auto_snapshot: false

同時將 snapshot_before_compaction 設為 false,以停用在壓縮前自動建立快照

snapshot_before_compaction: false

建立快照

在建立任何快照之前,請搜尋快照,不會列出任何快照

$ find -name snapshots

我們將使用範例鍵空間和表格來建立快照。

擷取鍵空間中所有表格的快照

使用上述語法,為 catalogkeyspace 鍵空間中的所有表格建立一個名為 catalog-ks 的快照

$ nodetool snapshot --tag catalog-ks catalogkeyspace

結果為

Requested creating snapshot(s) for [catalogkeyspace] with snapshot name [catalog-ks] and
options {skipFlush=false}
Snapshot directory: catalog-ks

使用上述 find 指令,現在會找到快照和 snapshots 目錄,其中列出的檔案類似於

./cassandra/data/data/catalogkeyspace/journal-296a2d30c22a11e9b1350d927649052c/snapshots
./cassandra/data/data/catalogkeyspace/magazine-446eae30c22a11e9b1350d927649052c/snapshots

可以類似地建立多個鍵空間中所有資料表的快照

$ nodetool snapshot --tag catalog-cql-ks catalogkeyspace, cqlkeyspace

在鍵空間中建立單一資料表的快照

若要建立單一資料表的快照,nodetool snapshot 命令語法如下

$ nodetool snapshot --tag <tag> --table <table>  --<keyspace>

使用上述語法,為鍵空間 catalogkeyspace 中的資料表 magazine 建立快照

$ nodetool snapshot --tag magazine --table magazine  catalogkeyspace

結果為

Requested creating snapshot(s) for [catalogkeyspace] with snapshot name [magazine] and
options {skipFlush=false}
Snapshot directory: magazine

從同一個鍵空間建立多個資料表的快照

若要建立鍵空間中多個資料表的快照,必須使用選項 --kt-list 指定 Keyspace.table 清單。例如,為 cqlkeyspace 鍵空間中的資料表 tt2 建立快照

$ nodetool snapshot --kt-list cqlkeyspace.t,cqlkeyspace.t2 --tag multi-table

結果為

Requested creating snapshot(s) for ["CQLKeyspace".t,"CQLKeyspace".t2] with snapshot name [multi-
table] and options {skipFlush=false}
Snapshot directory: multi-table

可以建立同一組資料表的快照,並標記不同的名稱。例如,為 cqlkeyspace 鍵空間中的資料表 tt2 建立另一組快照,並標記不同的快照

$ nodetool snapshot --kt-list cqlkeyspace.t, cqlkeyspace.t2 --tag multi-table-2

結果為

Requested creating snapshot(s) for ["CQLKeyspace".t,"CQLKeyspace".t2] with snapshot name [multi-
table-2] and options {skipFlush=false}
Snapshot directory: multi-table-2

從不同的鍵空間建立多個資料表的快照

若要建立位於不同鍵空間的多個資料表的快照,命令語法與多個資料表位於同一個鍵空間時相同。每個 <keyspace>.<table> 都必須在 --kt-list 選項中分別指定。

例如,為 cqlkeyspace 中的資料表 tcatalogkeyspace 中的資料表 journal 建立快照,並標記快照 multi-ks

$ nodetool snapshot --kt-list catalogkeyspace.journal,cqlkeyspace.t --tag multi-ks

結果為

Requested creating snapshot(s) for [catalogkeyspace.journal,cqlkeyspace.t] with snapshot
name [multi-ks] and options {skipFlush=false}
Snapshot directory: multi-ks

列出快照

若要列出快照,請使用 nodetool listsnapshots 命令。在前面的範例中建立的所有快照都會列出

$ nodetool listsnapshots

結果為

Snapshot Details:
Snapshot name Keyspace name   Column family name True size Size on disk
multi-table   cqlkeyspace     t2                 4.86 KiB  5.67 KiB
multi-table   cqlkeyspace     t                  4.89 KiB  5.7 KiB
multi-ks      cqlkeyspace     t                  4.89 KiB  5.7 KiB
multi-ks      catalogkeyspace journal            4.9 KiB   5.73 KiB
magazine      catalogkeyspace magazine           4.9 KiB   5.73 KiB
multi-table-2 cqlkeyspace     t2                 4.86 KiB  5.67 KiB
multi-table-2 cqlkeyspace     t                  4.89 KiB  5.7 KiB
catalog-ks    catalogkeyspace journal            4.9 KiB   5.73 KiB
catalog-ks    catalogkeyspace magazine           4.9 KiB   5.73 KiB

Total TrueDiskSpaceUsed: 44.02 KiB

尋找快照目錄

可以使用 find –name snapshots 命令列出 snapshots 目錄

$ find -name snapshots

結果為

./cassandra/data/data/cqlkeyspace/t-d132e240c21711e9bbee19821dcea330/snapshots
./cassandra/data/data/cqlkeyspace/t2-d993a390c22911e9b1350d927649052c/snapshots
./cassandra/data/data/catalogkeyspace/journal-296a2d30c22a11e9b1350d927649052c/snapshots
./cassandra/data/data/catalogkeyspace/magazine-446eae30c22a11e9b1350d927649052c/snapshots

若要列出特定資料表的快照,請先變更為該資料表的快照目錄。例如,列出 catalogkeyspace/journal 資料表的快照

$ cd ./cassandra/data/data/catalogkeyspace/journal-296a2d30c22a11e9b1350d927649052c/snapshots && ls -l

結果為

total 0
drwxrwxr-x. 2 ec2-user ec2-user 265 Aug 19 02:44 catalog-ks
drwxrwxr-x. 2 ec2-user ec2-user 265 Aug 19 02:52 multi-ks

snapshots 目錄會列出快照中的 SSTable 檔案。每個快照中也會建立一個 schema.cql 檔案,定義從快照還原時可以使用 CQL 重新建立資料表的架構

$ cd catalog-ks && ls -l

結果為

total 44
-rw-rw-r--. 1 ec2-user ec2-user   31 Aug 19 02:44 manifest.jsonZ
-rw-rw-r--. 4 ec2-user ec2-user   47 Aug 19 02:38 na-1-big-CompressionInfo.db
-rw-rw-r--. 4 ec2-user ec2-user   97 Aug 19 02:38 na-1-big-Data.db
-rw-rw-r--. 4 ec2-user ec2-user   10 Aug 19 02:38 na-1-big-Digest.crc32
-rw-rw-r--. 4 ec2-user ec2-user   16 Aug 19 02:38 na-1-big-Filter.db
-rw-rw-r--. 4 ec2-user ec2-user   16 Aug 19 02:38 na-1-big-Index.db
-rw-rw-r--. 4 ec2-user ec2-user 4687 Aug 19 02:38 na-1-big-Statistics.db
-rw-rw-r--. 4 ec2-user ec2-user   56 Aug 19 02:38 na-1-big-Summary.db
-rw-rw-r--. 4 ec2-user ec2-user   92 Aug 19 02:38 na-1-big-TOC.txt
-rw-rw-r--. 1 ec2-user ec2-user  814 Aug 19 02:44 schema.cql

清除快照

快照可以使用 nodetool clearsnapshot 指令清除或刪除。必須指定特定快照名稱或 –all 選項。

例如,從鍵空間 cqlkeyspace 中刪除名為 magazine 的快照

$ nodetool clearsnapshot -t magazine cqlkeyspace

或使用 –all 選項從 cqlkeyspace 中刪除所有快照

$ nodetool clearsnapshot -all cqlkeyspace

增量備份

在以下各節中,我們將討論如何設定和建立增量備份。

設定增量備份

若要建立增量備份,請在 cassandra.yaml 中將 incremental_backups 設定為 true

incremental_backups: true

這是建立增量備份所需的唯一設定。預設情況下,incremental_backups 設定為 false,因為會為每個資料清除建立一組新的 SSTable 檔案,如果要執行多個 CQL 陳述式,則 backups 目錄可能會快速填滿,並耗盡儲存表格資料所需的儲存空間。增量備份也可以使用 nodetool enablebackup 指令在命令列中啟用。增量備份可以使用 nodetool disablebackup 指令停用。增量備份的狀態(是否已啟用)可以使用 nodetool statusbackup 檢查。

建立增量備份

建立每個表格後,請使用 nodetool flush 指令清除表格資料。將建立增量備份。

$ nodetool flush cqlkeyspace t
$ nodetool flush cqlkeyspace t2
$ nodetool flush catalogkeyspace journal magazine

尋找增量備份

增量備份是在表格目錄中的 Cassandra 的 data 目錄中建立的。可以使用以下指令尋找備份。

$ find -name backups

結果為

./cassandra/data/data/cqlkeyspace/t-d132e240c21711e9bbee19821dcea330/backups
./cassandra/data/data/cqlkeyspace/t2-d993a390c22911e9b1350d927649052c/backups
./cassandra/data/data/catalogkeyspace/journal-296a2d30c22a11e9b1350d927649052c/backups
./cassandra/data/data/catalogkeyspace/magazine-446eae30c22a11e9b1350d927649052c/backups

建立增量備份

本節將更詳細地討論如何使用先前建立的鍵空間和表格建立增量備份。

清除鍵空間和表格

$ nodetool flush cqlkeyspace t

搜尋備份和 backups 目錄將列出備份目錄,即使我們尚未新增任何表格資料。

$ find -name backups

結果為

./cassandra/data/data/cqlkeyspace/t-d132e240c21711e9bbee19821dcea330/backups

檢查 backups 目錄將顯示也沒有備份檔案

$ cd ./cassandra/data/data/cqlkeyspace/t-d132e240c21711e9bbee19821dcea330/backups && ls -l

結果為

total 0

如果將資料列新增至資料中,執行 nodetool flush 指令將清除表格資料,並建立增量備份

$ nodetool flush cqlkeyspace t
$ cd ./cassandra/data/data/cqlkeyspace/t-d132e240c21711e9bbee19821dcea330/backups && ls -l

結果為

total 36
-rw-rw-r--. 2 ec2-user ec2-user   47 Aug 19 00:32 na-1-big-CompressionInfo.db
-rw-rw-r--. 2 ec2-user ec2-user   43 Aug 19 00:32 na-1-big-Data.db
-rw-rw-r--. 2 ec2-user ec2-user   10 Aug 19 00:32 na-1-big-Digest.crc32
-rw-rw-r--. 2 ec2-user ec2-user   16 Aug 19 00:32 na-1-big-Filter.db
-rw-rw-r--. 2 ec2-user ec2-user    8 Aug 19 00:32 na-1-big-Index.db
-rw-rw-r--. 2 ec2-user ec2-user 4673 Aug 19 00:32 na-1-big-Statistics.db
-rw-rw-r--. 2 ec2-user ec2-user   56 Aug 19 00:32 na-1-big-Summary.db
-rw-rw-r--. 2 ec2-user ec2-user   92 Aug 19 00:32 na-1-big-TOC.txt

任何資料表的 backups 目錄,例如 cqlkeyspace/t,都會建立在該資料表的 data 目錄中。

新增另一列資料並執行快取,會產生另一組增量備份檔案。SSTable 檔案會加上時間戳記,用來區分第一個增量備份和第二個增量備份

total 72
-rw-rw-r--. 2 ec2-user ec2-user   47 Aug 19 00:32 na-1-big-CompressionInfo.db
-rw-rw-r--. 2 ec2-user ec2-user   43 Aug 19 00:32 na-1-big-Data.db
-rw-rw-r--. 2 ec2-user ec2-user   10 Aug 19 00:32 na-1-big-Digest.crc32
-rw-rw-r--. 2 ec2-user ec2-user   16 Aug 19 00:32 na-1-big-Filter.db
-rw-rw-r--. 2 ec2-user ec2-user    8 Aug 19 00:32 na-1-big-Index.db
-rw-rw-r--. 2 ec2-user ec2-user 4673 Aug 19 00:32 na-1-big-Statistics.db
-rw-rw-r--. 2 ec2-user ec2-user   56 Aug 19 00:32 na-1-big-Summary.db
-rw-rw-r--. 2 ec2-user ec2-user   92 Aug 19 00:32 na-1-big-TOC.txt
-rw-rw-r--. 2 ec2-user ec2-user   47 Aug 19 00:35 na-2-big-CompressionInfo.db
-rw-rw-r--. 2 ec2-user ec2-user   41 Aug 19 00:35 na-2-big-Data.db
-rw-rw-r--. 2 ec2-user ec2-user   10 Aug 19 00:35 na-2-big-Digest.crc32
-rw-rw-r--. 2 ec2-user ec2-user   16 Aug 19 00:35 na-2-big-Filter.db
-rw-rw-r--. 2 ec2-user ec2-user    8 Aug 19 00:35 na-2-big-Index.db
-rw-rw-r--. 2 ec2-user ec2-user 4673 Aug 19 00:35 na-2-big-Statistics.db
-rw-rw-r--. 2 ec2-user ec2-user   56 Aug 19 00:35 na-2-big-Summary.db
-rw-rw-r--. 2 ec2-user ec2-user   92 Aug 19 00:35 na-2-big-TOC.txt

從增量備份和快照還原

在資料表刪除後,用於還原資料表的兩個主要工具/指令為

  • sstableloader

  • nodetool refresh

快照包含與增量備份相同的 SSTable 檔案組,以及一些額外的檔案。快照包含一個 schema.cql 檔案,用於建立 CQL 中資料表的架構 DDL。資料表備份不包含 DDL,必須在從增量備份還原時從快照取得 DDL。