Droongaチュートリアル: データベースのバックアップと復元

チュートリアルのゴール

データのバックアップと復元を手動で行う際の手順を学ぶこと。

前提条件

Droongaクラスタのデータをバックアップする

drndump のインストール

最初に、Rubygems経由で drndump と名付けられたコマンドラインツールをインストールします:

# gem install drndump

その後、drndump コマンドが正しくインストールできたかどうかを確認します:

# drndump --version
drndump 1.0.0

Droongaクラスタ内のデータをダンプする

drndump コマンドはすべてのスキ−マ定義とデータをJSONs形式で取り出します。既存のDroongaクラスタのすべての内容をダンプ出力してみましょう。

例えば、クラスタが 192.168.0.10192.168.0.11 の2つのノードから構成されていて、別のホスト 192.168.0.12 にログインしている場合、コマンドラインは以下の要領です。

# drndump --host=192.168.0.10 \
           --receiver-host=192.168.0.12
{
  "type": "table_create",
  "dataset": "Default",
  "body": {
    "name": "Location",
    "flags": "TABLE_PAT_KEY",
    "key_type": "WGS84GeoPoint"
  }
}
...
{
  "dataset": "Default",
  "body": {
    "table": "Store",
    "key": "store9",
    "values": {
      "location": "146702531x-266363233",
      "name": "Macy's 6th Floor - Herald Square - New York NY  (W)"
    }
  },
  "type": "add"
}
{
  "type": "column_create",
  "dataset": "Default",
  "body": {
    "table": "Location",
    "name": "store",
    "type": "Store",
    "flags": "COLUMN_INDEX",
    "source": "location"
  }
}
{
  "type": "column_create",
  "dataset": "Default",
  "body": {
    "table": "Term",
    "name": "store_name",
    "type": "Store",
    "flags": "COLUMN_INDEX|WITH_POSITION",
    "source": "name"
  }
}

以下の点に注意して下さい:

実行結果は標準出力に出力されます。 結果をJSONs形式のファイルに保存する場合は、リダイレクトを使って以下のようにして下さい:

# drndump --host=192.168.0.10 \
          --receiver-host=192.168.0.12 \
    > dump.jsons

Droongaクラスタのデータを復元する

droonga-clientのインストール

drndump コマンドの実行結果は、Droonga用のメッセージの一覧です。

Droongaクラスタにそれらのメッセージを送信するには、droonga-send コマンドを使います。 このコマンドを含んでいるGemパッケージ droonga-client をインストールして下さい:

# gem install droonga-client

droonga-send コマンドが正しくインストールされた事を確認しましょう:

# droonga-send --version
droonga-send 0.1.9

空のDroongaクラスタを用意する

2つのノード 192.168.0.10192.168.0.11 からなる空のクラスタがあり、今 192.168.0.12 にログインして操作を行っていて、ダンプファイルが dump.jsons という名前で手元にあると仮定します。

もし順番にこのチュートリアルを読み進めているのであれば、クラスタとダンプファイルが既に手元にあるはずです。以下の操作でクラスタを空にしましょう:

# endpoint="http://192.168.0.10:10041"
# curl "${endpoint}/d/table_remove?name=Location"
[[0,1406610703.2229023,0.0010793209075927734],true]
# curl "${endpoint}/d/table_remove?name=Store"
[[0,1406610708.2757723,0.006396293640136719],true]
# curl "${endpoint}/d/table_remove?name=Term"
[[0,1406610712.379644,0.00006723403930664062],true]

これでクラスタは空になりました。確かめてみましょう:

# endpoint="http://192.168.0.10:10041"
#  curl "${endpoint}/d/table_list"
[[0,1406610804.1535122,0.0002875328063964844],[[["id","UInt32"],["name","ShortText"],["path","ShortText"],["flags","ShortText"],["domain","ShortText"],["range","ShortText"],["default_tokenizer","ShortText"],["normalizer","ShortText"]]]]
# curl "${endpoint}/d/select?table=Store&output_columns=name&limit=10"
[[0,1401363465.610241,0],[[[null],[]]]]

ダンプ結果から空のDroongaクラスタへデータを復元する

drndump の実行結果はダンプ出力元と同じ内容のデータセットを作るために必要な情報をすべて含んでいます。そのため、クラスタが壊れた場合でも、ダンプファイルからクラスタを再構築する事ができます。 やり方は単純で、単にダンプファイルを droonga-send コマンドを使ってからのクラスタに流し込むだけです。

ダンプファイルからクラスタの内容を復元するには、以下のようなコマンドを実行します:

# droonga-send --server=192.168.0.10  \
                    dump.jsons

以下の点に注意して下さい:

これで、データが完全に復元されました。確かめてみましょう:

# curl "${endpoint}/d/select?table=Store&output_columns=name&limit=10"
[[0,1401363556.0294158,0.0000762939453125],[[[40],[["name","ShortText"]],["1st Avenue & 75th St. - New York NY  (W)"],["76th & Second - New York NY  (W)"],["Herald Square- Macy's - New York NY"],["Macy's 5th Floor - Herald Square - New York NY  (W)"],["80th & York - New York NY  (W)"],["Columbus @ 67th - New York NY  (W)"],["45th & Broadway - New York NY  (W)"],["Marriott Marquis - Lobby - New York NY"],["Second @ 81st - New York NY  (W)"],["52nd & Seventh - New York NY  (W)"]]]]

既存のクラスタを別の空のクラスタに直接複製する

複数のDroongaクラスタが存在する場合、片方のクラスタの内容をもう片方のクラスタに複製することができます。 droonga-engine パッケージは droonga-engine-absorb-data というユーティリティコマンドを含んでおり、これを使うと、既存のクラスタから別のクラスタへ直接データをコピーする事ができます。ローカルにダンプファイルを保存する必要がない場合には、この方法がおすすめです。

複数のDroongaクラスタを用意する

ノード 192.168.0.10 を含む複製元クラスタと、ノード 192.168.0.11 を含む複製先クラスタの2つのクラスタがあると仮定します。

もし順番にこのチュートリアルを読み進めているのであれば、2つのノードを含むクラスタが手元にあるはずです。droonga-engine-catalog-modify を使って2つのクラスタを作り、1つを空にしましょう。手順は以下の通りです:

(on 192.168.0.10)
# host=192.168.0.10
# droonga-engine-catalog-modify --source=~/droonga/catalog.json \
                                --update \
                                --replica-hosts=$host

(on 192.168.0.11)
# host=192.168.0.11
# droonga-engine-catalog-modify --source=~/droonga/catalog.json \
                                --update \
                                --replica-hosts=$host
# endpoint="http://$host:10041"
# curl "${endpoint}/d/table_remove?name=Location"
# curl "${endpoint}/d/table_remove?name=Store"
# curl "${endpoint}/d/table_remove?name=Term"

これで、ノード 192.168.0.10 を含む複製元クラスタと、ノード 192.168.0.11 を含む複製先の空のクラスタの、2つのクラスタができました。確かめてみましょう:

# curl "http://192.168.0.10:10041/droonga/system/status"
{
  "nodes": {
    "192.168.0.10:10031/droonga": {
      "live": true
    }
  }
}
# curl "http://192.168.0.10:10041/d/select?table=Store&output_columns=name&limit=10"
[[0,1401363556.0294158,0.0000762939453125],[[[40],[["name","ShortText"]],["1st Avenue & 75th St. - New York NY  (W)"],["76th & Second - New York NY  (W)"],["Herald Square- Macy's - New York NY"],["Macy's 5th Floor - Herald Square - New York NY  (W)"],["80th & York - New York NY  (W)"],["Columbus @ 67th - New York NY  (W)"],["45th & Broadway - New York NY  (W)"],["Marriott Marquis - Lobby - New York NY"],["Second @ 81st - New York NY  (W)"],["52nd & Seventh - New York NY  (W)"]]]]
# curl "http://192.168.0.11:10041/droonga/system/status"
{
  "nodes": {
    "192.168.0.11:10031/droonga": {
      "live": true
    }
  }
}
# curl "http://192.168.0.11:10041/d/select?table=Store&output_columns=name&limit=10"
[[0,1401363465.610241,0],[[[null],[]]]]

2つのDroongaクラスタの間でデータを複製する

2つのクラスタの間でデータをコピーするには、いずれかのノード上で以下のように droonga-engine-absorb-data コマンドを実行します:

(on 192.168.0.10 or 192.168.0.11)
# droonga-engine-absorb-data --source-host=192.168.0.10 \
                             --destination-host=192.168.0.11
Start to absorb data from 192.168.0.10
                       to 192.168.0.11
  dataset = Default
  port    = 10031
  tag     = droonga

Absorbing...
...
Done.

以上の操作で、2つのクラスタの内容が完全に同期されました。確かめてみましょう:

# curl "http://192.168.0.10:10041/d/select?table=Store&output_columns=name&limit=10"
[[0,1401363556.0294158,0.0000762939453125],[[[40],[["name","ShortText"]],["1st Avenue & 75th St. - New York NY  (W)"],["76th & Second - New York NY  (W)"],["Herald Square- Macy's - New York NY"],["Macy's 5th Floor - Herald Square - New York NY  (W)"],["80th & York - New York NY  (W)"],["Columbus @ 67th - New York NY  (W)"],["45th & Broadway - New York NY  (W)"],["Marriott Marquis - Lobby - New York NY"],["Second @ 81st - New York NY  (W)"],["52nd & Seventh - New York NY  (W)"]]]]
# curl "http://192.168.0.11:10041/d/select?table=Store&output_columns=name&limit=10"
[[0,1401363556.0294158,0.0000762939453125],[[[40],[["name","ShortText"]],["1st Avenue & 75th St. - New York NY  (W)"],["76th & Second - New York NY  (W)"],["Herald Square- Macy's - New York NY"],["Macy's 5th Floor - Herald Square - New York NY  (W)"],["80th & York - New York NY  (W)"],["Columbus @ 67th - New York NY  (W)"],["45th & Broadway - New York NY  (W)"],["Marriott Marquis - Lobby - New York NY"],["Second @ 81st - New York NY  (W)"],["52nd & Seventh - New York NY  (W)"]]]]

2つのDroongaクラスタを結合する

これらの2つのクラスタを結合するために、以下のコマンド列を実行しましょう:

(on 192.168.0.10)
# droonga-engine-catalog-modify --source=~/droonga/catalog.json \
                                --update \
                                --add-replica-hosts=192.168.0.11

(on 192.168.0.11)
# droonga-engine-catalog-modify --source=~/droonga/catalog.json \
                                --update \
                                --add-replica-hosts=192.168.0.10

これで、1つだけクラスタがある状態になりました。最初の状態に戻ったという事になります。

# curl "http://192.168.0.10:10041/droonga/system/status"
{
  "nodes": {
    "192.168.0.10:10031/droonga": {
      "live": true
    },
    "192.168.0.11:10031/droonga": {
      "live": true
    }
  }
}

まとめ

このチュートリアルでは、Droongaクラスタのバックアップとデータの復元の方法を実践しました。 また、既存のDroongaクラスタの内容を別の空のクラスタへ複製する方法も実践しました。

続いて、既存のDroongaクラスタに新しいreplicaを追加する手順を学びましょう。