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:
-
Ensure the Neo4j instance is started.
-
In
cypher-shell
stop the source database you want to copy::USE system STOP DATABASE <sourceDB>;
-
In a different terminal window, enter this as the user, neo4j:
/usr/bin/neo4j-admin copy --from-database=<sourceDB> --to-database=<targetDB>
-
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:
-
Ensure the Neo4j instance is started.
-
In
cypher-shell
, stop the database you want to dump::USE system STOP DATABASE <sourceDB>;
-
Ensure that the directory where you will dump the database exists.
-
As the neo4j users, you can use the
dump
command of theneo4j-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:
-
Use the
load
command of theneo4j-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>
-
In
cypher-shell
create the database::USE system CREATE DATABASE <targetDB>;
Exercise #5: Copying databases
neo4j-admin
to copy a database and create a database from a dump file.
Before you begin
-
Make sure you have a terminal window open to Docker Neo4j instance for this course.
-
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.
-
In
cypher-shell
create a database named movies. -
Exit out of
cypher-shell
. -
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>
-
In
cypher-shell
confirm that the database was loaded. It should contain 171 nodes:MATCH (n) RETURN count(n);
Part 2: You will make a copy of the movies database, movies2 for local use in the Neo4j instance.
-
In
cypher-shell
stop the movies database. -
Exit
cypher-shell
. -
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
-
In
cypher-shell
create movies2. -
Confirm that this movies2 database has 171 nodes.
MATCH (n) RETURN count(n);

Part 3: You will dump the movies database.
-
Use the
dump
command of theneo4j-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.
-
Confirm that the movies.dump file was created.
Part 4: You use the dump file to create a database.
-
Use the
load
command of theneo4j-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
-
In
cypher-shell
create movies3. -
Confirm that this movies3 database has 171 nodes.

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.
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.
Need help? Ask in the Neo4j Community