Created
July 18, 2016 20:03
-
-
Save alwalker/444748efcd74f2b0cd9c9998d221ae77 to your computer and use it in GitHub Desktop.
Keep your CouchDB databases synchronized between two servers
This file contains 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/bash | |
#CouchDB replication sync script. | |
#Run on target CouchDB server and supply IP or DNS name of source. | |
#For example running 'bash couch_replicator.sh tcdb01' on tcdb02 | |
#will keep tcdb02 in sync with all databases on tcdb01. | |
if [ "$#" -ne 1 ]; then | |
echo "Wrong number of arguments!" | |
echo "USAGE: $0 source_database" | |
fi | |
#sourceDB="http://admin:[password]@${1}:5984" | |
sourceDB="http://${1}:5984" | |
targetDB="http://admin:[password]@localhost:5984" | |
#get sources from all replication active tasks | |
replications=`curl -s ${targetDB}/_active_tasks | jq -r '[select(.[].type == "replication") | .[].source]'` | |
#go thru each target database and make sure there is a matching active task | |
for dbname in `curl -s ${sourceDB}/_all_dbs | jq -r '.[] | select(. | startswith("_") | not)'`; do | |
found=`echo ${replications} | jq -r "select(.[] | contains(\"${dbname}\"))"` | |
if [ -n "$found" ]; then | |
echo "Found active replication task for ${dbname}" | |
continue | |
else | |
echo "No active replication task for ${dbname}, starting continuous replication" | |
#call replicate API | |
curl -vX POST ${targetDB}/_replicate \ | |
-d "{\"source\":\"${sourceDB}/${dbname}\",\"target\":\"${dbname}\", \"continuous\": true, \"create_target\": true, \"user_ctx\": {\"roles\": [\"_admin\"]}}" \ | |
-H "Content-Type: application/json" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment