[PYTHON] I made a tool to automatically back up the metadata of the Salesforce organization

This deliverable

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

Folder structure

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

Run

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.

Summary

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

I made a tool to automatically back up the metadata of the Salesforce organization
I made a tool to estimate the execution time of cron (+ PyPI debut)
I made a function to check the model of DCGAN
I made a tool to get the answer links of OpenAI Gym all at once
I made a tool to automatically generate a simple ER diagram from the CREATE TABLE statement
I made a calendar that automatically updates the distribution schedule of Vtuber
I made a tool to automatically browse multiple sites with Selenium (Python)
I made a program to check the size of a file in Python
I made a function to see the movement of a two-dimensional array (Python)
I made a tool to compile Hy natively
I made a tool to get new articles
I made a tool to notify Slack of Connpass events and made it Terraform
I made an appdo command to execute a command in the context of the app
I made a program to look up words on the window (previous development)
I made a script to record the active window using win32gui of Python
I made a command to markdown the table clipboard
I made a small donation to the non-profit organization "Open Source Robot Foundation" OSRF
I made a calendar that automatically updates the distribution schedule of Vtuber (Google Calendar edition)
A tool that automatically turns the gacha of a social game
I scraped the Organization member team and made a ranking
I made a tool to create a word cloud from wikipedia
I made a dot picture of the image of Irasutoya. (part1)
I made a dot picture of the image of Irasutoya. (part2)
[Titan Craft] I made a tool to summon a giant to Minecraft
I tried to automatically generate OGP of a blog made with Hugo with tcardgen made by Go
I made a program to solve (hint) Saizeriya's spot the difference
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
I made a slack bot that notifies me of the temperature
I made a scaffolding tool for the Python web framework Bottle
I made a command to display a colorful calendar in the terminal
I made a program that automatically calculates the zodiac with tkinter
[Kaggle] I made a collection of questions using the Titanic tutorial
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
[Django] I made a field to enter the date with 4 digit numbers
I made a kitchen timer to be displayed on the status bar!
I want to sort a list in the order of other lists
I tried to automatically extract the movements of PES players with software
I made a script to display emoji
I made a GAN with Keras, so I made a video of the learning process.
I made a library konoha that switches the tokenizer to a nice feeling
I made a tool to convert Jupyter py to ipynb with VS Code
I tried to get the RSS of the top song of the iTunes store automatically
I made a browser automatic stamping tool.
I made a mistake in fetching the hierarchy with MultiIndex of pandas
I tried to display the altitude value of DTM in a graph
A note I looked up to make a command line tool in Python
I tried to verify the result of A / B test by chi-square test
Python: I want to measure the processing time of a function neatly
[Python] I made a web scraping code that automatically acquires the news title and URL of Nikkei Inc.
The story of IPv6 address that I want to keep at a minimum
I made you to express the end of the IP address with L Chika
I made a tool to easily display data as a graph by GUI operation.
I failed to install django with pip, so a reminder of the solution
I tried to automatically generate a port management table from Config of L2SW
I want to set a life cycle in the task definition of ECS
I want to see a list of WebDAV files in the Requests module
I made a github action that notifies Slack of the visual regression test
I tried to create a model with the sample of Amazon SageMaker Autopilot
I tried to automatically send the literature of the new coronavirus to LINE with Python
How to calculate the volatility of a brand
I made a useful tool for Digital Ocean