Export & Import a Database with CouchDB (Updated)

In order to Export and Import a CouchDB database, following the simple guide from this post: “Export & Import a Database with CouchDB”, we need to perform 3 steps: Export, Modify and Import operations.
But what I found as addition is, that changing the records mentioned bellow in step 2 for a bigger or just production databases, requires some additional work. So for fun I created simple Node.js script that can do that transformation — see bellow in step 2.
When is this useful? Well this is quite useful in case both instances of CouchDB databases do not have direct internet connection. If they do, just use the Clone or Replication functionality of CouchDB — a way easier. Or in cases you want to perform partial export and import of a certain database.
1. Export
So following the above mentioned post, a simple way of exporting a CouchDB database to a file, will be by running the following Curl command in the terminal window:
curl -X GET http://127.0.0.1:5984/[mydatabase]/_all_docs?include_docs=true > /Users/[username]/Desktop/db.json
2. Modify
Next step is to modify the exported json file to look like something like the json below (note the _id):
{
"docs": [
{"_id": "0", "integer": 0, "string": "0"},
{"_id": "1", "integer": 1, "string": "1"},
{"_id": "2", "integer": 2, "string": "2"}
]
}
Main bit you need to look at, is adding the documents in the “docs” code block. So if you try to perform this manually is not an easy job.
And here is the Node.js JavaScript code I created to perform the transformation mentioned above:
var fs = require(“fs”);
console.log(“\n *START transformation* \n”);var content = fs.readFileSync(”db.json”);
var json = JSON.parse(content);
var docs = json.rows;
var newDocs = new Array();docs.forEach(function(doc) {
var innerdoc = doc.doc;
delete innerdoc._rev;
newDocs.push(innerdoc);
});var newJson = new Object();
newJson.docs = newDocs;
var newContent = JSON.stringify(newJson)
fs.writeFile(’db_u.json’, newContent, ‘utf8’, function(err) {
if (err) throw err;
console.log(‘complete’);
});
console.log(“\n *DONE transformation!* \n”);
So you can save this code to “transformdocs.js” file in “/Users/[username]/Desktop/” folder. And execute it from the same folder with Node.js like this:
node transformdocs.js
This will create file in the same folder with name “db_u.json”, which can be used in step 3 bellow to import the DB.
- Note: Depends on your editor and OS you may need to replace the quotation marks “” in the code above.
3. Import
Once this is done you can run the following Curl command to import the data to a CouchDB database:
curl -d @db_u.json -H “Content-type: application/json” -X POST http://127.0.0.1:5984/[mydatabase]/_bulk_docs
Clone
Off course if both databases have access to each other, we can use replicate functionality. How to use replication functionality check in the original post: “Export & Import a Database with CouchDB”….
Another place to read about replication is here.
Copy
Another maybe the easiest way to export/import and backup/restore a CouchDB database is to simply copy the raw database file from one instance of CouchDB to the other instance. Usually, this file is located at /var/lib/couchdb/database-name.couch
. You can safely copy this file, even while the database is running. (Source)
*Note: using Desktop(/Users/[username]/Desktop/”) folder for development is not a good or recommended practice, but I used it for my example to follow the guide from the post mentioned above.