Sub topic 18
Building your backup plan
This chapter combines the earlier export methods into one repeatable plan. It assumes you can run SOQL queries, use the Salesforce CLI with an authorized org alias, and read a package.xml manifest. It also assumes you know the difference between record data and metadata.
Start from recovery goals
A plan is a set of decisions that you can check. Write down four of them before you choose tools.
- Scope: which objects, files, and metadata matter. Data and metadata are separate backups with separate tools.
- Recovery point objective (RPO): how much recent change you can afford to lose. A weekly export gives an RPO of up to seven days.
- Recovery time objective (RTO): how long a restore may take before the business is hurt.
- Retention: how long you keep each backup, and who can open it.
The Data Export Service alone gives you a weekly or monthly RPO, and it does not cover metadata. If the business needs a smaller RPO for key objects, add incremental SOQL exports. These filter on LastModifiedDate, as shown in the SOQL chapter.
Map each need to a method
| Need | Method | Notes |
|---|---|---|
| Full copy of all records and files | Data Export Service | Scheduled in Setup. Download the files before they expire. |
| Frequent export of chosen objects | Salesforce CLI on a scheduler | Scriptable, and you control the queries. |
| Very large objects | Bulk API 2.0 query jobs | Asynchronous, so it suits high volumes. |
| Configuration and code | `package.xml` retrieve into version control | Gives history and diffs. |
| Deleted records | `queryAll` or Data Loader Export All | Plain queries skip the Recycle Bin. |
| Small ad hoc extract | A report export | Not a backup. |
Layer these methods. A common baseline is a weekly Data Export Service run as the full copy, a nightly CLI export of high-change objects, and a metadata retrieve committed on every change or every night.
The flow of one backup cycle
The key point is the decision node. An export that has not been verified is not yet a backup.
A combined script
This script extends the earlier loop. It exports each object, compares the CSV row count with COUNT(), retrieves metadata, and records checksums. It needs jq and a prior sf org login.
#!/usr/bin/env bash
set -euo pipefail
ORG="my-prod-alias"
STAMP="$(date +%Y%m%d)"
DEST="backup/$STAMP"
mkdir -p "$DEST"
for OBJ in Account Contact Opportunity; do
sf data query \
--query "SELECT Id, Name FROM $OBJ" \
--result-format csv \
--target-org "$ORG" > "$DEST/$OBJ.csv"
EXPECTED="$(sf data query \
--query "SELECT COUNT() FROM $OBJ" \
--json \
--target-org "$ORG" | jq '.result.totalSize')"
ACTUAL="$(( $(wc -l < "$DEST/$OBJ.csv") - 1 ))"
if [ "$EXPECTED" -ne "$ACTUAL" ]; then
echo "Count mismatch for $OBJ: expected $EXPECTED, got $ACTUAL" >&2
exit 1
fi
done
sf project retrieve start \
--manifest manifest/package.xml \
--target-org "$ORG"
sha256sum "$DEST"/*.csv > "$DEST/SHA256SUMS"The script has three limits you should know about.
- Line counts:
wc -lcounts lines, not records. A text field containing line breaks inflates the count. For objects with long text fields, count rows with a CSV parser instead. - Counts that drift: records change while the job runs, so a busy org can show a small mismatch. Decide on a tolerance, or compare against a
LastModifiedDatecutoff. - Limited fields: the script exports only
IdandName. A real backup lists every field you need, because a CSV from a query holds only the columns you selected.
Common errors
Most backup failures come from a short list of mistakes.
- Backing up only records. After a bad deployment you also need flows, Apex, and object definitions, and these are not in the data export.
- Missing the download window. Data Export Service files stay available for a limited time. If nobody downloads them, the scheduled run was wasted.
- Silent failures. A scheduled job that fails and tells nobody looks identical to one that works. Send alerts on a non-zero exit code.
- Expired credentials. An integration user whose password or token lapses stops every run. Use a dedicated user and track its credential expiry.
- Exporting too few fields. Missing fields cannot be recovered from a backup that never had them. Review field lists after each schema change.
- Exports that break relationships. If you do not keep the original
Idvalues, you cannot rebuild lookups later. - Never restoring. A backup that has never been loaded back into a sandbox is an untested assumption.
Protect and retain
Exported CSV files hold customer data, often outside Salesforce's access controls. Treat them as production data.
- Store them in a location with restricted access and audit logs.
- Encrypt them at rest, and keep the keys apart from the files.
- Keep the checksum file with each backup so you can detect tampering or corruption.
- Set a retention rule, for example daily copies for 30 days, weekly for a quarter, and monthly for a year. Check it against privacy rules. If a customer asks you to erase their data, old backups may need a defined purge process.
- Keep at least one copy in a separate account or location from your main storage.
Rehearse the restore
Restore order follows the relationships between objects. Load parents before children: Account before Contact and Opportunity, and users and record types before the records that refer to them. Load the data into a sandbox, map the old Id values to the new ones, and check that lookups resolve. Time the whole exercise. The result is your measured RTO, and it often differs from the estimate you wrote down. Repeat the rehearsal at least twice a year and after major schema changes.
Document the plan
Keep one page that answers these questions:
- Who owns the backup, and who is the backup owner when that person is away?
- What runs, when, and where do the outputs go?
- What does a failure alert look like, and who acts on it?
- Where are the restore steps, and when were they last tested?
- Which gaps are accepted, for example "files are not covered by the nightly job"?
If the gaps or the volumes outgrow scripts, that is the point at which to weigh a managed backup product. Write down the trigger, such as an RPO under a few hours or a team that cannot maintain the scripts.
Exercise
Your business requires losing no more than 24 hours of changes to Opportunities, and the Data Export Service runs weekly. Which addition meets the requirement, and what must it do before you can call the result a backup?
Show answer
Add a nightly Salesforce CLI export (or a Bulk API 2.0 query for large volumes) of Opportunity, run from a scheduler, optionally filtered on LastModifiedDate for incremental runs. Before the output counts as a backup, the job must verify it, for example by comparing the CSV row count with SELECT COUNT() FROM Opportunity, and alert the owner on any mismatch or non-zero exit.
Exercise
Why should a restore rehearsal load Account records before Contact records, and what do you need to keep from the export to make lookups work?
Show answer
Contact has a lookup to Account, so the parent must exist before the child can reference it. You need the original Id values from the export. After loading the parents, you map each old Account Id to its new Id and write that into the Contact load file.