Download the entire metadata contents of a Salesforce org as a backup or a source control commit.

This macrodef retrieves a snapshot of ALL components using vanilla Ant and the Force.com Migration Tool without the need for additional JAR dependencies.

Supports foldered metadata:

  • Reports
  • Documents
  • Dashboards
  • Email Templates

Usage:

First paste this macrodef into your build.xml

Then the macro may be called by any build target:

<target name="snapshot">
    <snapshot
        username="${sf.username}"
        password="${sf.password}"
        loginurl="${sf.serverurl}"
        outPath="src"
    />
</target>

How does it work?

First step is to identify the "shape" of the org. All applicable metadata types are discovered using sf:describeMetadata. Different orgs have different features, this describe works universally.

Second step is to iterate through each metadata type and list all components using sf:listMetadata. The output is used to assemble an explicit package definition / project manifest (the * wildcard does not work for all component types).

Third step is to retrieve the components!

Why is the first SOAP request traced?

It's faster to use an existing Session ID than log in to the Metadata API each time. By dumping the first API call to file, the script can extract a Session ID and use that for all subsequent calls. This saves 100+ round trips or handshakes.

How many components can it retrieve?

Maximum of 10,000 components, the salesforce package limit. If the organization exceeds this limit, you can exclude managed packages from the retrieve or exclude components. See under negate in the script.