Skip to content

Controller Backups

There are different ways to configure backups for your controller, however we recommend the following:

  1. If you have virtualized the controller in a virtual machine, implement regular backups of the entire virtual machine.
  2. Implement our automated offsite database backups. You can read more how to implement this by visiting our github repository git.cmptstks.com/cs-public/ops/controller-backups. For our customers with a support plan including remote server support, please open a ticket and we can help configure this for you.

Take Backup

Database backups can be taken manually at anytime by running cstacks database-backup. This will created a compressed backup under /var/lib/computestacks/backups.

Restoring a backup

To restore a backup, cd to the backup directory and run gzip -d <filename> && psql cloudportal < <filename-without-gz>.

For example, if the database backup is: cloudportal-20200617-0940_53.sql.gz, then you would run:

Danger

I don't recommend copy/pasting the entire block below and running it. Instead, copy each line one-by-one and run it to make sure it's successful.

docker stop portal
dropdb cloudportal && createdb cloudportal
gzip -d cloudportal-20200617-0940_53.sql.gz && psql cloudportal < cloudportal-20200617-0940_53.sql
cstacks upgrade && cstacks run

This will:

  1. Stop the controller
  2. Delete the existing database and re-create it (you may also do this from within psql)
  3. Decompress the database backup and restore it to the newly created cloudportal database
  4. Run the upgrade command for ComputeStacks to ensure the schema is up to date, and finally;
  5. Start the portal back up.

Node Agent Database Backups

Each node's cs-agent (3.7.0 or later) keeps its own state in embedded SQLite databases: control.db and one database per project, under the agent's data_dir. The agent can back these up off the node. It is off by default.

When enabled, the agent snapshots every database on a schedule, encrypts each snapshot to your age public keys, and uploads it to S3-compatible storage. Unchanged databases are skipped. Recent snapshots are kept in full, older ones are thinned to one per day, and anything past the retention window is deleted.

Enable it in the node's agent.yml:

store:
  backup:
    enabled: true
    freq: "*/15 * * * *"
    recipients:
      - age1...
    keep_all_hours: 48
    retention_days: 30
    s3:
      bucket: my-bucket
      prefix: "sqlite/"
Key Default Description
enabled false Turn the backup on.
freq */15 * * * * Cron schedule for a backup pass.
recipients none age public keys to encrypt to. Required when enabled.
node_name the hostname Name used in the object keys.
keep_all_hours 48 Keep every snapshot this recent.
retention_days 30 Thinned to one per day until this age, then deleted.
s3.* backups.export.s3.* endpoint, region, bucket, access_key, secret_key, force_path_style, prefix. Any key left unset falls back to the same key under backups.export.s3, except prefix (default sqlite/). The credentials need Put, Get, List and Delete on the prefix.

Keep the age private identity off the nodes; it is only needed on the machine where you restore. With the provisioner, set cs_agent_store_backup_enabled and cs_agent_store_backup_recipients (plus the other cs_agent_store_backup_* variables), and it renders these keys for you.

An admin token can read the backup state from GET /v1/admin/store-backup on the node: the last success, last error and upload counts. It returns {"enabled": false} when the backup is off.

Restoring

Stop the agent, restore, and start it again:

sudo systemctl stop cs-agent
sudo cs-agent store-restore list
sudo cs-agent store-restore node --identity /path/to/identity.txt      # control.db and all projects
sudo cs-agent store-restore control --identity /path/to/identity.txt   # control.db only
sudo cs-agent store-restore project --identity /path/to/identity.txt --project <id>
sudo systemctl start cs-agent

Existing databases are moved aside to <data_dir>/pre-restore-<timestamp>/, not deleted. Run cs-agent store-restore help for the remaining flags (--node, --at, --lineage, --allow-missing, --yes).

A node or control restore lists the restored pending and running tasks and leaves the node in maintenance (the node hold), because some of those tasks may already have run after the snapshot was taken. Review them, then run sudo cs-agent maintenance off.