Sub topic 8
Exporting from the command line with the Salesforce CLI
This chapter assumes you can write a basic SOQL query, have a Salesforce org you can log in to, and are comfortable running commands in a terminal. You also need the Salesforce CLI (sf) installed and an org authorized under an alias. The examples use my-prod-alias. Replace it with your own alias.
Why use the CLI for exports
The Salesforce CLI runs a SOQL query and writes the result to your terminal. You can redirect that output to a file, so one command produces a CSV. Because it is a command, you can put it in a script and run it on a schedule. That makes it the natural step up from clicking through Data Loader.
Exporting one object
This command exports three fields from Account and saves them to accounts.csv.
sf data query \
--query "SELECT Id, Name, Industry FROM Account" \
--result-format csv \
--target-org my-prod-alias > accounts.csvEach part has a job:
--queryholds the SOQL statement. Select explicit fields, as you did in the SOQL chapter.--result-format csvproduces CSV. The default is a human-readable table, which is not suitable for a backup.--target-orgpicks the org by alias, so the command never depends on which org you used last.> accounts.csvis the shell redirect that writes the output to a file.
Exporting several objects with a script
A backup usually covers more than one object. This script loops over a list of objects and writes one CSV for each into a dated folder.
#!/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"
doneThe line set -euo pipefail makes the script stop at the first failure. Without it, a failed export could leave an empty file, and the next steps would carry on as if nothing was wrong.
The query selects only Id and Name, which keeps the example short. A real backup needs every field you would want back after a loss. Contact has no Name field you would restore from, for example, so check each object's fields before you rely on this list.
Common errors
- Selecting a child subquery. A query such as
SELECT Id, (SELECT Id FROM Tasks) FROM Contactreturns nested data that a flat CSV cannot hold. Export each child object with its own query and keep the parentIdas the link. - Redirecting error text into the file. The
>redirect captures standard output only. Error messages go to the terminal, so check the exit status or the file size, not just whether the file exists. - Using the wrong org. Always pass
--target-org. Running a backup against a sandbox by mistake gives you a backup of the wrong data. - Very large objects.
sf data queryis suited to moderate volumes. For millions of rows, use a Bulk API 2.0 query job, covered in a later chapter.
Exercise
Your backup script finishes without errors, but one CSV file is empty. Name two things in the script design that help you detect or prevent this.
Show answer
First, set -euo pipefail stops the script when a command fails, so a failed export does not go unnoticed. Second, compare the row count in each CSV with the result of SELECT COUNT() FROM <Object> (the verification step), and check the file size after each export.