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.
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
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
Leave a Reply