Sub topic 16
Rehearsing a restore
This chapter assumes you can export records with SOQL and the Salesforce CLI, you know the parent and child relationships between standard objects, and you have a sandbox you can overwrite. It also assumes you have an export set from earlier chapters. A backup you have never restored is only a guess. A rehearsal turns it into something you can measure.
What a rehearsal proves
A rehearsal answers four questions:
- Can the files be read and loaded? Encoding, column names, and field types can all break a load.
- Do relationships survive? Child records need valid parent references.
- How long does it take? This is your real recovery time, which you compare with the recovery time objective (RTO) you set earlier.
- Do the steps work for someone else? Someone other than the person who wrote the backup script should be able to run them.
Run a rehearsal after you first build the backup. Run it again after any schema change that adds required fields or validation rules. Otherwise run it on a schedule, such as quarterly.
Choose a safe target
Never rehearse in production. Use a full or partial copy sandbox, or a scratch org or Developer Edition org for a small sample. Check these points before you start:
- Email deliverability is set to "System email only" or "No access", so that restoring Contacts does not email real people.
- Triggers, flows, and validation rules are reviewed. A restore usually should not fire automation that sends messages, creates tasks, or recalculates fields.
- The sandbox has enough data storage. A restore needs room for the full export.
The restore sequence
Metadata goes back first, because records cannot load into fields that do not exist. Then data loads from parents to children.
Step 1: Deploy metadata
Deploy the project you retrieved with package.xml into the sandbox:
sf project deploy start \
--source-dir force-app \
--target-org my-sandbox-aliasCommon failures here are missing dependencies, such as a profile that references a field you did not retrieve, and Apex tests that fail without data. Write every failure down. Each one is a gap in the metadata backup.
Step 2: Load parents before children
Load an object only after every object it references. A typical order for a small org is:
- Users, or a mapping from old owner Ids to new ones
- Account
- Contact
- Opportunity
- Tasks and other activities
Record Ids from production do not carry over to a different org. Salesforce assigns new Ids on insert. To keep relationships intact, add a custom text field on each object, marked External ID and Unique, such as Legacy_Id__c, and fill it with the old Id. Include it in your exports. Then load with an upsert on that field:
sf data upsert bulk \
--sobject Account \
--file backup/20260801/Account.csv \
--external-id Legacy_Id__c \
--target-org my-sandbox-alias \
--wait 30For child objects, the CSV needs a column that points at the parent by its external ID. For a Contact, the header is Account.Legacy_Id__c, with the old Account Id as the value. Alternatively, load the parents first, query the new Ids, and rewrite the child CSVs. That works with any tool but adds a step.
Time the restore
Record the start and end time of every step. Then compare the total with your RTO. A simple wrapper does the job:
#!/usr/bin/env bash
set -euo pipefail
ORG="my-sandbox-alias"
SRC="backup/20260801"
START=$(date +%s)
for OBJ in Account Contact Opportunity; do
STEP=$(date +%s)
sf data upsert bulk \
--sobject "$OBJ" \
--file "$SRC/$OBJ.csv" \
--external-id Legacy_Id__c \
--target-org "$ORG" \
--wait 60
echo "$OBJ loaded in $(( $(date +%s) - STEP )) seconds"
done
echo "Total: $(( $(date +%s) - START )) seconds"If the total exceeds your RTO, you have three choices. Load fewer objects first and restore the rest later, split large files and run loads in parallel within API limits, or change the RTO to match reality. Document whichever you choose.
Verify the result
After the load, check three things.
Counts. Compare the row count in each CSV with the record count in the org:
sf data query \
--query "SELECT COUNT() FROM Account" \
--target-org my-sandbox-alias \
--json | jq '.result.totalSize'Count CSV records with a CSV parser, not wc -l. A field with a line break inside quotes spans several lines but is one record.
import csv
import sys
path = sys.argv[1]
expected = int(sys.argv[2])
with open(path, newline="", encoding="utf-8") as f:
rows = sum(1 for _ in csv.DictReader(f))
print(f"{path}: {rows} rows, org reports {expected}")
sys.exit(0 if rows == expected else 1)Run it as python check_count.py backup/20260801/Account.csv 1250, with the count from the org in place of 1250. A nonzero exit code means the numbers differ.
Relationships. Spot-check that children point at the right parents. This query lists Contacts that lost their Account:
SELECT COUNT() FROM Contact WHERE AccountId = nullCompare the result with the same count in the source CSV. Some Contacts legitimately have no Account, so the two numbers should match, not be zero.
Content. Open ten to twenty records in the UI and compare field values with the CSV. Check text with accents or non-Latin characters, long text areas, currency, and date and time values. Time zone shifts are a common silent error.
What to write down
Keep a short results record after each rehearsal:
| Item | Example entry |
|---|---|
| Date and backup used | 2026-08-01 export |
| Total restore time | 2 h 40 min against a 4 h RTO |
| Records that failed | 38 Contacts, validation rule on Phone |
| Gaps found | Profile references a missing field |
| Fixes made | Added the field to `package.xml` |
Each rehearsal should leave the backup, the scripts, or the runbook better than before. If you find nothing wrong, check that you looked hard enough.
Exercise
During a rehearsal, 40 Contacts fail to load, and the error says the Account reference could not be resolved. The Account load reported success. Give two likely causes and how you would check each.
Show answer
First, the Contact CSV may reference old Account Ids that were never loaded, for example because the Account export was filtered or incomplete. Check that every Account.Legacy_Id__c value in the Contact file exists in the Account file's Legacy_Id__c column. Second, some Account rows may have been skipped or failed during the Account load even though the job finished. Check the failed-results file from the Account job and compare the Account count in the org with the CSV row count. A third possibility is that the Contact column header does not match the external ID field name.