Export whole database in Cypher format (ASCII text)?

Phil picture Phil · Sep 16, 2013 · Viewed 15.7k times · Source

Is there a way to export an entire Neo4J database in Cypher, resulting in an ASCII file of the Cypher commands which could be used on an empty Neo4J database to re-create the original database? Since Neo4J is undergoing such rapid development, I worry about using the built-in backup functionality (of the enterprise version).

For example, with Oracle, you can export the whole database in SQL*PLUS DML/DDL commands, which can be useful.

Answer

jjaderberg picture jjaderberg · Jan 22, 2014

As of Neo4j 2.0 there is a dump command in neo4j-shell that does this. You can dump the results of a specific query or the entire database. By passing the dump command as an argument when starting neo4j-shell you can redirect the output to file to make a 'cypher create script' or to another neo4j-shell session recreating all or parts of the graph in a different database.

Flatten and dump the results of a query

neo4j-sh$ dump MATCH (n:StackOverflow:Question {questionId:18830686})<-[:ANSWERS]-(answer)<-[:WRITES]-(user) RETURN *;

Dump entire database to a file

usr@term: bin/neo4j-sh -c dump > ./backup/$(date +"%y%m%d_%H%M%S").cypher

Pipe the dump to another shell session & database

usr@term: db1/bin/neo4j-sh -path db1/data/graph.db/ -c dump | db2/bin/neo4j-shell -path db2/data/graph.db/

Caveat emptor

There were some issues with doubles and floats being exported in scientific notation, which neo4j-shell couldn't interpret again on import (SO, github) and there were some problems with escaping "quoted strings" (github). I think these are both resolved, so if you have trouble you may want to check out a recent build.

And finally there is one problem that I don't think is resolved yet. Recently schema was included in the dump so create index and create constraint statements are exported as well. However, all the exported statements are framed in one and the same transaction in the output and neo4j won't let you create schema and data in the same transaction. So if you pipe the dump directly into another shell session to recreate the graph, you're likely to run into

> ;
ConstraintViolationException: Cannot perform data updates in a transaction that has performed schema updates.
neo4j-sh (?)$ commit
Failed to commit, transaction rolled back

It's easy to work around this by redirecting to a file and manually adding commit and begin after the last schema statement. Be sure to put them each on a new line, it should look something like

...
create index on :`Person`(`name`)
commit
begin
create (_0:`Person` {`name`:"Paul"})
...

Or you can edit the output from neo4j-shell on the fly and add it there, for instance if you dump programmatically and don't want to edit manually. On osx I have used sed like so

db1/bin/neo4j-shell -path db1/data/graph.db/ -c dump | sed -E 's/create (index|constraint) on .*/&\'$'\ncommit''\'$'\nbegin/' | db2/bin/neo4j-shell -path db2/data/graph.db/

This adds a commit after each schema statement, which is more than what is necessary (could commit all schema statements together), but hey, it works.