Sub topic 11
Backing Up Metadata with a Manifest
Records are only half of a Salesforce backup. Metadata describes how the org is built: custom objects and fields, Apex classes, flows, page layouts, profiles, and so on. If you lose it, restored records have nowhere correct to go. This chapter shows how to retrieve metadata with a package.xml manifest, store it in version control, and handle the problems that most often cause an incomplete backup.
Prerequisites
This chapter assumes you have:
- Installed the Salesforce CLI (
sf) and authorized an org with an alias such asmy-prod-alias. - A Salesforce DX project folder (created with
sf project generate) or an empty folder you can turn into one. - Basic Git knowledge:
init,add,commit, andpush. - An administrator profile in the org, or a user with the "Modify Metadata" permission.
Data backups and metadata backups differ
A data export copies rows. A metadata retrieve copies configuration, as text files, mostly XML. The two use different tools and different APIs, and they fail in different ways.
| Aspect | Data backup | Metadata backup |
|---|---|---|
| What it holds | Records (Accounts, Contacts, and so on) | Objects, fields, code, flows, layouts, permissions |
| Typical tool | Data Export Service, Data Loader, Bulk API | Salesforce CLI, Metadata API |
| Query language | SOQL | Manifest (`package.xml`) |
| Best storage | Encrypted file storage | Git repository |
| Changes | Constantly | In bursts, when someone edits configuration |
Because metadata is text, it works well with version control. You get history, diffs, and the ability to see who changed a flow and when.
How a manifest works
A manifest is an XML file named package.xml. It lists metadata types and, for each type, which components to include. The Metadata API reads the manifest and returns the matching components.
Here is a manifest that retrieves all custom objects, Apex classes, and flows:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>CustomObject</name>
</types>
<types>
<members>*</members>
<name>ApexClass</name>
</types>
<types>
<members>*</members>
<name>Flow</name>
</types>
<version>60.0</version>
</Package>Each part has a job:
<name>is the metadata type, such asCustomObject.<members>is either*(every component of that type) or a specific component name, such asAccount.<version>is the Metadata API version. Components and fields that a newer version adds will not appear if you pin an older one.
Retrieving into a DX project
Save the manifest as manifest/package.xml inside your project, then run the retrieve:
sf project retrieve start \
--manifest manifest/package.xml \
--target-org my-prod-aliasThe CLI converts the result into source format and writes it under your package directory, usually force-app/main/default. In source format, one custom object becomes a folder with a separate file for each field, which keeps Git diffs small and readable.
Wildcard gotchas
A wildcard looks like a complete backup, but it has limits. Know these before you rely on it.
- Standard objects.
CustomObjectwith*returns custom objects. For standard objects such as Account, it returns only the customizations if you also name them explicitly. ListAccount,Contact, and other standard objects by name in theCustomObjecttype to capture their custom fields. - Folder-based types. Reports, dashboards, documents, and email templates live in folders. A
*does not retrieve their contents. You must list each folder and each item inside it, for exampleSalesReports/Pipeline_Report. - Profiles and permission sets. A profile retrieved alone contains only the permissions for components in the same retrieve. To capture full permissions, retrieve profiles together with the objects, fields, and Apex classes they refer to.
- Types that do not support
*. Some types need explicit names. If a retrieve reports a type as unsupported or returns nothing for it, list the members by name. - Managed package components. Components that belong to an installed managed package are mostly not retrievable. Back up the package version you use, not its code.
Listing what exists in the org
To write a precise manifest, find out what components exist. The CLI can list them by type:
sf org list metadata \
--metadata-type ApexClass \
--target-org my-prod-aliasRun it for each type you care about and compare the output against what landed in your project folder. The CLI can also generate a manifest from existing source:
sf project generate manifest \
--source-dir force-app \
--name manifest/package.xmlThis is handy after you have retrieved once, because later runs can reuse a manifest that matches what you actually track.
Storing metadata in version control
Turn the project into a repository the first time, then commit after each retrieve:
git init
git add manifest force-app
git commit -m "Metadata backup 2026-11-30"For a routine, run the retrieve and commit together in a script:
#!/usr/bin/env bash
set -euo pipefail
ORG="my-prod-alias"
sf project retrieve start \
--manifest manifest/package.xml \
--target-org "$ORG"
git add manifest force-app
if git diff --cached --quiet; then
echo "No metadata changes."
else
git commit -m "Metadata backup $(date +%Y-%m-%d)"
git push
fiThe git diff --cached --quiet check means the repository only gets a commit when something changed. Your history then becomes a change log of the org's configuration.
A few storage rules apply:
- Use a private repository. Metadata can reveal your data model, business logic, and the names of internal systems.
- Never commit access tokens or auth files. The CLI stores credentials outside the project, but add any local credential files to
.gitignore. - Keep the repository separate from your data backups. Their access rules and retention needs differ.
Reading the diff
The biggest benefit of this approach is the diff. After a retrieve, git diff or git log -p shows what changed. If a validation rule was deleted, a field type was edited, or a flow was deactivated, the change appears as a changed line in a specific file. This is a faster way to answer "what changed in production last week" than reading the Setup Audit Trail.
What a metadata backup does not do
- It does not store records. A custom object's definition is in the backup, but the rows are not.
- It does not store everything about users. Some settings and org-level secrets do not come through the Metadata API.
- It does not restore itself. To restore, you deploy the files back with
sf project deploy start. Deployments validate against the target org, so dependencies must exist first, such as a field before a layout that uses it. - It may include items you want to exclude. Source tracking and
.forceignorelet you skip files you do not want in the repository.
Common errors
- Empty or partial results. The most common cause is a wildcard that does not cover the type, as described above. List components by name.
- API version mismatch. A manifest
<version>lower than the org's features means newer components are missed. Update the version when your org upgrades. - Insufficient permissions. If a component is missing, the backup user may lack access to it. Use a dedicated integration user with "Modify Metadata" or "Modify All Data".
- Retrieve size limits. A single retrieve has limits on the number of files and total size. If it fails, split the manifest into several files, for example one per type group, and run them in sequence.
- Case and spelling errors. Type names are case sensitive.
Customobjectfails;CustomObjectworks.
Verifying a metadata backup
Check three things after each run:
- The retrieve output lists the counts of components retrieved, and none is flagged as an error.
- The component counts in
sf org list metadatafor key types match the file counts in your project folder. - A spot check: open one recently changed component, such as a flow, and confirm the saved version matches the org.
Do this manually at first. Once the manifest stabilizes, add a count comparison to your script.
Exercise
Your manifest uses <members>*</members> for the Report type, but the retrieved project contains no reports. Why, and how do you fix it?
Show answer
Reports are a folder-based type. A wildcard does not retrieve items inside folders. List each report folder and each report by its full name in the manifest, for example SalesReports/Pipeline_Report, and retrieve again.
Exercise
A scheduled script runs sf project retrieve start daily and commits the result. Last week an admin deleted a validation rule in production. How do you find when it was removed and restore it?
Show answer
Run git log -p -- <path to the validation rule file> to see the commit in which the file disappeared. Check out the file from the commit before that one, then deploy it to the org with sf project deploy start --source-dir <path>, with its dependencies, such as the object and fields it uses, already in place.