Online Course Basic Neo4j 4.0 Administration Introduction to Neo4j Overview of Neo4j Administration Managing a Neo4j Instance Using cypher-shell to Manage Databases Copying Databases Changing the Database Location Checking Database Consistency Scripting to Manage Databases Configuring Plugins Managing HTTP Ports… Read more →

Copying Databases

About this module

Next, you will learn the ways that you can copy a database.

At the end of this module, you should be able to:

  • Copy a Neo4j database using the copy command.
  • Create a dump file for a database.
  • Load a dump file to create a database.

Copying a Neo4j database

You or one of your application developers may want to copy a Neo4j database. Perhaps the developers are at a juncture in their development where they want to save what they have worked on thus far. Or perhaps they want to give a copy of the database to another developer to work with on a different system.

The structure of a Neo4j database is proprietary and could change from one release to another. You should never copy the database files from one location in the filesystem/network to another location.

There are two ways that you can copy a database:

  • Create a copy within the same Neo4j instance.
  • Create a copy (dump file) that you will give to another developer to load into their Neo4j instance running on a different system.

Before you copy a database it must be stopped.

Later in this training, you will learn how to back up a database while it is online.

Copying a database within the same Neo4j instance

To copy a database within the same Neo4j instance, the source database must be stopped. Additionally, the target database that you will be creating must not exist.

You use the neo4j-admin tool to copy a database within your Neo4j instance. Here are the steps:

  1. Ensure the Neo4j instance is started.
  2. In cypher-shell stop the source database you want to copy:
    :USE system
    STOP DATABASE <sourceDB>;
  3. In a different terminal window, enter this as the user, neo4j:
    /usr/bin/neo4j-admin copy --from-database=<sourceDB> --to-database=<targetDB>
  4. In cypher-shell again:
CREATE DATABASE <targetDB>;

There are other options you can use to customize what is copied. See the Neo4j Operations Manual for details.

Creating a dump file from a database

To create a dump file of a database, you must:

  1. Ensure the Neo4j instance is started.
  2. In cypher-shell, stop the database you want to dump:
    :USE system
    STOP DATABASE <sourceDB>;
  3. Ensure that the directory where you will dump the database exists.
  4. As the neo4j users, you can use the dump command of the neo4j-admin tool to create the dump file as follows:
/usr/bin/neo4j-admin dump --database=<sourceDB> --to=<absolute path to dump file>

Note that the user, neo4j, must have write access to the directory.

This will create a dump file that can now be transferred between systems to create a new database on a different system.

Creating a database from a dump file

Then, if you want to create a database from any dump file (offline backup) to use for a Neo4j instance, you must:

  1. Use the load command of the neo4j-admin tool as the neo4j user, to create the database from the dump file as follows:
    /usr/bin/neo4j-admin load --database=<targetDB> --from=<absolute path to dump file>
  2. In cypher-shell create the database:
    :USE system
    CREATE DATABASE <targetDB>;

Exercise #5: Copying databases

In this exercise, you will use neo4j-admin to copy a database and create a database from a dump file.

Before you begin

  1. Make sure you have a terminal window open to Docker Neo4j instance for this course.
  2. Ensure that the Docker Neo4j instance is started.

Exercise steps:

Part 1: First you will create a movies database that will be used in this exercise for copying.

  1. In cypher-shell create a database named movies.
  2. Exit out of cypher-shell.
  3. Invoke cypher-shell using movies.cypher as input and specify movies as the database.

On OS X or Linux:

cat ~/docker-neo4j/files/movies.cypher | docker exec --interactive neo4j bin/cypher-shell --database movies -u neo4j -p <passwordYouSpecified>

On Windows:

type files\movies.cypher | docker exec --interactive neo4j bin/cypher-shell --database movies -u neo4j -p <passwordYouSpecified>
  1. In cypher-shell confirm that the database was loaded. It should contain 171 nodes:
MATCH (n) RETURN count(n);

ConfirmMovieLoadDocker

Part 2: You will make a copy of the movies database, movies2 for local use in the Neo4j instance.

  1. In cypher-shell stop the movies database.

    ConfirmMovieStoppedDocker

  2. Exit cypher-shell.
  3. Copy the movies database to the movies2 database using the neo4j-admin tool:
[sudo] docker exec --interactive neo4j bin/neo4j-admin copy --from-database=movies --to-database=movies2

neo4j-adminCopyDocker

  1. In cypher-shell create movies2.
  2. Confirm that this movies2 database has 171 nodes.
MATCH (n) RETURN count(n);

Verify-neo4j-adminCopyDocker

Part 3: You will dump the movies database.

  1. Use the dump command of the neo4j-admin tool to create the dump file as follows:
[sudo] docker exec --interactive neo4j bin/neo4j-admin dump --database=movies --to=data/movies.dump

This writes the movies.dump file to the $HOME/docker-neo4j/data directory which is available to the Docker Neo4j instance.

  1. Confirm that the movies.dump file was created.

Part 4: You use the dump file to create a database.

  1. Use the load command of the neo4j-admin tool to create the database, movies3 from the dump file as follows:
    [sudo] docker exec --interactive neo4j bin/neo4j-admin load --database=movies3 --from=data/movies.dump
  2. In cypher-shell create movies3.
  3. Confirm that this movies3 database has 171 nodes.

Verify-neo4j-adminLoadDocker

Exercise summary

You have now gained experience copying a database within the Neo4j instance and also creating a dump file that can be used to create a database on a different system.

Check your understanding

Question 1

You want to make a copy of an existing database that is online in your Neo4j instance. What must you do before you copy it?

Select the correct answer.

  • Export the schema of the database.
  • Export the nodes and relationships in the database.
  • Lock the database.
  • Stop the database.

Question 2

What tools do you use the create a copy of a database within the same Neo4j instance?

Select the correct answers.

  • cypher-shell
  • neo4j copy
  • neo4j-admin copy
  • neo4j-admin dump

Question 3

What tools do you use the create a copy of a database that will be used on a different system?

Select the correct answers.

  • cypher-shell
  • neo4j copy
  • neo4j-admin dump
  • neo4j-admin load

Summary

You should now be able to:

  • Copy a Neo4j database using the copy command.
  • Create a dump file for a database.
  • Load a dump file to create a database.

Stay Connected

Sign up to find out more about Neo4j's upcoming events & meetups.