Sub topic 18
Building your backup plan
This chapter assumes you have read the earlier chapters. You should know the export methods (Data Export Service, Data Loader, the Salesforce CLI, Bulk API 2.0), how to retrieve metadata with a manifest, and how to verify and secure exports. Here you combine them into one routine.
Start from recovery goals
A plan begins with two numbers for each kind of data.
- Recovery point objective (RPO) is how much recent data you can afford to lose. A daily export gives an RPO of up to 24 hours.
- Recovery time objective (RTO) is how long a restore may take before the business is hurt.
Records and metadata usually need different goals. Metadata changes when people deploy. Records change all day.
Map data to methods
| What | Method | Frequency |
|---|---|---|
| Full record set and files | Data Export Service | Weekly or monthly |
| Key objects, incremental | CLI script with a `LastModifiedDate` filter | Daily |
| Very large objects | Bulk API 2.0 query jobs | Daily or weekly |
| Metadata | `package.xml` retrieve, committed to Git | On every change, plus nightly |
The Data Export Service is your complete baseline. The scripted exports keep the gap between baselines small.
The routine as a flow
A script that checks itself
This script exports each object and fails loudly if the row count does not match a COUNT() query. It assumes the jq tool is installed. It also assumes the objects contain no multi-line field values. Long text fields can break the line-based count, so adjust the check for those objects.
#!/usr/bin/env bash
set -euo pipefail
ORG="my-prod-alias"
DEST="backup/$(date +%Y%m%d)"
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" \
--result-format 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 vs $ACTUAL" >&2
exit 1
fi
done
sf project retrieve start \
--manifest manifest/package.xml \
--target-org "$ORG"Records can change between the two queries, so a small difference on a busy org does not always mean a broken export. Decide on a tolerance and write it down.
Common mistakes
- Backing up only records. Without metadata, a restore has no custom fields to load into.
- Never testing a restore. An untested backup is a guess.
- Storing exports in the same place that can fail or be breached, with no access control.
- Keeping files forever. Set a retention period, and delete older copies on schedule.
Write the plan down
Record these items in one document:
- Objects covered, and the RPO and RTO for each.
- The method and schedule for each object.
- The person who owns each job and who gets alerts.
- Where files are stored, who can read them, and how long they are kept.
- Restore order for related objects, such as Accounts before Contacts.
- The date of the last restore rehearsal, and the date the next one is due.
Review the plan every quarter and after any large change to your objects.
Exercise
Your business accepts losing at most 4 hours of record changes. Your only backup is a weekly Data Export Service run. Does it meet the goal, and what would you add?
Show answer
No. A weekly export gives an RPO of up to a week. Add a scripted incremental export, run from a scheduler at least every 4 hours, that filters on LastModifiedDate. Keep the weekly export as the full baseline.
Exercise
Name three things the written backup plan should say about the stored files.
Show answer
Where they are stored, who can access them, and how long they are retained. Encryption is also a valid answer.