Sub topic 16
Rehearsing a restore
This chapter assumes you already have CSV exports and a metadata retrieve from earlier chapters. It also assumes you know which objects depend on which, as covered in the chapter on restore order. You need a sandbox where you can load data safely.
Why rehearse
A backup you have never restored is an unproven guess. Typical failures only show up during a restore:
- A required field or validation rule rejects rows.
- A flow or trigger fires on insert and changes the data.
- Salesforce assigns new Ids, so child rows lose their parents.
- The restore takes far longer than your recovery time objective (RTO).
Prepare the target
Use a sandbox, never production, for the rehearsal. Restore in this order:
- Deploy the metadata backup, so the objects and fields exist.
- Turn off or plan for automation that would alter loaded data, such as flows, triggers, and validation rules.
- Load parent objects, then children.
Keep relationships intact
Salesforce does not let you insert a record with a chosen Id. Restored records get new Ids, so old foreign keys such as AccountId no longer point anywhere. The usual fix is an external ID field, for example a custom Legacy_Id__c on Account. Fill it with the original Id, then let child rows refer to the parent through it. In the Contact CSV, the column header Account.Legacy_Id__c holds the old Account Id.
Load in order
This script upserts parents and then children into a sandbox. The file names assume you prepared the CSVs with the external ID columns.
#!/usr/bin/env bash
set -euo pipefail
ORG="my-sandbox-alias"
sf data upsert bulk \
--sobject Account \
--file restore/Account.csv \
--external-id Legacy_Id__c \
--wait 10 \
--target-org "$ORG"
sf data upsert bulk \
--sobject Contact \
--file restore/Contact.csv \
--external-id Legacy_Id__c \
--wait 10 \
--target-org "$ORG"Upsert is useful for rehearsals because you can rerun the script after fixing a problem without creating duplicates.
Check the result
After each load, compare the sandbox with your export. Run a count in the sandbox and compare it with the row count of the CSV, excluding the header line.
SELECT COUNT() FROM ContactThen spot-check relationships. Open a few Contacts and confirm each one shows the correct Account.
Record what you learn
Keep a short runbook with the exact commands, the order, the errors you hit, and the total time. Repeat the rehearsal on a schedule, and again after you add objects or fields.
Exercise
A restored Contact CSV loads without errors, but every Contact has an empty Account. What is the most likely cause, and how do you fix it?
Show answer
The CSV still holds the old Account Ids, which do not exist in the target org. Store each original Account Id in an external ID field such as LegacyIdc, load Accounts first, and use a column like Account.LegacyId__c in the Contact file.