Skip to content

Back Up and Restore

Three parts make up a full backup of a Gradient server. They are the database, the secret files and the state directory. The setup below can take a backup with restic every night. The last step is a restore of that backup to a new host.

Requirements:

  • A server with the local database, see Installation
  • A restic repository on another machine

Backup Contents

Data Location Without a Backup
Database PostgreSQL database gradient Users, projects, caches and build history disappear.
cryptFile Path in services.gradient.secrets.cryptFile Secrets in the database stay unreadable, including cache signing keys, SSH keys for projects, tokens for Git hosts and webhook secrets.
jwtFile Path in services.gradient.secrets.jwtFile A new random key is enough. All users sign in again.
NAR files /var/lib/gradient/nars Workers build the lost paths again. Paths from gradient cache upload disappear.
Build logs /var/lib/gradient/logs Logs of completed builds are empty.
SSH host key /var/lib/gradient/ssh_host_ed25519_key Clients of Build over SSH get a warning about a changed host key.

/var/lib/gradient is the default for services.gradient.baseDir.

1. Database Dump

services.postgresqlBackup = {
  enable = true;
  databases = [ "gradient" ];
  compression = "zstd";
  startAt = [ ]; # (1)!
};
  1. The dump job has no timer of its own. The file backup in step 2 is the trigger for the dump.

  2. The dump file is /var/backup/postgresql/gradient.sql.zstd.

  3. pg_dump can read one consistent snapshot of the database. The server is up during the dump.
  4. Use a local database for this setup. Take the backup on the database host when the database is external and omit the systemd.services block in step 2.

2. File Backup

services.restic.backups.gradient = {
  repository = "sftp:backup@backup.example.com:/srv/gradient"; # (1)!
  passwordFile = "/var/lib/gradient-secrets/restic-password"; # (2)!
  initialize = true;
  paths = [
    "/var/backup/postgresql"
    "/var/lib/gradient"
    "/var/lib/gradient-secrets" # (3)!
  ];
  pruneOpts = [ "--keep-daily 7" "--keep-weekly 4" "--keep-monthly 6" ]; # (4)!
  timerConfig.OnCalendar = "01:15";
};

systemd.services.restic-backups-gradient = { # (5)!
  requires = [ "postgresqlBackup-gradient.service" ];
  after = [ "postgresqlBackup-gradient.service" ];
};
  1. Set any restic repository type here. Pick a location on another machine. Store the credentials for the repository outside the host as well, such as SSH keys and environmentFile.
  2. Generate the password with openssl rand -base64 32. Store a copy off-host. The repository is unreadable without this password.
  3. Directory for jwtFile and cryptFile as plain files. Skip this line for sops-nix and agenix. See step 3.
  4. restic can keep every snapshot without this option and the repository can only grow.
  5. All backups start from a new dump. No file backup can start after a failed dump.

systemctl start restic-backups-gradient.service can take a backup at once, for example before an upgrade.

3. Secrets

Setup Backup
Plain files The directory in paths of step 2. Add the files for every other *File option from services.gradient to paths.
sops-nix, agenix The encrypted files in the configuration repository. Keep the key for decryption outside the host.

A database without the matching cryptFile has lost every secret. Store both in the same backup.

4. Restore

Deploy the NixOS configuration to the new host with the same Gradient release or a newer one. Create /var/lib/gradient-secrets with install -d -m 700. Copy the restic password to the path in passwordFile. Copy the credentials for the repository to the right place. Then run the following commands as root.

systemctl stop gradient-server.service
restic-gradient restore latest --target / # (1)!
chown -R gradient:gradient /var/lib/gradient # (2)!
sudo -u postgres dropdb --if-exists gradient # (3)!
zstd -dc /var/backup/postgresql/gradient.sql.zstd | sudo -u postgres psql --set ON_ERROR_STOP=1 postgres
systemctl start gradient-server.service
  1. The restic module can install this command with the repository and password already configured. Use restic-gradient snapshots to list all backups. Replace latest with the snapshot ID to restore an older backup.
  2. The user ID can differ between the two hosts.
  3. The first boot on the new host created an empty database. The dump can recreate the database.

  4. The local worker can reconnect automatically.

  5. Do not start an older Gradient release on the restored database. See Upgrade Notes.

Verify Deployment

  • Sign in to an account from before the loss and confirm the sign-in works.
  • Find the key from before the loss in Public Key on each cache settings page. Clients keep trusted-public-keys.
  • Check journalctl -u gradient-server for errors about the database or secrets.
  • Open the logs of builds from before the loss.

Data Newer than the Dump

File backups start after the dump. Files can be newer than the database. Deep GC can repair both differences after a restore.

Difference Repair
NAR file without a database row Deep GC can delete files older than gc.narUploadGraceHours (24 hours by default).
Database row without a NAR file Deep GC can delete the row. Workers build the path again for the next evaluation with that path.

The Run Deep GC button on the Administration page can start a pass immediately.

Backup without NAR Files

NAR files are the largest part of a backup. Add exclude = [ "/var/lib/gradient/nars" ]; to the restic backup for a small backup. Use Run Deep GC right after restoring such a backup. Caches can list paths with a failing NAR download until that pass has finished. Paths from gradient cache upload are lost.

S3 Storage

NAR files and completed build logs go to an S3 bucket with services.gradient.s3.bucket set.

  • Keep /var/lib/gradient in paths. Logs for builds in progress and the SSH host key stay there.
  • Copy the bucket to a second bucket. Use rclone sync, for example. Object versioning isn't a substitute for that copy. Use a bucket without object versioning for Gradient.

Standalone

The database, secrets and state directory of a standalone instance all live in one place.

Variant Backup
Docker Stop the container and archive the gradient volume. See the Docker documentation for volume backups.
Nix Stop the VM and copy gradient.qcow2.

Workers

  • Workers upload every build output to the server and keep no copy that a backup needs to cover.
  • A reinstalled worker can connect again with the same services.gradient.worker.id and the same peers file.
  • Workers without services.gradient.worker.id keep the generated ID in /var/lib/gradient-worker/worker-id. Add that file to the backup on the worker.
  • Workers with a new ID need a new registration on the server.

Next Steps