Drupal Files Migration Script Using Phing

Tuesday, November 24, 2009 - 22:32

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?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.

Category: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

Comments

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.

Add new comment