Drupal Files Migration Script Using Phing

The other day I needed to copy a Drupal project from my source folder to another folder, so rather than manually copy the files I decided to create a Phing script that would do it for me in one go. This Phing script will export your Drupal project into another directory, change the database credentials and create zip and tar files of the project. The first thing to do is create a properties, here is the contents of that file.

drupal.destination.database.database= drupaldatabase

Just save this file as build.properties and change the properties to your needed settings.

The build file part of the the contains a fileset to include all of the needed files and then some targets to do things with those files. Those of you used to Phing and Drupal will notice that we are including a lot of files but we are missing one called settings.php. This is deliberate for the reason that trying to match the parameters in our build.properties file into the database settings in the settings.php file is going to be tricky. So rather than enter multiple database parameters we can just use the destination parameters, the default.settings.php file (which we will know the format) and a small amount of regular expression replacing in order to create a new settings.php file for our destination. Here is the contents of the build file.

<?xml version="1.0"?>
<project name="DrupalExport" default="main">
    <!-- Include properties file. -->
    <property file="build.properties" />
    <fileset dir="${drupal.source.directory}" id="DrupalFiles">
        <include name="cron.php" />
        <include name="index.php" />
        <include name="install.php" />
        <include name="robots.txt" />
        <include name="update.php" />
        <include name="xmlrpc.php" />
        <include name=".htaccess" />
        <include name="scripts/**" />
        <include name="themes/**" />
        <include name="misc/**" />
        <include name="modules/**" />
        <include name="profiles/**" />
        <include name="includes/**" />
        <include name="sites/all/**" />
        <include name="sites/default/files/**" />        
        <include name="sites/default/default.settings.php" />
    <target name="prepare">
        <!-- Reset the permissions on the setttings.php file so we can delete it. -->
        <exec command="php -r &quot;var_dump(chmod('${drupal.destination.directory}sites/default/settings.php', 0777));&quot;" />      
        <!-- Delete the destination directory. -->
        <delete dir="${drupal.destination.directory}" />
        <echo>Prepare complete.</echo>
    <target name="copyFiles" depends="prepare">
        <copy todir="${drupal.destination.directory}" overwrite="false" tstamp="false">
            <fileset refid="DrupalFiles" />
        <echo>copyFiles complete</echo>
    <target name="drupalDatabaseConnection" depends="copyFiles">
        <copy file="${drupal.source.directory}/sites/default/default.settings.php"
                    <!-- Add some rules to replace settings with real values. -->
                    <regexp pattern="\$db_url = 'mysql:\/\/username:password\@localhost\/databasename';" replace="\$db_url = '${drupal.destination.database.url}';"  ignoreCase="true"/>
                    <regexp pattern="\$db_prefix = '';" replace="\$db_prefix = '${drupal.destination.database.prefix}';" ignoreCase="true"/>
        <!-- Make settings.php readonly -->
        <exec command="php -r &quot;var_dump(chmod('${drupal.destination.directory}sites/default/settings.php', 0444));&quot;" />          
        <echo>drupalDatabaseConnection complete</echo>
    <target name="zipFiles" depends="drupalDatabaseConnection">
        <fileset dir="${drupal.destination.directory}" id="DestinationFiles">
            <include name="**" />
        <!-- Delete original file. -->
        <delete file="${drupal.destination.compressedfile}.zip" />        
        <zip destfile="${drupal.destination.compressedfile}.zip">
            <fileset refid="DestinationFiles" />
        <echo>zipFiles complete</echo>    
    <target name="tarFiles">
        <fileset dir="${drupal.destination.directory}" id="DestinationFiles">
            <include name="**" />
        <!-- Delete original file. -->        
        <delete file="${drupal.destination.compressedfile}.tar" />          
        <tar destfile="${drupal.destination.compressedfile}.tar" compression="gzip">
            <fileset refid="DestinationFiles" />
        <echo>tarFiles complete</echo>    
    <target name="main">
        <phingcall target="copyFiles" />
        <phingcall target="zipFiles" />
        <phingcall target="tarFiles" />
        <echo>Export finished!</echo>

To run this script save this xml into a file called drupalexport.xml use the following command to run it.

phing -f drupalexport.xml

You will now have a directory containing your Drupal project, as well as a zip and a tar file with the same files in them in the directory above.


great post as usual .. thanks .. you just gave me a few more ideas to play with
Very interesting your article. Thanks for the information.
That's pretty insane. You've not taken into account any support for multiple sites, you're migrating Drupal core as well as just the necessary site files, and you're not looking at creating Apache Vhosts or migrating databases or creating databases. It was a good start though, and has inspired me to create a phing build script which does all these things.
It would be great if you could contribute the updated script that you made.

Add new comment

The content of this field is kept private and will not be shown publicly.
5 + 9 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.