This article explains what to back up on a Countly Server that runs on Docker Compose, how to take each backup, and how to restore it. Use it before every upgrade and for your regular backups.
Run the commands as root on the Countly host, in the bundle directory. The names are the bundle defaults: containers countly-mongodb, countly-clickhouse, and countly-kafka, network countly-network, and customer directory /etc/countly. If MongoDB, ClickHouse, or Kafka is external, your provider backs up that store.
Knowing What to Back Up
| Data | Why | How |
|---|---|---|
| MongoDB | Apps, users, settings, aggregated data | mongodump --oplog, or a disk snapshot |
ClickHouse countly_drill and identity | Drill events, user identity map | BACKUP to a backups disk |
| Kafka | Push sends that are not due yet, consumer offsets, events not consumed yet, cluster ID | Snapshot of KAFKA_DATA_DIR, and KAFKA_CLUSTER_ID |
| Secrets and settings | New secrets cannot read old data | A copy of /etc/countly/ |
Kafka data is not temporary. The topic CLY_PUSH_MESSAGE_SCHEDULE has no expiry and holds every push send that is not due yet.
All procedures are manual. Countly does not ship backup automation, point-in-time restore of MongoDB, or an export tool for Kafka topics.
Understanding the Exact Pair Rule
The ClickHouse table countly_drill.drill_events does not remove duplicate rows. Thus the ClickHouse backup and the offsets of the Kafka sink must match exactly. For each ClickHouse backup, you pause the sink, save its offsets, take the backup, and then resume the sink. On a restore, you set the sink back to the saved offsets, and Kafka sends the events after the backup again.
A long pause can lose events
If a restore uses offsets that do not match the backup, Countly loses events or writes them twice. When the drill-events topic reaches its share of the Kafka disk, Kafka discards the oldest events, also events that the sink did not read. Keep the pause short and monitor the sink lag.
Following the Recommended Policy
Change these values to agree with your own recovery targets.
| Item | Recommended |
|---|---|
| MongoDB | One backup each day |
| ClickHouse | One full backup each week, one incremental backup each day |
| Retention | 7 daily, 4 weekly, 3 monthly |
| Location | A different project or region from the data, encrypted |
| Restore drill | Each month, on a staging copy. The time it takes is your real recovery time. |
Backing Up the Files
Copy these off the host after each change. Keep them with the data backups of the same date.
/etc/countly/.env: the generated app secrets,CLICKHOUSE_PASSWORD,KAFKA_CLUSTER_ID(when you set it), and your settings. Pre-install refuses to make new secrets whilecountly-mongodbexists, so you must keep this file./etc/countly/ssl/and/etc/countly/vhosts/: certificates and nginx sites./etc/countly/clickhouse/: your ClickHouse drop-ins, for examplebackups.xml./etc/countly/.active-tierand/etc/countly/.active-options: tier and profiles.The Docker volume
countly_letsencrypt(Let's Encrypt account and certificates):docker run --rm -v countly_letsencrypt:/le:ro -v "$PWD":/out alpine:3.21 tar czf /out/letsencrypt.tgz -C /le .
Without KAFKA_CLUSTER_ID in .env, the stack uses the fixed default MkU3OEVBNTcwNTJENDM2Qk. The broker refuses data with a different cluster ID.
Backing Up the Data
Back up MongoDB first. Then pause the sink only for the ClickHouse backup, and resume it as soon as that backup is complete. A short pause keeps the risk of lost events low.
Step 1: Backing Up MongoDB
The bundle runs a one-member replica set (rs0). A dump with --oplog is a consistent copy of the full instance. --oplog does not work with --db, so always dump the full instance:
docker exec countly-mongodb mongodump --oplog --gzip --archive > mongo-$(date +%F).archive.gz
The hardened MongoDB image has no mongodump. On the hardened flavor, run the tools image on the stack network:
docker run --rm --network countly-network library/mongo:8.3.11 mongodump \ --uri='mongodb://mongodb:27017/?directConnection=true' --oplog --gzip --archive > mongo-$(date +%F).archive.gz
For large data, take a snapshot of MONGODB_DATA_DIR with LVM, ZFS, or a cloud disk snapshot. The journal is on the same volume, so the snapshot is crash-consistent. If your storage has no snapshots, stop mongodb, copy the directory, and then start it again.
Step 2: Pausing the Sink and Saving Its Offsets
Pause the sink and check that its state is
PAUSED:docker exec countly-connector-watch curl -fsS -X PUT http://kafka-connect:8083/connectors/clickhouse-sink/pause docker exec countly-connector-watch curl -fsS http://kafka-connect:8083/connectors/clickhouse-sink/status
Save the offsets of the sink consumer group:
docker exec countly-kafka env -u KAFKA_OPTS KAFKA_HEAP_OPTS=-Xmx256m \ /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server kafka:29092 \ --describe --group connect-clickhouse-sink \ | awk '$2 == "drill-events" {print $2 "," $3 "," ($4 == "-" ? 0 : $4)}' > sink-offsets-$(date +%F).csv- Run the last command again one minute later. The file must not change. If you set
KAFKA_DRILL_EVENTS_TOPIC, use that topic name in place ofdrill-events.
Do not run make up until you resume the sink. connector-init resumes a paused sink.
Step 3: Backing Up ClickHouse and Resuming the Sink
One time only, add a backups disk. Create /etc/countly/clickhouse/config.d/backups.xml. This folder belongs to you, and a release does not replace the file:
<clickhouse>
<storage_configuration>
<disks>
<backups>
<type>local</type>
<path>/var/lib/clickhouse/backups/</path>
</backups>
</disks>
</storage_configuration>
<backups>
<allowed_disk>backups</allowed_disk>
<allowed_path>/var/lib/clickhouse/backups/</allowed_path>
</backups>
</clickhouse>ClickHouse usually loads the new disk by itself. If backups is not in system.disks (the first query below shows no row), plan a ClickHouse restart.
Disk space
By default the backups are on the ClickHouse data disk. A full backup needs about as much free space as the two databases use, and a full disk stops ClickHouse inserts. On a host with one disk, it also stops MongoDB and Kafka.
To keep the backups off the data disk, mount a separate disk at $CLICKHOUSE_DATA_DIR/backups, give it to uid 101 (chown 101:101), and restart ClickHouse. You can also back up to object storage with BACKUP ... TO S3(...). In all cases, copy the backups off the host.
Check the free space. Start the backup only when
okis1. The disk then keeps 20% of its size free after a full backup:CLICKHOUSE_PASSWORD=$(grep '^CLICKHOUSE_PASSWORD=' /etc/countly/.env | cut -d= -f2-) ch() { docker exec countly-clickhouse clickhouse-client --user countly --password "$CLICKHOUSE_PASSWORD" -q "$1"; } ch "SELECT formatReadableSize(p.need) AS need, formatReadableSize(d.free_space) AS free, d.free_space > p.need + 0.2 * d.total_space AS ok FROM (SELECT sum(bytes_on_disk) AS need FROM system.parts WHERE active AND database IN ('countly_drill', 'identity')) AS p, (SELECT free_space, total_space FROM system.disks WHERE name = 'backups') AS d"Back up both databases:
ch "BACKUP DATABASE countly_drill, DATABASE identity TO Disk('backups', 'full-$(date +%F)') ASYNC"Wait until the backup shows
BACKUP_CREATED:ch "SELECT name, status, error FROM system.backups ORDER BY start_time DESC LIMIT 5"
Resume the sink:
docker exec countly-connector-watch curl -fsS -X PUT http://kafka-connect:8083/connectors/clickhouse-sink/resume
For an incremental backup on top of a full one, add a base backup. It copies only the parts that are not in the base backup. Keep each incremental backup with its full backup, because you need both to restore:
ch "BACKUP DATABASE countly_drill, DATABASE identity TO Disk('backups', 'incr-$(date +%F)')
SETTINGS base_backup = Disk('backups', 'full-YYYY-MM-DD') ASYNC"Step 4: Backing Up Kafka
- Take a snapshot of
KAFKA_DATA_DIR. The bundle has no tool to export a topic. Save the offsets of all consumer groups for reference:
docker exec countly-kafka env -u KAFKA_OPTS KAFKA_HEAP_OPTS=-Xmx256m /opt/kafka/bin/kafka-consumer-groups.sh \ --bootstrap-server kafka:29092 --describe --all-groups > kafka-groups-$(date +%F).txt
Restoring Countly
A restore replaces your current data
mongorestore --drop and DROP DATABASE delete the current data. Check that the backup is complete before you drop anything.
To restore a full host, follow this order:
-
Install the bundle. Put back
/etc/countly/and the Let's Encrypt volume before the first deploy. -
Put back the data directories from the snapshots, or start with empty
ones. If you start with empty ones, copy the ClickHouse backup files
back to
$CLICKHOUSE_DATA_DIR/backups/and give them to uid 101 (chown -R 101:101). -
Start the datastores only, then start the replica set:
bin/countly-compose up -d mongodb clickhouse kafka bin/countly-compose up mongodb-init -
Restore MongoDB:
docker exec -i countly-mongodb mongorestore --oplogReplay --drop --gzip --archive < mongo-YYYY-MM-DD.archive.gz -
Check the ClickHouse backup. Do this step only when you restore on the
host that made the backup: a new, empty ClickHouse has no line for the
old backup in
system.backup_log. The last line must showBACKUP_CREATED. For an incremental backup, check its full backup too:CLICKHOUSE_PASSWORD=$(grep '^CLICKHOUSE_PASSWORD=' /etc/countly/.env | cut -d= -f2-) ch() { docker exec countly-clickhouse clickhouse-client --user countly --password "$CLICKHOUSE_PASSWORD" -q "$1"; } ch "SELECT event_time, name, status, error FROM system.backup_log WHERE name LIKE '%full-YYYY-MM-DD%' ORDER BY event_time_microseconds DESC LIMIT 1" -
Drop the two databases and restore them.
RESTORErefuses a table that holds data:ch "DROP DATABASE IF EXISTS identity SYNC" ch "DROP DATABASE IF EXISTS countly_drill SYNC" ch "RESTORE DATABASE countly_drill, DATABASE identity FROM Disk('backups', 'full-YYYY-MM-DD')" -
Set the sink group to the offsets that you saved with this backup. Kafka
Connect must not run yet:
docker cp sink-offsets-YYYY-MM-DD.csv countly-kafka:/tmp/sink-offsets.csv docker exec countly-kafka env -u KAFKA_OPTS KAFKA_HEAP_OPTS=-Xmx256m /opt/kafka/bin/kafka-consumer-groups.sh \ --bootstrap-server kafka:29092 --group connect-clickhouse-sink \ --reset-offsets --from-file /tmp/sink-offsets.csv --execute -
Start the full stack with
make up, then runmake verify.connector-initresumes the sink.
To restore on a running host, first stop the writers. To restore MongoDB only, stop the Countly services:
bin/countly-compose stop countly-api countly-frontend countly-ingestor countly-aggregator countly-jobserver
To restore ClickHouse, stop Kafka Connect too. With Kafka Connect stopped, the sink group has no members, and you can set its offsets:
bin/countly-compose stop countly-api countly-frontend countly-ingestor countly-aggregator countly-jobserver kafka-connect
Then do steps 4 to 8. The hardened MongoDB image has no mongorestore. On the hardened flavor, run it from the tools image. Keep the -i option: without it, mongorestore gets no input and restores nothing.
docker run --rm -i --network countly-network library/mongo:8.3.11 mongorestore \ --uri='mongodb://mongodb:27017/?directConnection=true' --oplogReplay --drop --gzip --archive < mongo-YYYY-MM-DD.archive.gz