DEV Community

Faelvorn538072
Faelvorn538072

Posted on

Full App Backup: Tenant-Safe User Uploads and Database Dump Storage (7 Checks)

At 08:17, the page says last night's full app backup is green. A healthtech team opens a user upload training artifact and finds a missing object, or worse, an object belonging to a different tenant. The useful question is not whether a job uploaded bytes. It is whether the run can prove what it copied, who owns it, and how to restore it without crossing a tenant boundary.

Short answer: make each run an immutable, tenant-scoped unit with a manifest, capture the database dump at a defined snapshot point, and alert on restore evidence rather than upload success. A nightly Node.js scheduler can trigger this workflow, but the isolation contract must live in storage policy, credentials, and verification.

The first signal should have fired earlier. Track the number of database references selected, uploads discovered, objects copied, checksum mismatches, and manifests committed. A zero-copy count for a tenant with active training data is actionable; a generic “job completed” counter is not. I have been paged for missed jobs and duplicate deliveries, so I treat a retry as a state transition with an idempotency key, not permission to create another backup.

Governance starts with a tenant recovery invariant

Start with a recovery contract. Define the tenant, the database snapshot identifier, the upload set, retention class, and the expected object count before copying anything. The manifest is the evidence package: source key, destination key, byte count, checksum, content type, tenant ID, run ID, and schema version. Write it after the dump and uploads have passed verification. No manifest means no approved restore.

Use a path that is easy to validate and hard to confuse:

backups/{tenant_id}/{run_id}/uploads/{object_id}

backups/{tenant_id}/{run_id}/database.dump

backups/{tenant_id}/{run_id}/manifest.json

User filenames remain metadata. They do not become authorization input or path components. The run ID should be a UTC timestamp plus random suffix; a retry for the same logical run reuses its idempotency key. That keeps duplicate deliveries visible without making the restore set ambiguous.

When the page fires, the on-call should be able to answer three questions from one run record: which tenant was selected, which snapshot was used, and which object failed verification? Log those fields with a correlation ID. The worker should stop before publishing the manifest when the tenant context is empty, the snapshot is not fixed, or a checksum differs.

The instrumentation change is small but important. Emit a manifest_committed event only after a read-back confirms the manifest checksum. Emit restore_verified only after a disposable restore checks database references against the manifest and compares representative artifact checksums. A threshold that pages on one transient copy retry creates noise; a threshold that ignores a tenant count mismatch hides a real recovery gap. Your mileage may vary by recovery objective, but the threshold must map to a decision an engineer can take.

Integration: how does a Node.js nightly scheduler hand off user uploads and a database dump?

The application may schedule in Node.js, while the copy worker stays behind a narrow interface so it can be tested without a particular object-storage SDK. The scheduler passes a tenant-scoped run specification and records the returned status.

package backup

import (
    "context"
    "crypto/sha256"
    "fmt"
)

type Upload struct {
    ID   string
    Data []byte
}

type Store interface {
    Put(ctx context.Context, key string, body []byte, checksum string) error
}

func Run(ctx context.Context, store Store, tenant, runID, dump string, uploads []Upload) error {
    if tenant == "" || runID == "" {
        return fmt.Errorf("tenant and run ID are required")
    }
    prefix := fmt.Sprintf("backups/%s/%s", tenant, runID)
    for _, upload := range uploads {
        sum := sha256.Sum256(upload.Data)
        key := fmt.Sprintf("%s/uploads/%s", prefix, upload.ID)
        if err := store.Put(ctx, key, upload.Data, fmt.Sprintf("%x", sum)); err != nil {
            return fmt.Errorf("upload %s: %w", upload.ID, err)
        }
    }
    dumpSum := sha256.Sum256([]byte(dump))
    if err := store.Put(ctx, prefix+"/database.dump", []byte(dump), fmt.Sprintf("%x", dumpSum)); err != nil {
        return fmt.Errorf("database dump: %w", err)
    }
    manifest := []byte(fmt.Sprintf("tenant=%s run=%s uploads=%d", tenant, runID, len(uploads)))
    manifestSum := sha256.Sum256(manifest)
    return store.Put(ctx, prefix+"/manifest.json", manifest, fmt.Sprintf("%x", manifestSum))
}
Enter fullscreen mode Exit fullscreen mode

This example leaves database snapshot creation and read-back verification to the caller because those boundaries differ by database engine. In production, the manifest records every upload rather than only a count. The important ordering is deterministic: validate identity, copy data, write the dump, then publish the manifest.

How do we evaluate restore verification for a tenant-scoped bucket prefix?

A prefix is a namespace, not an authorization policy. Give the backup worker a service identity that can write only to the backup bucket. During a restore, issue a short-lived identity limited to one tenant and one run prefix. Deny public access, require encryption, and separate the source upload bucket from the backup destination when the threat model calls for it. Object versioning or an immutable retention control can protect a committed run from accidental overwrite, but neither replaces tenant checks.

Restore into a disposable account or project first. Fetch and verify the manifest, reject any key outside the expected prefix, load the dump without production credentials, and then run tenant-scoped application smoke tests. Compare row references, object checksums, MIME metadata, and a sample of training artifacts. Record elapsed time, missing objects, and cleanup confirmation. A restore that only starts the database is partial.

The runbook I keep beside the alert is deliberately short:

  • select a run and record its age, tenant, snapshot ID, and retention class;
  • verify the manifest checksum and enumerate every referenced key;
  • restore the database and uploads into disposable resources;
  • run cross-tenant access tests with a tenant-scoped identity;
  • record mismatches, elapsed time, and cleanup evidence.

One failed restore should remain visible in metrics. I'm not sure a single “restore succeeded” counter tells an on-call engineer enough; separate manifest, checksum, and reference counts make the next action obvious.

Decision: when should this backup boundary change?

The trade-off is operational clarity versus capability. Tenant-scoped copies consume more storage than clever cross-tenant deduplication, but they are easier to audit. Retention also belongs in lifecycle policy, not in a filename: a 30-day hot window and a 180-day archive window must be reviewed against healthtech classification, legal holds, and deletion requests.

This approach is not suitable when you need millisecond replication, cross-region active-active writes, or a records platform with built-in legal discovery. Stick with a specialized replication or compliance system when those controls are mandatory. For a nightly recovery objective, explicit prefixes, immutable manifests, and repeatable restore drills give the on-call team evidence instead of hope.

References

Top comments (0)