Skip to content

Back up a site before you deploy

Back a site up just before a deploy, and go on only once the backup is stored.

2 min read Updated

Back a site up just before a deploy, and go on only once the backup is stored: if the deploy breaks the site, you can put it back as it was.

What you need

  • A token with sites:read (View sites) and sites:write (Manage sites).
  • The site's id, and backups turned on for it: where they go is chosen once, in the panel or with its backup settings.

1. Check that backups are on

Read the site's backups: settings is null while they're off. latestBackup is the newest backup; when it's running (a scheduled one, say), wait for it instead of starting another: a site has one backup at a time.

curl "https://api.rabbitflare.com/v1/sites/8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d/backup" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN"

2. Back the site up

POST /v1/sites/{siteId}/backups starts a backup of the site's files and database now, to where its backups go. The answer is 202, with the new backup in latestBackup, running.

curl -X POST "https://api.rabbitflare.com/v1/sites/8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d/backups" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN"

3. Wait until it's stored

A backup takes from a minute to an hour or more, by the site's size. Read the site's backups until latestBackup is completed, then deploy. If it's failed, its error says why: don't deploy without a backup. Or let webhooks tell you (backup.completed or backup.failed).

If the deploy breaks the site

Restore the backup, all of it or only its files or database; or download its parts to keep a copy of your own. Both are listed with the site's backups, by id.

What can go wrong

CodeStatusWhat it meansWhat to do
conflict 409 This clashes with something that exists, or is already done (for example, a domain already connected to a site). Read message; there may be nothing left to do.
staging_busy 409 A staging operation is running on this site, or on a site of this server. Wait until it finishes (activeRun in GET /sites/{siteId}/staging is null again), or cancel it, then try again.
payment_required 402 An unpaid bill must be settled first (for example, the Own servers plan's). Pay the open bill on the panel's Billing page, then try again.
rate_limited 429 Too many requests. scope says which limit: token, account, create, operation or network. Wait for Retry-After seconds, then go on more slowly.
feature_unavailable 403 This product isn't open to the account. Contact support if you'd like it opened.
not_found 404 The resource doesn't exist, or isn't this account's. Check the id. Ids of other accounts' resources answer the same way.

Over the number of backups a site can start in a day or an hour, the answer is 429, and the script waits as its Retry-After says, which can be hours: run it again later instead if your deploy can't wait. Any other refusal stops the script, with the API's code and message.

The whole script

Steps 1 to 3 in JavaScript (Node.js 18 or later; save it as an .mjs file). Put your site's id in SITE_ID, and run it before your deploy, in the same job: it ends with an error when the backup fails or is still running after 3 hours, which stops the job before the deploy. After the deploy, clear the site's caches. To try a change before it reaches the live site, use staging.

JavaScript
// Backs a site up before a deploy and waits until the backup is stored. It ends with an error when the backup
// fails, so a deploy that runs after it in the same job doesn't go ahead without one.
const API = "https://api.rabbitflare.com/v1";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

// Sends a request and gives back its data. Over a rate limit (429) it waits as long as Retry-After says, then sends
// the same request again; any other error is thrown with the API's code.
async function rabbitflare(method, path, body, headers = {}) {
  for (;;) {
    const res = await fetch(API + path, {
      method,
      headers: {
        Authorization: `Bearer ${process.env.RABBITFLARE_TOKEN}`,
        ...(body ? { "Content-Type": "application/json" } : {}),
        ...headers,
      },
      body: body ? JSON.stringify(body) : undefined,
    });
    if (res.status === 429) {
      await sleep((Number(res.headers.get("Retry-After")) || 1) * 1000);
      continue;
    }
    const answer = res.status === 204 ? null : await res.json().catch(() => null);
    if (!res.ok) throw Object.assign(new Error(answer?.error?.message ?? `HTTP ${res.status}`), { code: answer?.error?.code });
    return answer?.data ?? null;
  }
}

const SITE_ID = "8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d";

// 1. Backups must be on for the site: where they go is chosen once, in the panel or with PUT /sites/{siteId}/backup/settings.
let view = await rabbitflare("GET", `/sites/${SITE_ID}/backup`);
if (!view.settings) throw new Error("Backups are off for this site: choose where they go first");

// 2. Back it up now, unless a backup is running already (a scheduled one, say): then that one is waited for.
let backup = view.latestBackup?.status === "running" ? view.latestBackup : (await rabbitflare("POST", `/sites/${SITE_ID}/backups`)).latestBackup;
console.log(`Backing up the site (${backup.id})`);

// 3. Wait until it's stored: from a minute to an hour or more, by the site's size.
const deadline = Date.now() + 180 * 60_000;
while (backup.status === "running") {
  if (Date.now() > deadline) throw new Error("The backup is still running after 3 hours");
  await sleep(10_000);
  view = await rabbitflare("GET", `/sites/${SITE_ID}/backup`);
  backup = view.latestBackup?.id === backup.id ? view.latestBackup : (await rabbitflare("GET", `/sites/${SITE_ID}/backups`)).find((b) => b.id === backup.id);
  if (!backup) throw new Error("The backup is no longer listed");
}
if (backup.status !== "completed") throw new Error(`The backup failed: ${backup.error}. Don't deploy without one.`);
console.log(`Backed up to ${backup.location}: go on with the deploy`);

Still have a question?

Our team answers support tickets about the API, and other developers answer in the community.

Contact support

Popular

API docs