We will be using command line dataloader from Salesforce and ANT script to extract data from Salesforce and backup on FTP server. I would recommend to go through this article first to get familiar with basics of commandline dataloader and followed by this article to get some insight that how we can use ANT with dataloader.
There can be hundreds of use cases where we need to backup data from salesforce and we have thousands of options. We can use Jitterbit, Mulesoft, Dataloader.io or some big ETL tools like BOOMI or informatica. However sometimes clients are not willing to pay hefty amount on licensing cost of ETL tools and I love open source. In this article, we will be using open source solution to very common problem of backing up data on FTP server.
Complete code is available on my github account as well in case you need it.

Considering you know how to use command line dataloader, first challenge would be how to get it worked using ANT. Below code snippet show how we can create ANT macro to invoke dataloader to extract data.
<macrodef name="export_Account"> <sequential> <java classname="com.salesforce.dataloader.process.ProcessRunner" classpath="D:\Program Files (x86)\salesforce.com\Data Loader\dataloader-33.0.0-uber.jar" failonerror="true"> <sysproperty key="salesforce.config.dir" value="config"/> <arg line="process.name=Account"/> </java> </sequential> </macrodef>
Once data is extracted, we can copy csv file created by dataloader to ftp server using below code
<ftp server="${ftpServer}" port="${ftpPort}" userid="${ftpUserName}" password="${ftpPassword}" passive="yes" ` binary="no"> <fileset dir="${localFolder}"> <include name="**/*.csv"/> </fileset> </ftp>
parameters like “ftpServer”, “ftpPort” are coming from build.properties file. You can use FTP task from ANT to move local file to FTP server. Complete detail of capability and syntax of this task can be found on official site.
Complete code of build.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- @Author : Jitendra Zaa @Blog : https://jitendrazaa.com @Description : This ANT script depends on external dependency that can be downloaded from "http://commons.apache.org/proper/commons-net/download_net.cgi". : once downloaded, jar files can be added in any folder which should be available to Windows shell. : Some suggested path - ANT bin directory, Java lib directory. Or in any directive that is added in PATH env variable. : Getting started with Dataloader - https://www.jitendrazaa.com/blog/salesforce/tutorial-of-command-line-dataloader-salesforce/ : Dataloader and ANT - https://www.jitendrazaa.com/blog/salesforce/automate-command-line-dataloader-using-ant-with-dynamic-arguments-and-query/ --> <project name="Export" default="all"> <property file="build.properties"/> <property environment="env"/> <macrodef name="export_Account"> <sequential> <java classname="com.salesforce.dataloader.process.ProcessRunner" classpath="D:\Program Files (x86)\salesforce.com\Data Loader\dataloader-33.0.0-uber.jar" failonerror="true"> <sysproperty key="salesforce.config.dir" value="config"/> <arg line="process.name=Account"/> </java> </sequential> </macrodef> <target name="all"> <export_Account /> <ftp server="${ftpServer}" port="${ftpPort}" userid="${ftpUserName}" password="${ftpPassword}" passive="yes" ` binary="no"> <fileset dir="${localFolder}"> <include name="**/*.csv"/> </fileset> </ftp> </target> </project>
sample configuration settings in build.properties
ftpServer=YOUR FTP SERVER ftpPort=21 ftpUserName=USERNAME ftpPassword=FTP PASSWORD localFolder=C:\\Users\\Jitendra\\Desktop\\SFDC to FTP
Using SFTP with ANT
Sample ANT target
<target name="saveFiletoSFTP" description="Saving file produced by Dataloader to SFTP"> <scp file="sample.csv" sftp="true" trust="true" todir="userid:password@host:/to/dir/" /> </target>
Video Demo:
Command line dataloader related configurations – process-conf.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="Account" class="com.salesforce.dataloader.process.ProcessRunner" singleton="false"> <description>Export Account Data</description> <property name="name" value="Account"/> <property name="configOverrideMap"> <map> <entry key="sfdc.enableLastRunOutput" value="false"/> <entry key="sfdc.entity" value="Account"/> <entry key="process.operation" value="extract"/> <entry key="sfdc.extractionRequestSize" value="500"/> <entry key="sfdc.extractionSOQL" value="SELECT Id FROM Account"/> <entry key="dataAccess.name" value="C:\Users\Jitendra\Desktop\SFDC to FTP\AccountExport.csv"/> <entry key="dataAccess.type" value="csvWrite"/> <entry key="dataAccess.writeUTF8" value="true" /> </map> </property> </bean> </beans>
config.properties
sfdc.debugMessages=true process.encryptionKeyFile=C:\\Users\\Jitendra\\Desktop\\SFDC to FTP\\Key.txt sfdc.debugMessagesFile = C:\\Users\\Jitendra\\Desktop\\SFDC to FTP\\FTP.log sfdc.endpoint=https://login.salesforce.com sfdc.username=jitendra.zaa5.ftpAccount@gmail.com. sfdc.password=encryptedpasswordsdsdsdsdsd sfdc.loadBatchSize=200 sfdc.timeoutSecs=600
Leave a Reply