Automated Daily Backup of Salesforce Using ANT Migration Tool and GIT

In few previous articles, I have talked about how to use “ANT Migration tool in Salesforce” and “How to Use EGit plugin in Eclipse to work with Git“. So to make this article short, I assume that you are already familiar with ANT Migration tool provided by Salesforce and Git.

During project development life cycle it is very essential to backup your code and Configurations daily. However sometimes developers forgot to commit changes in Git, may be  because of workload. It may create an issue if someone wants to check back history of code for that duration, there will be no use of having code repository in place if it cant help us to get out of problem.

In this article, I will move one step further and explain how to setup automated script which will run on your System startup and commit backup of your Salesforce organization into local Git repository without any manual interference.

Salesforce Automated Script for Data Backup Using CommandLine
Salesforce Automated Script for Data Backup Using CommandLine

Step1 : Get your Salesforce project into Eclipse, If possible select every Metadata components. This step will give us “package.xml” file.

Step 2 : Set up your EGit in Eclipse, It is explained here in very detail. This step will generate “.git” folder in your system which will have all information about your local and remote repository.

Step 3 : Create “build.properties” file, may be one folder above your Source code folder.

# build.properties
# Specify the login credentials for the desired Salesforce organization
sf.username = YOUR_SFDC_USERNAME
sf.password = YOUR_SFDC_PASSWORD+SECURITYTOKEN
# Use 'https://login.salesforce.com' for production or developer edition (the default if not specified).
# Use 'https://test.salesforce.com for sandbox.
sf.serverurl = https://test.salesforce.com

#Below String is used as a comment while pushing Backup into Git
gt.commitMessage = Daily Data Backup using Automated ANT Script

#Directory where Git Configuration is created by Egit. "../ denotes one folder above current folder"
gt.Directory = ../git/Shivasoft-sandbox

You can read in detail about ANT configurations here.

Step 4 : ANT Does not have any command for Git. So we have to write macro for ANT which will execute Git executable file from your operating System.

	<macrodef name="git">
		<attribute name="command" />
		<attribute name="dir" default="${gt.Directory}" />
		<element name="args" optional="true" />
		<sequential>
			<echo message="git @{command}" />
			<exec executable="git" dir="@{dir}">
				<arg value="@{command}" />
				<args/>
			</exec>
		</sequential>
	</macrodef>

We will add above code snippet in “build.xml” file of ANT Migration tool. Complete “build.xml” file will look like :

<project name="Shivasoft Sandbox" default="deployCode" basedir="." xmlns:sf="antlib:com.salesforce">
    <property file="build.properties"/>
    <property environment="env"/>

	<target name="SFDCFetch">
	  <!-- Retrieve the contents into another directory -->
	  <sf:retrieve username="${sf.username}"
				   password="${sf.password}"
				   serverurl="${sf.serverurl}"
				   retrieveTarget="RELATIVE PATH OF SRC FOLDER WHICH IS FETCHED BY ECLIPSE"
				   unpackaged="RELATIVE PATH OF PACKAGE.XML FILE" />
	</target>

	<macrodef name="git">
		<attribute name="command" />
		<attribute name="dir" default="${gt.Directory}" />
		<element name="args" optional="true" />
		<sequential>
			<echo message="git @{command}" />
			<exec executable="git" dir="@{dir}">
				<arg value="@{command}" />
				<args/>
			</exec>
		</sequential>
	</macrodef>

	<target name="GitOperations" description="Commits all changes to version git" depends="SFDCFetch">

		<echo message="Commiting all changes with message ${gt.commitMessage}" /> 

		<git command="add">
			<args>
				<arg value="." />
			</args>
		</git>

		<git command="commit">
			<args>
				<arg value="-am ${gt.commitMessage}" />
			</args>
		</git>
	</target>
</project>

As per above file, target “GitOperations” depends on “SFDCFetch”. So first metadata information will be fetched into folder specified in attribute “retrieveTarget” of tag “sf:retrieve”. After that target “GitOperation” will be executed.
As shown in above file, “GitOperations” uses “git” macro defined which eventually invokes Git executable file and passes required argument.

To run above file, we simple have to use command “ant GitOperations”.
However, If we want to run above files automatically, we need to create “Command/Batch” file in Windows or “Shell” file in linux.

Step 5 : Create Command file in Windowst by name “Script.cmd”. This file needs to save in same folder where “build.properties” and “build.xml” exists.

@echo OFF
echo Data Backup for Shivasoft Sandbox
ant GitOperations
pause
Salesforce Automated Script for Data Backup
Salesforce Automated Script for Data Backup

Step 6 : Add Shortcut of above command file into Windows startup folder. And at this step we achieved our target. Whenever you will start you System, this executable file will execute.
I hope you believe on Energy saving and shutdown your system everyday before leaving office 🙂 . However if you doesn’t shutdown or Log-Off your system, You may need to check for “Scheduling Task” to run above command file once in 24 hours.

Note : We have committed Data into Local Git repository only. Whenever you open Eclipse, you can use “Push to Remote” option in EGit. I tried to push automatically in remote repository however I run into issue where my Push Command in ANT hanged, I tried a lot but no luck. If anyone of you get lucky, please let me know.

Update (2-Nov-2014):
If you want to save log of Git operations daily, then we can pipe output of all operations to text file.
Assume, Command for “ant git operation” is saved in some batch file named “AntCommand.cmd”.

@echo OFF
echo Data Backup in Progress, Please wait
AntCommand.cmd > logs/"%date%".log 2>&1
echo Backup Complete
"logs/"%date%".log"

sample content of “AntCommand.cmd”

@echo OFF
echo Data Backup for all sandboxes
ant GitOperations

Related posts

13 thoughts on “Automated Daily Backup of Salesforce Using ANT Migration Tool and GIT”

  1. Hi Jitendra,

    First of all Thank you for a really helpful and productive article.

    I have tried the above thing as you have mentioned, but I am getting the below error. Can you please help me solve it.

    Execute failed: java.io.IOException: Cannot run program “git” (in directory “C:Program Files (x86)Gitbin”): CreateProcess error=2, The system cannot find the file specified

    THank you in advance.

  2. Hi Jitendra,

    May I know if the translations that we do are confined to just the namespace that we have in the managed package? If No, then may I know how can we restrict them to one Namespace?

    Thank you for the help in advance.

    Kalyan

  3. Hi jitendra, I ve been following your log since long. Thank you for all the knowledge you share with people. I have a question regarding integration of TFS and salesforce ANT tool. Can you give me a brief idea about it?

  4. Hi,

    Firstly Thanks for the awesome article.

    I tried for the same in one of my POC. However, i am stuck at one place.After configuring everything and invoking the command “ant GitOperations” in command prompt.

    I get the below error “D:Salesforce_Ant_37.0salesforce_ant_37.0samplebuild.xml:21: The directory you specified does not exist”.

    My .git folder or local repository location is : D:GITGITDEMO_1.git
    The gt directory value i have given is : gt.Directory = D:GITGITDEMO_1

    In build.xml, I have given :

    I have tried changing values of retrieveTarget,unpackaged and gt.Directory. But didnt get lucky till now.Can you please guide me in this regard. I guess, i am doing something wrong in these 3 variables.

    Thanks in advance.

  5. Hi Jitendra

    I managed to push the new branch to Git.

    I set up ant to retrieve all the code into git repo folder, and run window command to push(instead of pushing by using Ant).

    Here is the sample i use:

    call ant retrieveSource -DgtDirectory=c:yourgitrepo

    cd c:yourgitrepo

    git checkout -b newbranch
    git commit -am prod back up at
    git push –set-upstream origin newbranch

    Hope this help
    Shang

Leave a Reply