LoginSignup
6
4

More than 3 years have passed since last update.

Salesforce組織のメタデータを自動バックアップするツール作ってみた

Last updated at Posted at 2020-01-11

今回の成果物

Salesforceのメタデータをスケジュール実行で自動でバックアップを取りたいと思い、
WindowsのタスクスケジューラーからANTを実行して、自動バックアップをスケジュールするツールを作ってみました。

必要なものは以下の通り。
・Apache Antのインストール
・Java JDKのインストール
・Python3のインストール

ANTを利用して、Salesforceのリリースを行う詳細なガイドは以下のリンクから。(ANT,Javaのインストール方法も記載あり)
Ant 移行ツールを使用した変更のリリース

Pythonのインストールは以下のリンクから。
Python3のインストール

フォルダ構成

myorg/
┣unpackaged
┃ ┗package.xml
┣build.properties
┣build.xml
┣modifyXML.py
┣backupByDateTime.bat

・unpackaged/package.xml:取得したいメタデータの種類を定義
・build.properties : Salesforce 組織への接続情報を定義。

build.properties
sf.username = xxxxxxxxx@xxx.co.jp
sf.password = xxxyyyzzz
sf.serverurl = https://login.salesforce.com
sf.maxPoll = 20

・build.xml: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:build.xmlを編集するプログラム

modifyXML.py
import xml.etree.ElementTree as ET
import datetime

# XMLを解析 
tree = ET.parse('build.xml')
ET.register_namespace('sf', 'antlib:com.salesforce')

# 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)

# 保存
tree.write('build.xml', 'UTF-8', True)

・backupByDateTime.bat:上記のpythonを実行して、ANTのコマンドを実行するバッチファイル。

backupByDateTime.bat
python modifyXML.py
Ant retrieveUnpackaged

実行

windowsタスクスケジューラーで上記のバッチファイルをスケジュールする。
スケジューラーの設定はこちらを参考に。

上記フォルダを開いて、バッチファイルをダブルクリックでも実行可能。

実行するとフォルダ内に「backup実行日時秒」という名前のフォルダを作成し、
そのフォルダ内にメタデータが格納されます。

まとめ

Salesforce開発プロジェクトで、定期的に本番、検証、開発環境からバックアップを取らなければいけない時などに活用してみてください。

社内で作成物を共有したときに、「取得してきたフォルダをGitで管理しろ」という声が上がったので、時間があればそちらも挑戦してみたいと思います。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4