Skip to content

Backups

A community's backup carries everything a plug-in keeps in Initiative itself: its install's config, placements and permission rules, a tool plug-in's rows, and the values a plug-in keeps on items. Initiative backs those up for every plug-in, whoever publishes it. What a plug-in keeps on its own platform, it backs up there: Initiative manages no plug-in's data.

The exception is a plug-in Initiative's publisher builds. Initiative sends backup and restore only to those, so what they keep for a community travels in its backup. No other plug-in is sent either hook, or told that a backup is being made; the backup lists its own data as kept by its publisher.

import { fetchIdMap, fetchRestorePart, reportRestore, uploadBackupPart } from "initiative-plugin-sdk/client";
import type { RestoreCall } from "initiative-plugin-sdk/manifest";

hooks: {
  backup: async ({ installation, scope, upload }) => {
    if (!(await holdsAnything(installation, scope))) return "empty";
    // Answer at once: the upload goes on after the hook has answered.
    void uploadBackupPart(upload, dump(installation, scope), { format: "tracker-rows", schemaVersion: 3 }).catch(log);
    return "upload";
  },
  restore: async (call) => {
    void restorePart(call); // accepted: the work goes on afterwards
  },
},

async function restorePart(call: RestoreCall) {
  try {
    const ids = await fetchIdMap(call);
    const part = await fetchRestorePart(call);
    await load(call.installation, part.body!, { format: call.format, schemaVersion: call.schema_version, ids });
    await reportRestore(call, { status: "done" });
  } catch (error) {
    await reportRestore(call, { status: "failed", detail: String(error) });
  }
}

Backing up.

  1. While a backup runs, Initiative calls backup at once, rather than queueing it. scope.initiative_id is null for a backup of the whole community, or the id of the one initiative being backed up: hand over that initiative's part, or the community's.
  2. The hook answers within its 5 seconds:
  3. "upload" is answered 202: you will hand your part over;
  4. "empty" is answered 204: you hold nothing for this backup;
  5. a plug-in without the hook is answered 404, and the backup records that it does not take part.

A hook that throws, or answers anything else, is answered 500, and the part is recorded as failed. A backup never fails because a plug-in did. 3. uploadBackupPart(upload, body, { format, schemaVersion }) hands the part over before deadline, 30 minutes after the call. body is the part's bytes, streamed or whole; it is PUT to upload.url on upload.token, whose only right is writing this part until the deadline. It resolves once Initiative has stored the part, and throws BackupPartError otherwise: status 409 is a part already uploaded, since a part is taken once, and 413 a part over the backup's upload limit, which it counts toward. A part that has not come by the deadline is recorded as failed.

Restoring.

  1. A restore installs the plug-in again where it is missing. Once installed has been delivered, Initiative queues restore. Accept it at once (answered 202) and do the work afterwards. A plug-in without the hook is answered 404.
  2. fetchRestorePart(call) answers Initiative's response, whose body streams the part's bytes. format and schema_version are what you uploaded it with.
  3. fetchIdMap(call) answers the map from what your part names to what the restore made. A restore always makes new initiatives and items, so every id of Initiative's that your part names goes through it:
{"initiatives": {"4": 12}, "members": {"uref_alice": "uref_ada", "uref_bob": null}, "items": {"task": {"30": 51}, "file": {"2": 6}}}

Keys are old ids, as strings. items holds every kind of item the restore made, by kind (task, file, contact, calendar_event, …). Your plug-in knows members only by reference, so members maps your old member references to new ones, never Initiative's user ids; a member the restore did not bring across maps to null. A restore into another community reaches the plug-in under that community's installation. 4. reportRestore(call, { status, detail }) says how it ended, done or failed; detail is cut to 500 characters. The restore shows the part as pending until then, and as failed if no report has come by deadline, 24 hours after the restore.

All three take download.token, whose only right is reading this restore's part and id map and making its report, until the deadline. Each throws BackupPartError when Initiative refuses. A plug-in that may restart in the meantime keeps the call's addresses and token in its own queue: they hold until the deadline.

What a part holds. A part is the plug-in's own. Its format name (at most 64 characters) and its schema version are yours to choose and change, and Initiative stores the bytes as given and hands them back unread. Put no credentials in a part: vendor tokens, keys and secrets stay out of every backup, and a restored plug-in's connections are made again.