Sub topic 15
Protecting and retaining exported files
This chapter assumes you can already produce exports with the Data Export Service, Data Loader, or the Salesforce CLI. It also assumes you can use a Linux or macOS shell. The chapter covers what happens to the files after they leave the org: who can read them, how they are encrypted, and how long you keep them.
Why exported files are a risk
An export is a complete copy of data that Salesforce otherwise protects with profiles, permission sets, field-level security, sharing rules, and login auditing. A CSV file has none of that protection. Anyone who can open it sees every column. Names, emails, phone numbers, deal values, and free-text notes are all readable in plain text.
Three properties make backups attractive to attackers:
- They are concentrated. One file can hold every Contact in the org.
- They are often stored with weaker controls than production, such as a shared drive or a laptop folder.
- They get forgotten. A copy from three years ago still counts as personal data under privacy laws.
Treat each export as production data. Apply the same classification, the same access rules, and a defined end of life.
Limit who can create and read exports
Start at the source. Exports can only be as restricted as the account that creates them.
- Give the Data Export Service permission ("Weekly Data Export") only to the few administrators who run it. The service exports all data in the objects you select, so it is not limited by what an ordinary user can see.
- For scheduled CLI jobs, use a dedicated integration user with a purpose-built permission set. Grant read access to the objects in your backup and nothing else. Do not run unattended jobs under a named administrator's login.
- Store the CLI's credentials as secrets in your scheduler or CI system. Never commit an access token, an auth URL, or a private key to a repository.
- Downloads from the Data Export Service are available only for a limited time, currently 48 hours after the notification email. Download promptly, and don't forward the link.
Choose where files are stored
The destination should be private by default, access-logged, and separate from the systems the backup protects.
| Location | Fit | Main risk |
|---|---|---|
| Laptop or desktop folder | One-off exports only | Loss, theft, no audit trail |
| Shared network drive | Small teams | Broad read access, accidental sharing |
| Private object storage (for example S3 with a bucket policy) | Recurring backups | Misconfigured public access, long-lived keys |
| Git repository | Metadata only | Data committed by mistake stays in history |
Keep record data out of version control. Git history is permanent for practical purposes, and removing a file later does not remove it from earlier commits. Metadata in a Salesforce DX project is safe to commit, but scan it first. Some components, such as named credentials and certain settings, can reveal endpoints or configuration you would rather not publish.
Also keep at least one copy outside the Salesforce account's own trust boundary. If the same compromised credential can reach both the org and the backup, you have one failure domain.
Encrypt before upload
Encrypt in transit and at rest. Transport over HTTPS covers the first. For the second, you can rely on storage-side encryption, encrypt the file yourself, or do both. Encrypting the file yourself means a mistake in bucket permissions exposes only ciphertext.
The script below archives a day's export folder, encrypts it with GPG, records a checksum, uploads it, and deletes the plaintext. It assumes the public key for the recipient is in the local keyring and that the AWS CLI is configured.
#!/usr/bin/env bash
set -euo pipefail
STAMP="$(date +%Y%m%d)"
SRC="backup/$STAMP"
ARCHIVE="backup/$STAMP.tar.gz"
RECIPIENT="[email protected]"
BUCKET="s3://example-sf-backups/daily"
tar -czf "$ARCHIVE" -C backup "$STAMP"
gpg --batch --yes --trust-model always \
--encrypt --recipient "$RECIPIENT" \
--output "$ARCHIVE.gpg" "$ARCHIVE"
sha256sum "$ARCHIVE.gpg" > "$ARCHIVE.gpg.sha256"
aws s3 cp "$ARCHIVE.gpg" "$BUCKET/" --sse aws:kms
aws s3 cp "$ARCHIVE.gpg.sha256" "$BUCKET/" --sse aws:kms
rm -rf "$SRC" "$ARCHIVE"Only the public key needs to exist on the machine that runs the backup. Keep the private key somewhere else, because it is needed only at restore time. If you lose the private key, every encrypted backup becomes unreadable. Store it, or a recovery copy, in a vault and test decryption as part of your restore rehearsal.
The checksum lets you detect a corrupted or altered file later. Run sha256sum -c against the stored value before you trust a backup.
Define a retention policy
Retention answers two questions: how far back can you restore, and how long are you obliged or allowed to keep data. Pull these together from the recovery goals you set earlier, plus legal and contractual requirements.
A common layered scheme keeps many recent copies and few old ones:
- Daily copies for the last 30 days.
- Weekly copies for the last 3 months.
- Monthly copies for the last year.
Whatever scheme you pick, write it down with an owner and enforce it with automation. Manual cleanup is skipped the first busy week. For object storage, a lifecycle rule does this for you. This one expires objects under the daily/ prefix after 35 days.
{
"Rules": [
{
"ID": "expire-daily-backups",
"Status": "Enabled",
"Filter": { "Prefix": "daily/" },
"Expiration": { "Days": 35 }
}
]
}Apply it with aws s3api put-bucket-lifecycle-configuration --bucket example-sf-backups --lifecycle-configuration file://lifecycle.json. For local folders, a scheduled find backup -name '*.gpg' -mtime +30 -delete does the same job.
Deletion requests and backups
Retention has a conflict built in. If a customer asks you to erase their data, the record disappears from the org, but older backups still contain it. Regulations and your own policy decide how to handle this. Common approaches are to document that backups expire on a fixed schedule, to restrict restores so erased records are not reloaded, or to keep a list of erased Ids and remove them after any restore. Choose one deliberately before you need it.
How the pieces fit
Common mistakes
- Leaving the plaintext CSV on the machine that ran the export. Delete it as the last step of the script, and also on failure if your tooling leaves partial files.
- Using one long-lived storage key with write and delete rights. Backup jobs should be able to add files but not remove them, so ransomware or a stolen key cannot wipe your history.
- Never testing decryption. An encrypted backup you cannot decrypt is not a backup.
- Setting a retention period but never recording it, so no one knows whether a file may be deleted.
Exercise
A nightly script exports Account and Contact data to CSV and uploads the files to a private bucket. Name three changes that would improve how the files are protected and retained.
Show answer
Encrypt the files before upload, for example with GPG using a public key, and keep the private key in a separate vault. Run the job as a dedicated integration user with read-only access to the needed objects, and give the storage credential add-only rights. Add a lifecycle rule that expires copies after the documented retention period, and delete the local plaintext once the upload succeeds. Storing a checksum and testing decryption during restore rehearsals are also valid answers.