教程:在单实例中备份和还原单个数据库

本教程详细说明了如何将一个数据库(本例中为 3.5 版本)备份并还原到正在运行的(已升级/迁移的)4.x 单机实例中。

以下示例假设您的数据库关联了用户和角色,并描述了如何对其进行备份、迁移,然后在正在运行的单机实例上进行还原。

准备备份数据库

在执行备份之前,最好记录一下您想要还原的数据库的数据和元数据。稍后您可以使用这些信息来验证还原是否成功,并重新创建数据库用户和角色。在本例中,该数据库使用了 Neo4j Browser → Favorites → Example Graphs 中的“电影图谱”(Movie Graph)数据集。

  1. 在运行该数据库的 3.5 Neo4j 实例中,导航至 /bin 文件夹,并使用您的凭据登录 Cypher® Shell 命令行控制台。有关 Cypher Shell 命令行界面 (CLI) 及其使用方法的更多信息,请参阅 Operations Manual → Cypher Shell

    ./cypher-shell -u neo4j -p <password>
    Connected to Neo4j at neo4j://:7687 as user neo4j.
    Type :help for a list of available commands or :exit to exit the shell.
    Note that Cypher queries must end with a semicolon.
  2. 运行查询以统计数据库中的节点数量。

    MATCH (n) RETURN count(n) AS countNode;
    +-----------+
    | countNode |
    +-----------+
    | 171       |
    +-----------+
    
    1 row available after 22 ms, consumed after another 1 ms
  3. 运行查询以统计关系数量。

    MATCH (n)-[r]->() RETURN count(r) AS countRelationships;
    +--------------------+
    | countRelationships |
    +--------------------+
    | 253                |
    +--------------------+
    
    1 row available after 29 ms, consumed after another 0 ms
  4. 运行以下两个查询以查看是否有定义的模式(schema)。

    CALL db.constraints()
    0 rows available after 2 ms, consumed after another 0 ms

    结果显示未定义任何约束。

    CALL db.indexes;
    +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | description              | indexName | tokenNames | properties | state    | type                  | progress | provider                              | id | failureMessage |
    +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | "INDEX ON :Movie(title)" | "index_1" | ["Movie"]  | ["title"]  | "ONLINE" | "node_label_property" | 100.0    | {version: "1.0", key: "native-btree"} | 1  | ""             |
    +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

    结果显示在 :Movie 节点的 title 属性上定义了一个索引。

  5. 运行查询以列出与此数据库关联的所有用户及其角色。

    CALL dbms.security.listUsers;
    +-----------------------------------------+
    | username | roles                | flags |
    +-----------------------------------------+
    | "user1"  | ["editor", "reader"] | []    |
    | "neo4j"  | ["admin"]            | []    |
    +-----------------------------------------+
    
    2 rows available after 2 ms, consumed after another 0 ms

    结果显示有两个用户 - 默认的 neo4j 用户(拥有 admin 权限)和一个自定义用户 user1(拥有内置角色 editorreader 的组合权限)。

  6. 退出 Cypher Shell 命令行控制台。

    :exit;
    
    Bye!

备份数据库

现在您可以备份数据库了。

导航至 /bin 文件夹,并运行以下命令将数据库备份到您的目标文件夹。如果您想要放置备份的文件夹不存在,则需要先创建它。在本例中,文件夹名称为 /tmp/3.5.24

./neo4j-admin backup --backup-dir=/tmp/3.5.24 --name=graphdbbackup

有关执行备份及不同命令选项的详细信息,请参阅 Operations Manual 3.5 → Perform a backup

在 4.x 单机实例上还原数据库备份

您有一个正在运行的 Neo4j 4.x 实例,想要将备份的数据库还原到其中。

  1. 在 4.x 单机实例的 neo4j.conf 文件中,设置 dbms.allow_upgrade=true

    如果您的 Neo4j 单机实例版本早于 4.1,则必须重启它以使配置生效。

  2. 导航至 /bin 文件夹并运行以下命令以还原数据库备份。

    ./neo4j-admin restore --from=/tmp/3.5.24/graphdbbackup --database=graphdbbackup
  3. 运行以下命令以验证 graphdbbackup 数据库是否存在

    ls -al ../data/databases
    total 0
    drwxr-xr-x@  6 username  staff   192  4 Dec 14:15 .
    drwxr-xr-x@  5 username  staff   160  7 Dec 09:35 ..
    drwxr-xr-x  42 username  staff  1344  4 Dec 14:15 graphdbbackup
    drwxr-xr-x  37 username  staff  1184  4 Dec 14:06 neo4j
    -rw-r--r--   1 username  staff     0  4 Dec 14:06 store_lock
    drwxr-xr-x  38 username  staff  1216  4 Dec 14:06 system

    但是,还原数据库并不会自动创建它。因此,如果您在 Cypher Shell 或 Neo4j Browser 中执行 SHOW DATABASES,它将不可见。

  4. 登录 Cypher Shell 命令行控制台。

  5. 将活动数据库更改为 system (:USE system;),并创建 graphdbbackup 数据库。

    CREATE DATABASE graphdbbackup;
    0 rows available after 145 ms, consumed after another 0 ms
  6. 验证 graphdbbackup 数据库是否处于联机(online)状态。

    SHOW DATABASES;
    +-------------------------------------------------------------------------------------------------------+
    | name            | address          | role         | requestedStatus | currentStatus | error | default |
    +-------------------------------------------------------------------------------------------------------+
    | "graphdbbackup" | "localhost:7687" | "standalone" | "online"        | "online"      | ""    | FALSE   |
    | "neo4j"         | "localhost:7687" | "standalone" | "online"        | "online"      | ""    | TRUE    |
    | "system"        | "localhost:7687" | "standalone" | "online"        | "online"      | ""    | FALSE   |
    +-------------------------------------------------------------------------------------------------------+
    
    3 rows available after 175 ms, consumed after another 9 ms
  7. 将活动数据库更改为 graphdbbackup,并重复“准备备份数据库”一节中的第 2、3 和 4 步,以验证所有数据是否已成功还原。

重新创建数据库用户和角色

您需要使用“准备备份数据库”一节第 5 步中的记录,并参考 Cypher Manual → Cypher administration commands,手动重新创建已还原数据库的所有用户和角色。

示例 1. 针对 system 数据库运行以下命令,以重新创建 graphdbbackup 数据库的自定义用户和角色。
  1. 创建用户 user1

    CREATE USER user1 IF NOT EXISTS
    SET PASSWORD 'password'
    SET STATUS ACTIVE;
  2. 授予用户 user1 reader 角色。

    GRANT ROLE reader TO user1;
  3. 授予用户 user1 editor 角色。

    GRANT ROLE editor TO user1;
  4. 验证用户 user1 是否拥有与数据库备份中相同的角色。

    SHOW USERS;
    +---------------------------------------------------------------------+
    | user    | roles                | passwordChangeRequired | suspended |
    +---------------------------------------------------------------------+
    | "neo4j" | ["admin"]            | FALSE                  | FALSE     |
    | "user1" | ["editor", "reader"] | TRUE                   | FALSE     |
    +---------------------------------------------------------------------+

© . This site is unofficial and not affiliated with Neo4j, Inc.