Resources /
Blog

How to Run a Salesforce Metadata Backup with the Salesforce CLI

Submit your details to get a book

7
Min Read
Resources /
Blog

How to Run a Salesforce Metadata Backup with the Salesforce CLI

Download

Submit your details to get a book

7
Min Read
Cloud with servers linked to database and document tiles, illustrating a Salesforce metadata backup
Cloud with servers linked to database and document tiles, illustrating a Salesforce metadata backup

A Salesforce metadata backup is a dated copy of your Salesforce org's configuration: objects, fields, flows, Apex and permission sets. You build one with the CLI, package.xml and git, and you restore pieces of it by deploying them.

It's written for admins and developers who are at home in a terminal and want a backup they can run tonight. If the case for backing up metadata separately isn't settled yet, read our post on metadata backup first and come back for the commands.

What do the five steps of a Salesforce backup look like?

There are five steps, and each one is a single command or a short file. The table shows what each produces; the code is in the sections that follow.

StepWhat you runWhat you get
1. List the metadataA package.xml manifest, written by hand or generated from the orgOne file naming every metadata type to back up
2. Retrieve itsf project retrieve start --manifestSource-format files for each component in your project
3. Commit itgit add and git commitA dated version you can diff against any other date
4. Schedule itA GitHub Actions schedule or a cron jobA backup every night without anyone remembering
5. Restore itsf project deploy start, with --dry-run firstThe old version of a component back in the org

What setup do you need before you start?

You need the Salesforce CLI, git and a Salesforce login. The CLI installs through npm, as its GitHub repo shows, and retrieve has to run inside a project, which takes one command to create.

Terminal
npm install -g @salesforce/cli
sf project generate --name org-backup
cd org-backup
sf org login web --alias prod

That login opens a browser so you can sign in once from your machine. Pick a login with permission to read all the metadata that should be backed up, then run git init in the folder.

Step 1: Which objects and types go in the package.xml?

The package.xml tells the Metadata API retrieve which metadata types and members to pull. A wildcard (*) grabs every component of a type, but the Metadata API guide notes it doesn't grab all standard objects.

XML: package.xml
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>*</members>
        <members>Account</members>
        <members>Contact</members>
        <members>Opportunity</members>
        <name>CustomObject</name>
    </types>
    <types><members>*</members><name>ApexClass</name></types>
    <types><members>*</members><name>ApexTrigger</name></types>
    <types><members>*</members><name>Flow</name></types>
    <types><members>*</members><name>Layout</name></types>
    <types><members>*</members><name>PermissionSet</name></types>
    <version>68.0</version>
</Package>

That's why Account, Contact and Opportunity are named one by one above. Listing a standard object under CustomObject returns its custom and customizable standard fields, while system ones like CreatedById stay out.

Version 68.0 is the Winter '27 API. The full type list is on the Metadata Types page, with plain-English notes in our guide to common metadata types.

You don't have to maintain the list manually, since the CLI can build the manifest from what's in the org. Add --excluded-metadata for any types you want left out.

Terminal
sf project generate manifest --from-org prod --output-dir manifest

Step 2: How do you retrieve metadata with the Salesforce CLI?

Run sf project retrieve start with the manifest and your org alias. Files land in force-app in source format, the command's default.

Terminal
rm -rf force-app/main
sf project retrieve start --manifest manifest/package.xml --target-org prod

That first line wipes last night's copy. If someone deleted a field in Salesforce, it then shows up as a deleted file in git.

Salesforce production orgs never allow source tracking, so a full retrieve each time is the reliable way to catch every change. Our package.xml walkthrough shows the same retrieve for one type.

Step 3: How do you commit each backup to git?

Commit the retrieved files with the date in the message. Every commit is a restore point, and git log lists each day something changed.

Terminal
git add force-app manifest
git commit -m "Metadata backup $(date -u +%F)"
git push

To see changes and deletions between backups, diff two commits. That's often the quickest way to date a validation rule change, and it's the history version control is built for.

Step 4: How do you schedule the backup?

A scheduled GitHub Actions workflow can handle the retrieve and commit each night. The workflow below uses the same commands as steps 2 and 3.

A CI job can't open a browser, so it logs in with an SFDX auth URL stored as a repository secret. Running sf org display --target-org prod --verbose prints that URL after a web login, and the CLI docs warn that it contains sensitive information.

YAML: GitHub Actions workflow
name: metadata-backup
on:
  schedule:
    - cron: "17 2 * * *"
  workflow_dispatch:
permissions:
  contents: write
jobs:
  backup:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - run: npm install -g @salesforce/cli
      - run: |
          echo "$SFDX_AUTH_URL" > auth.txt
          sf org login sfdx-url --sfdx-url-file auth.txt --alias prod
          rm auth.txt
        env:
          SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
      - run: |
          rm -rf force-app/main
          sf project retrieve start --manifest manifest/package.xml --target-org prod
          git config user.name "metadata-backup"
          git config user.email "metadata-backup@users.noreply.github.com"
          git add force-app manifest
          git commit -m "Metadata backup $(date -u +%F)" || echo "No changes"
          git push

GitHub schedules run in UTC by default and can lag at the top of the hour, which is why this one fires at 2:17. Public repos disable schedules after 60 inactive days; keep backup repos private.

Nothing here is tied to GitHub, and the same commands work in any CI tool or a server cron job. If you want a login that doesn't hang off one person's session, the CLI supports JWT login with a connected app and certificate.

Step 5: How do you restore metadata from a backup?

Find the last good version in git, check out that file and deploy it. Do a --dry-run first, which validates and runs tests without saving anything.

Terminal
git checkout <commit> -- force-app/main/default/objects/Account/fields/Region__c.field-meta.xml
sf project deploy start --source-dir force-app/main/default/objects/Account/fields/Region__c.field-meta.xml --target-org prod --dry-run
sf project deploy start --source-dir force-app/main/default/objects/Account/fields/Region__c.field-meta.xml --target-org prod

Apex restores to production run RunLocalTests by default, because production deploys need 75% org-wide coverage. If components were added since the backup, pass a destructive changes manifest via --pre-destructive-changes or --post-destructive-changes to remove them.

A field redeployed from git comes back empty, because metadata defines the field and not what's in it (only Undelete from Deleted Fields, within 15 days, brings the values back). The values need a data backup, per our backup and restore guide, so rehearse restoring in a sandbox first.

What can't the CLI back up from Salesforce orgs?

The Metadata API doesn't reach all of Setup, and Salesforce's unsupported metadata types page points to the Metadata Coverage report for the missing types. Those types can't be retrieved or deployed, so each org needs manual changes.

What you might expect in the backupBacked up by this method?What to do instead
Custom objects, fields, flows, Apex, layouts, permission setsYes, if listed in package.xmlKeep the manifest current, or regenerate it from the org
Standard objectsOnly when named one by oneAdd each standard object you configure under CustomObject
Unsupported metadata typesNoCheck the Metadata Coverage report and document those settings by hand
Record dataNoRun a data backup on its own schedule
Files and attachmentsNoInclude them in the data backup

Change sets copy metadata between production and its sandboxes, and a sandbox refresh copies production's metadata across. Neither keeps a dated history to restore from, so neither is a backup.

Should you script it yourself or use a Salesforce backup solution?

All the script needs is the CLI, git and a CI runner. It suits a single org where one person owns the job.

That owner has to fix the job when a login expires and add new types to the manifest. They also have to keep the auth URL secure and encrypt backups that leave the org.

A backup solution handles data, files and the restore itself as well; our buyer's guide lists what to compare.

Either way, fit it into your backup and recovery strategy and your disaster recovery plan. See our post on preventing data loss for the controls that keep you from needing a restore.

Where does Flosum fit?

Flosum Backup & Archive backs up metadata, files and attachments alongside your records. Restores go to a point in time at field, record or full-org level.

Flosum Backup & Archive runs in Flosum's cloud or in your own cloud.

On the release side, the Flosum DevOps Cloud App can roll back a deployment in one click. Our post on what a backup should cover shows how data and metadata backups fit together, and our metadata backup post covers the gaps in native tools.

FAQs

How do I back up Salesforce metadata?

List the metadata types in a package.xml, run sf project retrieve start --manifest package.xml --target-org with your alias, and commit the result to git. Then schedule those commands to run nightly in CI.

What is the Salesforce CLI command to retrieve metadata?

sf project retrieve start. Use --manifest for a package.xml, --metadata for named components such as ApexClass:MyClass, and --target-org for the org, as listed in the CLI reference.

Can package.xml use a wildcard for standard objects?

No. A wildcard under CustomObject retrieves all custom objects, but the Metadata API guide says each standard object must be named.

Does a Salesforce data export include metadata?

No. Data Export produces CSV files of your records and doesn't include metadata, so metadata needs its own backup through the Metadata API.

How do I restore metadata from a git backup?

Check out the old file version and run sf project deploy start --source-dir on it, with --dry-run first. A restored field gets its definition back, but its data doesn't come with it.

Which metadata types can't be backed up with the Metadata API?

Salesforce's Unsupported Metadata Types page points to the Metadata Coverage report for the full list. Those types are changed by hand in each org, so write those changes down.

Book a demo to see data and metadata restored together in Flosum Backup & Archive.

Table Of Contents
Author
Stay Up-to-Date
Get flosum.com news in your inbox.

Thank you for subscribing

Ship Salesforce changes faster
Teams on Flosum report 40% higher development velocity and 75%
fewer merge conflicts.
Free to start  |  No credit card required