Sub topic 14
Automating recurring exports
This chapter assumes you can write basic SOQL, have authorized the Salesforce CLI against an org alias, and are comfortable with shell scripts. You will turn a one-off export into a job that runs on a schedule and reports failures.
Why automate
A backup you run by hand gets skipped. Automation gives you a fixed cadence, which in turn defines your recovery point objective: if the job runs nightly, you can lose up to a day of changes.
A per-object export script
The script below writes one CSV per object into a dated folder. It uses set -euo pipefail, so a failed query stops the script instead of leaving a partial backup that looks complete.
#!/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"
doneTwo details to check before you rely on it:
Contacthas noNamefield you can query in every org configuration, so confirm each object's field list. A query error will stop the script, which is the behavior you want.- Real backups need more fields than
IdandName. Select the fields you would need for a restore.
Making it incremental
A full export of every object each night wastes API calls on records that have not changed. Filter on LastModifiedDate and store the time of the last successful run.
SELECT Id, Name, Industry, OwnerId, CreatedDate, LastModifiedDate
FROM Account
WHERE LastModifiedDate >= 2026-01-01T00:00:00Z
ORDER BY LastModifiedDateYour script replaces the date with the saved timestamp. Update the timestamp only after all queries succeed. An incremental export does not capture deletions, so keep a periodic full export alongside it.
Running it on a schedule
Any scheduler works. With cron, this entry runs the script at 02:00 every day and appends output to a log:
0 2 * * * /opt/backup/export.sh >> /var/log/sf-export.log 2>&1A CI job with a scheduled trigger does the same and gives you run history. In either case, the job needs to authenticate without a person present. Use a JWT-based authorization or an auth URL stored in your scheduler's secret store, never in the script.
The flow of a scheduled run
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Job works in terminal, fails in cron | Cron has a minimal PATH, so `sf` is not found | Use the full path to `sf` or set PATH in the script |
| Empty CSV with no error | Query returned zero rows, or output was redirected after a failure | Compare against `SELECT COUNT()` |
| Authentication fails after weeks | Session or refresh token expired | Use a connected app with JWT |
| Query fails on a large object | API row or timeout limits | Switch to a Bulk API 2.0 query job |
Exercise
Your nightly incremental export succeeds, but after a month a user asks you to restore a record that was deleted three weeks ago. Why might the backup not contain it, and what would you add?
Show answer
A query filtered on LastModifiedDate only returns records that still exist and have changed, so deletions are not captured. Add a periodic full export, and use queryAll (or Data Loader's Export All) to include deleted records.