CouchDB is a fantastic document database. With features such as an append-only file structure and the ability to continuously replicate a database across two instances, CouchDB makes it easy to prevent data loss due to drive failure. But what about data loss due to data corruption on the app-side or due to end-user action? In that case, what you need are database snapshots to store historical data.
Database snapshots are easy as well. Here’s how you can make nightly automated snapshots with CouchDB in GNU/Linux.
Open up crontab in the machine that will be holding the backups:
crontab -e
Insert the following line:
@daily curl 127.0.0.1:5984/_replicate -H "Content-Type: application/json" --data '{"source": "http://admin:remote-admin-password@the.remote.couchs.ip:5984/example-database", "target":"http://admin:local-admin-password@127.0.0.1:5984/example-database-backup-'$(date +%F)'", "continuous": false, "create_target": true}'
and make the following replacements:
remote-admin-password
should be the password of your remote CouchDB’s admin user. If your admin user’s name is different than ‘admin’ change that as well.
the.remote.couchs.ip
should be the IP of the machine with the database that you want to back up.
local-admin-password
should be the password of your local CouchDB’s admin user. If your admin user’s name is different than ‘admin’ change that as well.
example-database
should be replaced in both places with the name of the database that you are backing up.
This code will be run nightly at midnight and will create a new database called something like example-database-backup-2020-05-18.