Book a free review

Ultima / Blog

Backups that outlive your cloud provider

The backups existed. In the same cloud, under the same key, in the bucket next to production. Here is how to build copies that survive both a data center outage and a compromised server.

· Ultima team

Where it started

A home goods marketplace. A few hundred orders on a normal day, three to four times more before holidays. Everything ran at one cloud provider: an 800 GB MySQL database (orders, customers, catalog, price and stock history), about 6 TB of product card files and a couple of dozen services around them. A nightly cron job ran mysqldump into the same provider's object storage, with the same key production used.

On paper there were backups, but:

  • there was one copy, and it sat at the same provider. If that data center goes down, the backup goes with it;
  • the key on the server could delete. Whoever gets into the server can delete both production and the backups;
  • the backups were not encrypted: with the storage key you could read the whole customer database;
  • an 800 GB dump took 3–4 hours and loaded the database the whole time, so it could not run more than once a day;
  • up to 24 hours of orders could be lost;
  • nobody had ever restored from a backup, so nobody knew how long a restore would take.

What we did

We followed the 3-2-1-1-0 rule:

  1. Three copies: production, storage at the main provider and storage at a second provider.
  2. Two types of media: production on block disks, copies in object storage.
  3. One copy elsewhere: the second provider has a separate account, separate keys and a separate owner.
  4. One immutable copy: the second provider has Object Lock set to 35 days, longer than the restore test interval. The key that writes backups cannot delete object versions, and the key with full rights is not used in automation.
  5. Zero errors: once a month a test environment is built automatically from a fresh backup, smoke tests run on it, then the environment is deleted. If the restore fails, an alert fires.

How it is set up

WAL-G for MySQL. Full backup through xtrabackup, zstd compression. The config lives in /etc/wal-g/env:

WALG_S3_PREFIX=s3://backup-main/mysql
AWS_ENDPOINT=https://storage.provider-a.example
AWS_ACCESS_KEY_ID=<key>
AWS_SECRET_ACCESS_KEY=<secret>
# almost every non-AWS storage needs path-style addressing; without it WAL-G hangs silently
AWS_S3_FORCE_PATH_STYLE=true
WALG_MYSQL_DATASOURCE_NAME='backup:<password>@tcp(127.0.0.1:3306)/mysql'
WALG_COMPRESSION_METHOD=zstd
# encryption: only the public key is on the server
WALG_PGP_KEY_PATH=/etc/wal-g/backup-public.asc
WALG_STREAM_CREATE_COMMAND="xtrabackup --backup --stream=xbstream --datadir=/var/lib/mysql"
WALG_STREAM_RESTORE_COMMAND="xbstream -x -C /var/lib/mysql"
WALG_MYSQL_BACKUP_PREPARE_COMMAND="xtrabackup --prepare --target-dir=/var/lib/mysql"
WALG_MYSQL_BINLOG_REPLAY_COMMAND='mysqlbinlog --stop-datetime="$WALG_MYSQL_BINLOG_END_TS" "$WALG_MYSQL_CURRENT_BINLOG" | mysql'

The file has mode 600 because it holds keys. The login and password for xtrabackup and mysql come from /root/.my.cnf. A full backup runs once a week, binlogs ship every minute. Both run from systemd timers. The binlog service looks like this:

# /etc/systemd/system/walg-binlog.service
[Service]
Type=oneshot
EnvironmentFile=/etc/wal-g/env
ExecStart=/usr/bin/mysql -e "FLUSH BINARY LOGS"
ExecStart=/usr/local/bin/wal-g binlog-push

Its timer uses OnCalendar=*:0/1. The full backup has its own service with wal-g backup-push and OnCalendar=Sun 03:00. Old backups at the main provider are pruned with wal-g delete retain FULL 5 --confirm.

Encryption. WAL-G encrypts backups with a public PGP key. Only that key is on the server, so a backup can be written but not read: neither from the server nor with a leaked storage key. The private key is kept in a secrets manager and as an offline copy held by two people. It is needed only for a restore: then WALG_PGP_KEY_PATH points to it and the passphrase goes into WALG_PGP_KEY_PASSPHRASE. restic encrypts its repository itself, and its password is stored the same way. The monthly restore test takes the keys from the same store, so it also proves they have not been lost.

The copy at the second provider. A separate job copies new WAL-G files to the second storage every minute:

rclone copy main:backup-main/mysql reserve:backup-reserve/mysql

It has to be copy, not sync: copy only adds files, so a deletion at the main provider never reaches the reserve.

Object Lock in the second storage. You enable it once. How depends on the provider: in a cloud it is usually a checkbox at bucket creation or a CLI command. Here is an example for a self-hosted SeaweedFS through the S3 API:

s3() { aws --endpoint-url https://backup-reserve.internal:8333 s3api "$@"; }
# create the bucket with locking on; SeaweedFS turns versioning on by itself
s3 create-bucket --bucket backup-reserve --object-lock-enabled-for-bucket
s3 put-object-lock-configuration --bucket backup-reserve --object-lock-configuration \
  '{"ObjectLockEnabled":"Enabled","Rule":{"DefaultRetention":{"Mode":"COMPLIANCE","Days":35}}}'

In COMPLIANCE mode a version cannot be deleted before the retention period ends, even by the account owner.

Key permissions for the second storage. How you set them depends on the provider: a bucket policy in one place, service account roles in another. The idea is the same. The key that writes backups may read, write and place a delete marker (s3:GetObject, s3:PutObject, s3:DeleteObject, s3:ListBucket). It may not delete object versions, bypass the lock or change versioning and Object Lock settings (s3:DeleteObjectVersion, s3:BypassGovernanceRetention, s3:PutBucketObjectLockConfiguration, s3:PutBucketVersioning). The key with full rights is not used in automation.

Product card files. restic writes to two repositories, one at each provider:

export RESTIC_PASSWORD_FILE=/etc/restic/password
restic -r s3:https://storage.provider-a.example/cards-main backup /srv/cards
restic -r s3:https://storage.provider-b.example/cards-reserve backup /srv/cards
restic -r s3:https://storage.provider-a.example/cards-main forget --keep-daily 14 --keep-weekly 8 --prune

In the second repository forget --prune runs once a month, and only for snapshots older than the Object Lock period.

Alerting. After every successful run the service writes a timestamp to a file for node_exporter:

echo "backup_last_success_timestamp $(date +%s)" > /var/lib/node_exporter/textfile/backup.prom

The rule in Prometheus or VictoriaMetrics: time() - backup_last_success_timestamp > 3 * 3600.

The restore test. Once a month CI builds a test environment from the same OpenTofu and Ansible code as production, restores the database and the product card files into it from the second storage, runs smoke tests and deletes the environment. It holds real customer data, so no human access is configured: the environment lives in a separate isolated project, is deleted automatically after a few hours even on failure, and only metrics and test results leave it. The duration of every step goes into metrics, so at any moment we know how long a real restore will take.

Pitfalls

binlog-push ships only closed binlogs. The current file, the one MySQL is writing to right now, is not sent. Without FLUSH BINARY LOGS before the push you can lose not a minute but everything since the last rotation, which may be an hour. That is why the rotation is the first line of the service.

restic does not work with a write-only key. It creates and removes lock files in the repository. Without the delete permission the snapshot is saved but the lock file stays, and later forget and prune refuse to run because the repository is locked. So the key may delete, but only by placing a delete marker: in a versioned bucket the version itself stays, and Object Lock plus the ban on s3:DeleteObjectVersion keep it from being removed.

COMPLIANCE cannot be undone. The retention period cannot be shortened even by the account owner, so everything written to the bucket is stored and billed for at least 35 days. Test the setup first on a separate bucket with a 1-day period.

A restore from a versioned bucket may fail. If WAL-G uploads a file again (for example, after a server reinstall it does not remember what it has sent), old versions stay in the second storage. On SeaweedFS WAL-G saw them as duplicates and binlog-replay failed with file exists. The workaround: copy the backup from the second storage to disk with rclone copy and restore from that copy, setting WALG_FILE_PREFIX instead of WALG_S3_PREFIX. This is exactly what the monthly test catches, so the test has to restore from the second storage.

The result

BeforeAfter
Orders you can loseup to 24 hoursabout a minute
Restore timeunknowndatabase about 2 hours; product card files 2–3 hours over a 10 Gbit/s link
Provider's data center is downno datarestore at the second provider
Server is compromisedbackups deleted together with productionthe immutable copy is intact
Storage key leaksthe whole customer database is readablebackups are encrypted and unreadable without the private key
Backup load on the database3–4 hours every nighta full backup 3 times faster than the dump, once a week; binlogs every minute with almost no load

If production is down: the restore runbook

  1. Stop taking orders with a maintenance page.
  2. Build the environment at the second provider from OpenTofu and Ansible.
  3. Restore the database: copy the backup from the second storage to disk (rclone copy), take the private key from the secrets manager, then run wal-g backup-fetch LATEST and wal-g binlog-replay --since LATEST --until <time of the incident> from that copy. About 2 hours.
  4. In parallel with the database, restore the product card files: restic restore latest. 2–3 hours over a 10 Gbit/s link.
  5. Switch DNS to the new environment.
  6. Run smoke tests and start taking orders again.

Steps 3 and 4 run at the same time, so the whole thing takes about 3 hours. The order is the same as in the monthly test, so nobody invents it on the day of the incident.

Where to start

  1. Check where your backups live and which key can delete them.
  2. Add a second provider for a copy, at least for the database.
  3. Restore once by hand and time it.
  4. Automate that restore.

If you run Kubernetes, microservices and containers, there are answers for those too: volume and database backups straight from the cluster, a reserve in a second cloud, and the whole platform described in git.

Want a second pair of eyes on your setup? Book a free review with Ultima.