A robust Go service for replicating Apache CouchDB databases from a source to a target database on a configurable time interval. Unlike CouchDB's native replication which operates either continuously or on-demand, this utility bridges the gap by providing scheduled, interval-based replication - perfect for automated backup strategies.
CouchDB natively supports two replication modes:
- Continuous: Always replicating, consuming resources
- Ad-hoc: One-time manual replication
This service solves the need for periodic backups by executing replication jobs on a fixed schedule. It's ideal for maintaining synchronized backup servers of production CouchDB instances.
- Scheduled Replication: Execute replication jobs at configurable time intervals
- Multiple Databases: Replicate multiple CouchDB databases in a single configuration
- Selective Replication: Filter documents by ID selector for targeted backups
- HTTP API: Trigger manual replication via REST endpoint
- Health Checks: Automatic verification of source and target database connectivity
- Structured Logging: Detailed logging using Uber's Zap logger with configurable log levels
- Docker Support: Pre-built Docker images for easy deployment
- Cross-platform Builds: Compiled binaries for Linux AMD64 and ARM architectures
The service consists of three main components:
- HTTP Server (default port 7012): Exposes REST endpoints for manual replication triggers
- Replicator Engine: Manages scheduled replication jobs based on configured intervals
- Database Connections: Maintains connections to source and target CouchDB instances
The replicator continuously:
- Verifies database connectivity at startup
- Executes replication jobs on the configured time interval
- Logs replication status and any errors
- Exposes an endpoint for manual replication triggers
- Go 1.19 or higher
- Apache CouchDB 2.0+ (source and target instances)
- Network connectivity between replicator and both CouchDB instances
# Clone the repository
git clone https://github.com/byuoitav/db-replicator.git
cd db-replicator
# Build binaries
make build
# Binaries will be created in dist/Pull pre-built images from GitHub Package Registry:
docker pull docker.pkg.github.com/byuoitav/db-replicator/db-replicator:latestThe service requires a JSON configuration file specifying source and target databases along with replication jobs.
| Field | Type | Description |
|---|---|---|
source |
object | Source CouchDB connection credentials |
target |
object | Target CouchDB connection credentials |
jobs |
array | Array of replication job definitions |
time_interval |
integer | Interval between replications (in minutes) |
| Field | Type | Required | Description |
|---|---|---|---|
address |
string | Yes | CouchDB server URL (e.g., http://localhost:5984) |
username |
string | Yes | CouchDB username |
password |
string | Yes | CouchDB password |
| Field | Type | Required | Description |
|---|---|---|---|
database |
string | Yes | Name of the database to replicate |
continuous |
boolean | Yes | Whether to use continuous replication (typically false) |
id_selector |
string | No | Filter documents by ID prefix (empty string = replicate all) |
{
"source": {
"address": "http://localhost:42069",
"username": "admin",
"password": "secure_password"
},
"target": {
"address": "http://localhost:42068",
"username": "admin",
"password": "secure_password"
},
"jobs": [
{
"database": "devices",
"continuous": false,
"id_selector": "ABC-123"
},
{
"database": "rooms",
"continuous": false,
"id_selector": ""
}
],
"time_interval": 120
}# Run with default settings
./dist/db-replicator-linux-amd64 -c config.json
# With custom options
./dist/db-replicator-linux-amd64 \
-c config.json \
-p 7012 \
-l Debug| Flag | Short | Default | Description |
|---|---|---|---|
--config |
-c |
(required) | Path to configuration JSON file |
--port |
-p |
7012 |
HTTP server port |
--log |
-l |
Info |
Log level (Debug, Info, Warn, Error) |
# Using Docker with volume mount
docker run -d \
-v /path/to/config.json:/app/config.json \
-p 7012:7012 \
--name db-replicator \
docker.pkg.github.com/byuoitav/db-replicator/db-replicator:latest \
-c /app/config.jsonTrigger an immediate replication cycle without waiting for the scheduled interval.
Request:
GET /replication/start
Response:
"replication started"Example:
curl http://localhost:7012/replication/start.
├── cmd/
│ ├── main.go # Application entry point
│ ├── http.go # HTTP server setup
│ ├── deps.go # Dependency injection
│ └── logger.go # Logging configuration
├── replication/
│ ├── replicator.go # Core replication logic
│ ├── config.go # Configuration parsing
│ ├── couch.go # CouchDB client operations
│ └── couch_test.go # CouchDB tests
├── dockerfile # Docker image definition
├── makefile # Build automation
├── go.mod # Go module definition
├── go.sum # Go dependencies
└── README.md # This file
# Download dependencies
make deps
# Run tests
make test
# Run tests with coverage
make test-cov
# Run linter
make lint
# Build binaries for all platforms
make build
# Build and create Docker images
make docker
# Deploy Docker images to registry
make deploy
# Clean build artifacts
make clean- Update your configuration JSON with a new entry in the
jobsarray - Restart the service with the updated configuration
- (Optional) Trigger manual replication via the
/replication/startendpoint
Problem: "waiting for source database to start"
Solution: Verify the source CouchDB instance is running and accessible at the configured address.
curl -u username:password http://source-address:5984Problem: Replication fails with 401 Unauthorized
Solution: Verify credentials in the configuration file match those in your CouchDB instance.
Increase verbosity for debugging:
./dist/db-replicator-linux-amd64 -c config.json -l DebugContributions are welcome! Please ensure:
- Code follows Go best practices
- Tests are added for new functionality
- All tests pass:
make test - Linting passes:
make lint
See LICENSE file for details.