バックアップの課題(2):サイトファイルのバックアップ
バックアップ要件は次のとおりです。サイト内のすべてのファイルをバックアップします。バックアップの必要なサイトごとに、すべてのファイルを圧縮し、アーカイブをメールで安全な保管場所へ送信します。
まず、2つのターゲットを持つNAntスクリプトを作成しました。最初のターゲットはディレクトリの圧縮を行うもので、もう1つのターゲットはディレクトリを圧縮し、そのアーカイブをメールで送信するものです。
<?xml version="1.0"?> <project name="FileBackup" > <!--parameters baseDirectory This is the parent of the directory to be compressed targetDirectory This is the directory to be compressed backupDirectory This is the location to store the archives --> <target name="CompressDirectory"> <zip zipfile="${backupDirectory}${directoryName}.zip" > <fileset basedir="${baseDirectory}"> <include name="${ targetDirectory}/*" /> <include name="${targetDirectory }/**/*" /> </fileset> </zip> </target> <target name="CompressDirectoryAndEmail" depends="CompressDirectory"> <mail from="backup@anon.com" tolist="secure@anon.com" subject="${ targetDirectory} backup" mailhost="localhost"> <files> <include name="${backupDirectory}${ targetDirectory}.zip" /> </files> <attachments> <include name="${backupDirectory}${ targetDirectory}.zip" /> </attachments> </mail> <!-- The backup has be mailed so the file is no longer required. --> <delete file="${backupDirectory}${targetDirectory}.zip" /> </target> </project>
このファイルを「FileBackup.build」という名前で保存します。先のIISバックアップと同様、パラメータの設定が必要なので、このスクリプトを直接実行することはできません。別のスクリプトで、パラメータを設定し、バックアップ対象のサイトごとにバックアップポリシーを指定する必要があります。この例では、すべてのWebサイトが「C:\inetpub」というディレクトリに保存されているものと仮定します。もう1つ、「C:\backup」というディレクトリがあります。現在のところ、Webサイトはweb1、web2、web3の3つです。これらはそれぞれ「C:\inetpub\web1」「C:\inetpub\web2」「C:\inetpub\web3」に保存されています。web1の所有者はサイトを夜間バックアップし、アーカイブをサーバー上の「C:\backup」に保存することを要請しています。web2とweb3の所有者はサイトを夜間バックアップし、安全な場所にメールで送信することを要請していますが、サーバーにコピーを残すことは要請していません。これらの要件を満たすスクリプトは次のようになります。
<?xml version="1.0"?> <project name="BackupWebsites" default="Backup"> <!-- These two properties can be declared globally, because they are the same for all sites. --> <property name="baseDirectory" value="C:\inetpub\" /> <property name="backupDirectory" value="C:\backup\" /> <!-- This target exists only to specify which sites are to be backed up, and in what order. --> <target name="Backup" depends="BackupWeb1,BackupWeb2,BackupWeb3" /> <target name="BackupWeb1"> <property name="targetDirectory" value="web1" /> <nant buildfile="FileBackup.build" inheritall="true" target="CompressDirectory" /> </target> <target name="BackupWeb1"> <property name="targetDirectory" value="web2" /> <nant buildfile="FileBackup.build" inheritall="true" target="CompressDirectoryAndEmail" /> </target> <target name="BackupWeb1"> <property name="targetDirectory" value="web3" /> <nant buildfile="FileBackup.build" inheritall="true" target="CompressDirectoryAndEmail" /> </target> </project>
このスクリプトを「BackupWebsites.build」という名前で保存します。このバックアップを実行するためには、「FileBackup.build」と「BackupWebsites.build」を同じディレクトリに置いておく必要があります。コマンドプロンプトを開き、このディレクトリに移動して次のコマンドを実行します。
nant -buildfile:BackupWebsites.build
このスクリプトを実行すると、「web1.zip」というアーカイブが「C:\backup」に作られ、さらに「web2.zip」と「web3.zip」というアーカイブがメールでsecure@anon.comに送信されます。
