npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

couchreplicate

v1.1.0

Published

CouchDB/Cloudant database replication command-line tool

Downloads

85

Readme

couchreplicate

Build Status npm version

This is a command-line tool and library that helps coordinate Apache CouchDB™ or IBM Cloudant replications. It can be used to replicate a single database, multiple databases or an entire cluster from a source instance to a target instance.

It is written in Node.js and can be installed from npm or used programmatically in your own Node.js projects.

screenshot

Pre-requisites

  • Node.js & npm
  • one or more source databases in an Apache CouchDB or IBM Cloudant instance
  • a target Apache CouchDB or IBM Cloudant instance

Installation

Install with npm

npm install -g couchreplicate

Supplying -g instructs npm to install the library globally. You can find out where global npm modules are installed by running npm config list. You can find out which user is used to install global modules by running npm -g config get user. I had to repair the permissions of pre-existing libraries by running chown -R nobody /usr/local/lib/node_modules/*.

Usage

Replicating all databases from one instance to another

couchreplicate -a -s http://u:p@localhost:5984 -t https://U:[email protected]

or

couchreplicate --all --source http://u:p@localhost:5984 --target https://U:[email protected]

where http://u:p@localhost:5984 is the URL of your source instance and https://U:[email protected] is the URL of your target instance. They must include admin credentials.

Replicating a list of databases

couchreplicate -d db1,db2,db3 -s http://u:p@localhost:5984 -t https://U:[email protected]

where db1,db2,db3 is a comma-separated list of database names.

Replicating a single database

couchreplicate -d mydb -s http://u:p@localhost:5984 -t https://U:[email protected]

or

couchreplicate -s http://u:p@localhost:5984/mydb -t https://U:[email protected]/mydb

where mydb is the name of the database. When supplying the source and target database names in the URL, the database names need not match.

Additionally replicating the _security document

Normal CouchDB replication leaves the _security document behind. The _security document is used to determine which users have which levels of access to the database. Without a _security document specified, only an _admin user can read/write/update/delete documents.

If you pass the --auth or -x command-line parameter, then couchreplicate will copy the _security document from the source to the target database at the end of the replication process e.g:

couchreplicate -s http://u:p@localhost:5984/mydb -t https://U:[email protected]/mydb --auth

Generating continuous replications

The default action is to create one-shot replications that copy data from the source to the target and then finish. If you would like live, continuous replications instead, then add the --live or -l option to your command-line invocation.

e.g.

couchreplicate -l -a -s http://u:p@localhost:5984 -t https://U:[email protected]

Some things to note:

  • this tool will only permit you to create fifty live replications
  • as the replications are continuous, they will never reach a "completed" state so the tool will remain active forever, or until you kill the process (with Ctrl-C). After killing the process, any running replications will continue to run. See the source cluster's "replication" tab in the dashboard.

Errors during replication

Replication errors can occur and have a multitude of causes. If a replication does not complete successfully, you may see a status like this:

 cities [▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇——] 97% 21.1s error
  • the document count may not reach 100%
  • the status string at right-hand side will read 'error'

It may be caused by:

  • incorrect authentication credentials at the source or target side
  • insufficient privileges - make sure you have "admin" credentials for the source and target clusters
  • the target service may have a size restriction on documents. Cloudant instances on the IBM Cloud have a limit of 1MB per API request, so very large JSON documents would not make it
  • the target service may have a API rate limit in place. If the target is very busy, then there may be insufficient capacity left over to service the replication process

The _replicator database

The couchreplicate tool requires a database called _replicator to be present on the source cluster. The tool tries to create a the database if it doesn't already exist at the start of replication job. You can see the current and historical replication jobs in the Cloudant/CouchDB dashboard or by looking at the contents of the _replicator database yourself.

Note that even if you Ctrl-C out of couchreplicate, any running replications will continue to proceed.

Debugging

If you need to monitor replications more closely than watching the progress bars, then setting the DEBUG environment variable to couchreplicate before running the tool will send more status information to the console.

e.g.

DEBUG=couchreplicate couchreplicate -d mydb -s http://u:p@localhost:5984 -t https://U:[email protected]

Command-line reference

  • --source / -s - source URL (required)
  • --target / -t - target URL (required)
  • --databases / -d - comma-separated list of database names
  • --all / -a - replicate all databases
  • --concurrency / -c - the number of replications to run at once (default = 1)
  • --auth / -x - also copy the _security document during replication
  • --live / -l - set up continuous replications instead of the default "one-shot" replication
  • --quiet / -q - suppress progress bars
  • --nomonitor / -n - don't monitor the replications - just start the replication processed and exit. Only works with --live/-l
  • --help / -h - show help message
  • --version / -v - show version number

Command-line exit codes

  • 0 - success
  • 1 - invalid source URL
  • 2 - invalid target URL
  • 3 - --nomonitor/-n is only applicable with the --live/-l option
  • 4 - no source or target database names supplied
  • 5 - database names supplied in URLs and as other command-line options
  • 6 - runtime error

Using couchreplicate programmatically

Install the library into your own Node.js project

npm install --save couchreplicate

Load the module into your code

  const cm = require('couchreplicate')

Set off a single replication:

  var opts = {
    source: 'http://u:p@localhost:5984/sourcedb',
    target: 'https://U:[email protected]/targetdb',
    quiet: true,
    auth: true 
  }
  cm.migrateDB(opts).then(() => {
    console.log('done')
  })

multiple replications:

  var opts = {
    source: 'http://u:p@localhost:5984',
    target: 'https://U:[email protected]',
    databases: ['animals', 'minerals', 'vegetables'],
    quiet: false,
    concurrency: 3
    auth: true 
  }
  
  cm.migrateList(opts).then(() => {
    console.log('done')
  })

or replicate an entire cluster:

  var opts = {
    source: 'http://u:p@localhost:5984',
    target: 'https://U:[email protected]',
    quiet: false,
    concurrency: 3
    auth: true 
  }
  
  cm.migrateAll(opts).then(() => {
    console.log('done')
  })