Database / CouchDB Interview Questions
How do you back up and restore a CouchDB database?
CouchDB does not have a dedicated backup command like mysqldump. The recommended backup approaches depend on your deployment type and RPO requirements:
1. Replication-based backup (recommended for live systems) — replicate the database to a dedicated backup CouchDB instance (local or remote). Because CouchDB replication is idempotent and incremental, subsequent backup runs only copy changed documents. Schedule it via the _replicator database or a cron job calling /_replicate.
# One-shot backup to a backup server
curl -X POST http://admin:pass@localhost:5984/_replicate \
-H "Content-Type: application/json" \
-d '{
"source": "http://localhost:5984/production_db",
"target": "http://backup:5984/production_db_backup_2024_03",
"create_target": true
}'
2. File-system snapshot — stop CouchDB (or freeze I/O via OS-level snapshot), copy the .couch database files from the data directory (/var/lib/couchdb/ on Linux), then restart. Simple but requires downtime or snapshot coordination.
3. couchdb-backup / couchdbdump tools — community tools like couchdb-backup or couchdbdump serialize all documents to a JSON/ndjson file using the _all_docs or _changes endpoint.
# Dump all documents to ndjson using the _all_docs feed
curl "http://admin:pass@localhost:5984/mydb/_all_docs?include_docs=true" \
| python3 -c "import sys,json; [print(json.dumps(r['doc'])) for r in json.load(sys.stdin)['rows'] if not r['id'].startswith('_design')]" \
> mydb_backup_$(date +%F).ndjson
# Restore by posting each line to _bulk_docs
jq -sc '{docs:.}' mydb_backup_2024-03-15.ndjson | \
curl -X POST http://admin:pass@localhost:5984/mydb_restore/_bulk_docs \
-H "Content-Type: application/json" -d @-
For clustered deployments, replicate per-database since there is no single database file to snapshot. Always verify backups by doing a test restore periodically.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
