π End-to-end encrypted cloud for everything.
Copycat DB
Copycat DB is a service to take a backup of our database. It uses the Scaleway CLI to take backups of the database, and uploads them to an offsite bucket.
This bucket has an object lock configured, so backups cannot be deleted before expiry. Conversely, the service also deletes backups older than some threshold when it creates a new one to avoid indefinite retention.
In production the service runs as a cron job, scheduled using a systemd timer.
These backups are in addition to the regular snapshots that we take, and are meant as a second layer of replication. For more details, see our Reliability and Replication Specification.
Quick help
View service status (it gets invoked as a timer automatically, doesn't need to be started/stopped manually):
sudo systemctl status copycat-db
View logs locally (they'll also be available on Grafana):
sudo tail /root/var/logs/copycat-db.log
Backup Mode
Copycat DB requires an explicit BACKUP_MODE:
scw-export: take a Scaleway-managed backup directly fromSCW_RDB_INSTANCE_IDsnapshot-scw-export: restore the latest snapshot intocopycat-db-temp, take a Scaleway-managed backup from that temporary instance, then delete itsnapshot-pgdump: restore the latest snapshot intocopycat-db-temp, attach it toSCW_PRIVATE_NETWORK_ID, runpg_dumpover the private endpoint, delete the temporary instance, then package the dump
To use a snapshot-backed mode in production, set BACKUP_MODE accordingly in /root/copycat-db.env and then start the service or let the timer run it.
This path is intentionally conservative about cleanup. If something fails before the final explicit delete, the script leaves the temporary instance behind instead of attempting automatic cleanup during error handling. This is done to avoid accidental deletions if the script ends up in an unexpected state.
If that happens, clean it up manually in the Scaleway dashboard by deleting the DB instance named copycat-db-temp.
Required environment variables
BACKUP_MODE
Required. See the Backup Mode section above for the available values and their behaviour.
SCW_CONFIG_PATH
Path to the config.yaml used by Scaleway CLI.
This contains the credentials and the default region to use when trying to create and download the database dump.
If needed, this config file can be generated by running the following commands on a shell prompt in the container (using ./test.sh sh)
scw init
scw config dump
SCW_RDB_INSTANCE_ID
The UUID of the Scaleway RDB instance to back up.
SCW_PRIVATE_NETWORK_ID
Required when BACKUP_MODE=snapshot-pgdump.
This is the UUID of the Private Network that copycat-db should attach to the temporary restored Database Instance before running pg_dump.
The compute instance running copycat-db must also be attached to this Private Network.
PGUSER, PGPASSWORD, PGHOST, PGPORT
PGUSER and PGPASSWORD are required for snapshot-pgdump backups and restores.
For snapshot-pgdump backups, the host and port are resolved by the script. For restores, provide PGHOST and PGPORT.
RCLONE_CONFIG
Location of the config file containing the destination bucket and its credentials.
Specifically, the config file contains two remotes:
The bucket itself, where data will be stored.
A "crypt" remote that wraps the bucket by applying client side encryption.
The configuration file will contain (lightly) obfuscated versions of the password, and as long as we have the configuration file we can continue using rclone to download and decrypt the plaintext. Still, it is helpful to retain the original password too separately so that the file can be recreated if needed.
A config file can be generated using ./test.sh sh
rclone config
rclone config show
When generating the config, we keep file (and directory) name encryption off.
Note that rclone creates a backup of the config file, so Docker needs to have write access to the directory where it is mounted.
RCLONE_DESTINATION
Name of the (crypt) remote to which the dump should be saved. Example: db-backup-crypt:.
Note that this will not include the bucket - the bucket name will be part of the remote that the crypt remote wraps.
Logging
The service logs to standard output and standard error. The systemd unit writes these to /root/var/logs/copycat-db.log.
Local testing
The provided test.sh script can be used to do a smoke test for building and running the image. For example,
./test.sh sh
gives us a shell prompt inside the built and running container.
For more thorough testing, run this service as part of a local test-cluster.
Restoring
The service can also restore the latest backup into a Postgres instance. Phoenix periodically uses this to verify that backups are restorable.
To invoke this, use "./restore.sh" as the command when running the container (e.g. ./test.sh ./restore.sh). This will restore the latest backup into the Postgres instance whose credentials are provided via the various PG* environment variables.
Preparing the bucket
The database dumps are stored in a bucket that has object lock enabled (compliance mode), and has a default bucket level retention time of 30 days.
Deploying
Ensure that Alloy is running, and is configured to scrape /root/var/logs/copycat-db.log.
Create the configuration and log directories.
sudo mkdir -p /root/var/config/scw
sudo mkdir -p /root/var/config/rclone
sudo mkdir -p /root/var/logs
Create the environment, Scaleway, and rclone configuration files.
sudo tee /root/copycat-db.env
sudo tee /root/var/config/scw/copycat-db-config.yaml
sudo tee /root/var/config/rclone/copycat-db-rclone.conf
Install the service and timer definitions.
scp copycat-db.{service,timer} instance:
sudo mv copycat-db.{service,timer} /etc/systemd/system
sudo systemctl daemon-reload
To start the cron job
sudo systemctl start copycat-db.timer
The timer triggers the service on its schedule. To run the job immediately:
sudo systemctl start copycat-db.service
Updating
To update, run the GitHub workflow to build and push the latest image to our Docker registry, then restart the systemd service on the instance.
sudo systemctl restart copycat-db