Created
May 28, 2014 22:27
-
-
Save janoskk/339d76a40b63969ea568 to your computer and use it in GitHub Desktop.
Create (if necessary) and replicate all databases from a couchdb server to another one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Janos Kasza (@janoskk) | |
# | |
# Creates (if necessary) and replicates all databases from a couchdb server to another one | |
# | |
if [ -z "$2" ]; then | |
cat <<EOF | |
Usage: $0 <sourceUrl> <targetUrl> | |
Creates (if necessary) and replicates all databases from a couchdb server to another one. | |
Note that the urls must not contain the trailing '/' and the targetUrl may need to contain | |
authentication for admin. | |
Example: $0 http://example.com:5984 http://admin:admin@localhost:5984 | |
EOF | |
exit 1 | |
fi | |
DBNAMES=`curl -s -X GET $1/_all_dbs | sed 's/\[//;s/\]//;s/"_[a-z]*"//g;s/[,\"]/ /g'` | |
for i in $DBNAMES; do | |
curl -s -X PUT $2/$i | |
curl -s -X POST $2/_replicate -d '{"source":"'$1/$i'", "target":"'$2/$i'"}' -H "Content-Type: application/json" | |
done |
Nice little script. If you want replication to be continuous and restartable (i.e. continue if you restart the couchdb sever) then change line 24 to:
curl -s -X POST $2/_replicator -d '{"_id" : "'$i'", "source":"'$1/$i'", "target":"'$2/$i'", "continuous": true}' -H "Content-Type: application/json"
The _id of the record in the _replicator table will be the table name it is replicating. Tested with CouchDB 1.6.1
This is a great script and the way that I used to replicate a cluster of databases. I needed something a little more robust that supports concurrency and is fault-tolerant, so I created replicate-couchdb-cluster. There is also a docker image that you can use.
it was working well, thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just wanted to say that this script is awesome, but it filters any databases that begin with an underscore. If you want to replicate system databases like
_users
, replace line 20 with:DBNAMES=`curl -s -X GET $1/_all_dbs | sed 's/\[//;s/\]//;s/[,\"]/ /g'`
Note that I don't have any database like
_replicator
to worry about replicating -- I just want_users
(I use CouchDB 2.0). Make sure you want to replicate all of these if you have them before you make this change...