Automate metadata backup of your Salesforce org with the help of ANT and GIT
When working with a team on a Project , the situation often arises that more than one person end up working on the same class and overwrites each other code. This could turn into a nightmare if you don’t have a backup of the org to refer to and merge the changes.
We faced the same situation in one of the project I was working on , and I took it upon myself to find a better way to take metadata backup rather than manually taking it in eclipse or any other IDE.While looking for a way for this I stumbled across an excellent post by Douglas C. Ayers Blog Post. This blog post is an extension of the same.
Java version 1.6 or higher
Part 1:- Setting up ANT
2) Login to your Salesforce org and go to the following address.
Salesforce migration tool to download Salesforce Migration Tool and extract it in a folder.
You will find a Salesforce.jar file in the extracted file. Copy it and paste it in the ‘lib’ folder of Ant home directory.
3) Set up an Environmental Variable for bin folder of your Ant directory , call it Path.
4) Check whether ANT is installed properly:
Open your command prompt and type ant –version to check the version of the ANT installed in your machine
5) Define Project Properties:
For this ANT uses 3 files ( build.properties, build.xml & package.xml) all 3 could be found in the sample folder of the folder extracted in step 2. Copy all three files in a separate folder in which you intend to take your code backup. Make appropriate changes in these files e.g : Your salesforce login and password, what components you want to extract etc.
Part 2: Create a project and Push to Git
1) Retrieve your metadata mentioned in package.xml by going to the folder in which you saved these files in command prompt and type the below command
2) The previous step will fetch the metadata from your Salesforce org to your local machine. Now you need to create a Git repository in which you have to push and commit this project. Refer the guide in the below link to check details on how to do it.
Part 3: Schedule your metadata retrieval.
1) You can write a shell script which will run on system startup to retrieve your code from salesforce org and pushing it to your Github repository.
2) Your shell script would look something like this :
git add .
git commit -am “Daily Sandbox backup”
git push -v -u
echo Git operation Done
Add the above file as .cmd
3) Go to your “Run” Dialog box by pressing windows and R keys together and type shell:startup.
A folder will open , In this folder paste your script file.
Now, whenever your system boots ANT will retrieve your metadata and then you will push it to your repository.