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.host=externalhost
drupal.destination.database.database= drupaldatabase
drupal.destination.database.password=password
drupal.destination.database.username=username
drupal.destination.database.prefix=dru_
 
drupal.destination.database.url=mysql://${drupal.destination.database.username}:${drupal.destination.database.password}@${drupal.destination.database.host}/${drupal.destination.database.database}
 
drupal.source.directory=D:/drupal/
drupal.source.name=MyDrupalProject
 
drupal.destination.directory=D:/${drupal.source.name}/
drupal.destination.compressedfile=${drupal.source.name}

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" />
    </fileset>
 
    <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>
    
    <target name="copyFiles" depends="prepare">
        <copy todir="${drupal.destination.directory}" overwrite="false" tstamp="false">
            <fileset refid="DrupalFiles" />
        </copy>
        <echo>copyFiles complete</echo>
    </target>
 
    <target name="drupalDatabaseConnection" depends="copyFiles">
        <copy file="${drupal.source.directory}/sites/default/default.settings.php"
            tofile="${drupal.destination.directory}/sites/default/settings.php"
            overwrite="true">
            <filterchain>            
                <replaceregexp>
                    <!-- 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"/>
                </replaceregexp>
            </filterchain>         
        </copy>
        <!-- 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>
      
    <target name="zipFiles" depends="drupalDatabaseConnection">
        <fileset dir="${drupal.destination.directory}" id="DestinationFiles">
            <include name="**" />
        </fileset>
        <!-- Delete original file. -->
        <delete file="${drupal.destination.compressedfile}.zip" />        
        <zip destfile="${drupal.destination.compressedfile}.zip">
            <fileset refid="DestinationFiles" />
        </zip>
        <echo>zipFiles complete</echo>    
    </target>
    
    <target name="tarFiles">
        <fileset dir="${drupal.destination.directory}" id="DestinationFiles">
            <include name="**" />
        </fileset>
        <!-- Delete original file. -->        
        <delete file="${drupal.destination.compressedfile}.tar" />          
        <tar destfile="${drupal.destination.compressedfile}.tar" compression="gzip">
            <fileset refid="DestinationFiles" />
        </tar>
        <echo>tarFiles complete</echo>    
    </target>    
 
    
    <target name="main">
        <phingcall target="copyFiles" />
        <phingcall target="zipFiles" />
        <phingcall target="tarFiles" />
 
        <echo>Export finished!</echo>
    </target> 
</project>

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.

Comments

great post as usual .. thanks .. you just gave me a few more ideas to play with
Permalink
Very interesting your article. Thanks for the information.
Permalink
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.
Permalink
It would be great if you could contribute the updated script that you made.
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
17 + 3 =
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.