Skip to content

Instantly share code, notes, and snippets.

@ruhnet
Forked from janoskk/couchclone.sh
Last active December 26, 2020 23:10
Show Gist options
  • Save ruhnet/b22f514e38968536d440402c2990ef2f to your computer and use it in GitHub Desktop.
Save ruhnet/b22f514e38968536d440402c2990ef2f to your computer and use it in GitHub Desktop.
Create (if necessary) and replicate all databases from a couchdb server to another one
#!/bin/sh
#
# Janos Kasza (@janoskk)
# Modified by Ruel Tmeizeh @ruhnet
#
# 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
#original (strips [ and " and , and ] from JSON)
#DBNAMES=`curl -s -X GET $1/_all_dbs | sed 's/\[//;s/\]//;s/"_[a-z]*"//g;s/[,\"]/ /g'`
#include _users and such (BEWARE--will also include _replicator!)
#DBNAMES=`curl -s -X GET $1/_all_dbs | sed 's/\[//;s/\]//;s/[,\"]/ /g'`
#include _users and such but NOT _replicator
#DBNAMES=`curl -s -X GET $1/_all_dbs | sed 's/\[//;s/\]//;s/_replicator//g;s/[,\"]/ /g'`
#No _replicator, replace forward slashes in DB names with %2F and + with %2B
#DBNAMES=`curl -s -X GET $1/_all_dbs | sed 's/\[//;s/\]//;s/[,\"]/ /g;s/_replicator//g;s/\//%2F/g;s/\+/%2B/g'`
#No _users/_replicator, replace forward slashes in DB names with %2F and + with %2B
DBNAMES=`curl -s -X GET $1/_all_dbs | sed 's/\[//;s/\]//;s/"_[a-z]*"//g;s/[,\"]/ /g;s/\//%2F/g;s/\+/%2B/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"
#If you want replication to be continuous and restartable (i.e. continue if you restart the couchdb sever):
#The _id of the record in the _replicator table will be the table name it is replicating.
#curl -s -X POST $2/_replicator -d '{"_id" : "'$i'", "source":"'$1/$i'", "target":"'$2/$i'", "continuous": true}' -H "Content-Type: application/json"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment