I want to automatically back up Salesforce metadata by schedule execution, I ran ANT from the Windows task scheduler to create a tool to schedule automatic backups.
What you need is as follows. -Installation of Apache Ant -Installing Java JDK ・ Installation of Python3
Follow the link below for a detailed guide to releasing Salesforce using ANT. (There is also a description of how to install ANT and Java) · Release Changes Using Ant Migration Tool
Follow the link below to install Python. -Install Python3
myorg/
┣unpackaged
┃ ┗package.xml
┣build.properties
┣build.xml
┣modifyXML.py
┣backupByDateTime.bat
-Unpackaged / package.xml: Define the type of metadata you want to retrieve · Build.properties: Defines connection information to the Salesforce org.
build.properties
sf.username = [email protected]
sf.password = xxxyyyzzz
sf.serverurl = https://login.salesforce.com
sf.maxPoll = 20
-Build.xml: Defines the command to be executed by Ant.
build.xml
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns:sf="antlib:com.salesforce" name="Sample usage of Salesforce Ant tasks" default="test" basedir=".">
<property file="build.properties" />
<property environment="env" />
<condition property="sf.username" value=""> <not> <isset property="sf.username" /> </not> </condition>
<condition property="sf.password" value=""> <not> <isset property="sf.password" /> </not> </condition>
<condition property="sf.sessionId" value=""> <not> <isset property="sf.sessionId" /> </not> </condition>
<taskdef resource="com/salesforce/antlib.xml" uri="antlib:com.salesforce">
<classpath>
<pathelement location="../ant-salesforce.jar" />
</classpath>
</taskdef>
<target name="retrieveUnpackaged">
<mkdir dir="backupxxxxxxxxx" />
<sf:retrieve username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" retrieveTarget="backup0111163434" unpackaged="unpackaged/package.xml" />
</target>
</project>
-ModifyXML.py: A program that edits build.xml
modifyXML.py
import xml.etree.ElementTree as ET
import datetime
#Parse XML
tree = ET.parse('build.xml')
ET.register_namespace('sf', 'antlib:com.salesforce')
#Get XML
root = tree.getroot()
now = datetime.datetime.now()
dt_string = now.strftime('%m%d%H%M%S')
for e_mkdir in root.iter('mkdir'):
e_mkdir.set('dir', 'backup' + dt_string)
for e_ret in root.iter("{antlib:com.salesforce}retrieve"):
e_ret.set('retrieveTarget', 'backup' + dt_string)
#Save
tree.write('build.xml', 'UTF-8', True)
-BackupByDateTime.bat: A batch file that executes the above python and executes ANT commands.
backupByDateTime.bat
python modifyXML.py
Ant retrieveUnpackaged
Schedule the above batch file in windows task scheduler. Refer to here for the scheduler settings.
You can also open the above folder and double-click the batch file.
When executed, a folder named "backup execution date and time seconds" is created in the folder, and The metadata is stored in that folder.
Use it when you need to make regular backups from production, verification, and development environment in a Salesforce development project.
When I shared the creations within the company, there was a voice saying "Manage the acquired folders with Git", so if you have time, I would like to try that as well.
Recommended Posts