Drupal Files Migration Script Using Phing

24th November 2009

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. drupal.destination.database.host=externalhost
  2. drupal.destination.database.database= drupaldatabase
  3. drupal.destination.database.password=password
  4. drupal.destination.database.username=username
  5. drupal.destination.database.prefix=dru_
  6.  
  7. drupal.destination.database.url=mysql://${drupal.destination.database.username}:${drupal.destination.database.password}@${drupal.destination.database.host}/${drupal.destination.database.database}
  8.  
  9. drupal.source.directory=D:/drupal/
  10. drupal.source.name=MyDrupalProject
  11.  
  12. drupal.destination.directory=D:/${drupal.source.name}/
  13. 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. <?xml version="1.0"??>
  2. <project name="DrupalExport" default="main">
  3. <!-- Include properties file. -->
  4. <property file="build.properties"></property>
  5.  
  6. <fileset dir="${drupal.source.directory}" id="DrupalFiles">
  7. <include name="cron.php"></include>
  8. <include name="index.php"></include>
  9. <include name="install.php"></include>
  10. <include name="robots.txt"></include>
  11. <include name="update.php"></include>
  12. <include name="xmlrpc.php"></include>
  13. <include name=".htaccess"></include>
  14. <include name="scripts/**"></include>
  15. <include name="themes/**"></include>
  16. <include name="misc/**"></include>
  17. <include name="modules/**"></include>
  18. <include name="profiles/**"></include>
  19. <include name="includes/**"></include>
  20. <include name="sites/all/**"></include>
  21. <include name="sites/default/files/**"></include>
  22. <include name="sites/default/default.settings.php"></include>
  23. </fileset>
  24.  
  25. <target name="prepare">
  26. <!-- Reset the permissions on the setttings.php file so we can delete it. -->
  27. <exec command="php -r "var_dump(chmod('${drupal.destination.directory}sites/default/settings.php', 0777));""></exec>
  28. <!-- Delete the destination directory. -->
  29. <delete dir="${drupal.destination.directory}"></delete>
  30. <echo>Prepare complete.</echo>
  31. </target>
  32.  
  33. <target name="copyFiles" depends="prepare">
  34. <copy todir="${drupal.destination.directory}" overwrite="false" tstamp="false">
  35. <fileset refid="DrupalFiles"></fileset>
  36. </copy>
  37. <echo>copyFiles complete</echo>
  38. </target>
  39.  
  40. <target name="drupalDatabaseConnection" depends="copyFiles">
  41. <copy file="${drupal.source.directory}/sites/default/default.settings.php" tofile="${drupal.destination.directory}/sites/default/settings.php" overwrite="true">
  42. <filterchain>
  43. <replaceregexp>
  44. <!-- Add some rules to replace settings with real values. -->
  45. <regexp pattern="\$db_url = 'mysql:\/\/username:password\@localhost\/databasename';" replace="\$db_url = '${drupal.destination.database.url}';" ignorecase="true"></regexp>
  46. <regexp pattern="\$db_prefix = '';" replace="\$db_prefix = '${drupal.destination.database.prefix}';" ignorecase="true"></regexp>
  47. </replaceregexp>
  48. </filterchain>
  49. </copy>
  50. <!-- Make settings.php readonly -->
  51. <exec command="php -r "var_dump(chmod('${drupal.destination.directory}sites/default/settings.php', 0444));""></exec>
  52. <echo>drupalDatabaseConnection complete</echo>
  53. </target>
  54.  
  55. <target name="zipFiles" depends="drupalDatabaseConnection">
  56. <fileset dir="${drupal.destination.directory}" id="DestinationFiles">
  57. <include name="**"></include>
  58. </fileset>
  59. <!-- Delete original file. -->
  60. <delete file="${drupal.destination.compressedfile}.zip"></delete>
  61. <zip destfile="${drupal.destination.compressedfile}.zip">
  62. <fileset refid="DestinationFiles"></fileset>
  63. </zip>
  64. <echo>zipFiles complete</echo>
  65. </target>
  66.  
  67. <target name="tarFiles">
  68. <fileset dir="${drupal.destination.directory}" id="DestinationFiles">
  69. <include name="**"></include>
  70. </fileset>
  71. <!-- Delete original file. -->
  72. <delete file="${drupal.destination.compressedfile}.tar"></delete>
  73. <tar destfile="${drupal.destination.compressedfile}.tar" compression="gzip">
  74. <fileset refid="DestinationFiles"></fileset>
  75. </tar>
  76. <echo>tarFiles complete</echo>
  77. </target>
  78.  
  79.  
  80. <target name="main">
  81. <phingcall target="copyFiles"></phingcall>
  82. <phingcall target="zipFiles"></phingcall>
  83. <phingcall target="tarFiles"></phingcall>
  84.  
  85. <echo>Export finished!</echo>
  86. </target>
  87. </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

Permalink
great post as usual .. thanks .. you just gave me a few more ideas to play with

forex robot (Wed, 12/09/2009 - 06:52)

Permalink
Very interesting your article. Thanks for the information.

Forex (Wed, 03/17/2010 - 12:39)

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.

Anonymous (Sun, 08/22/2010 - 20:08)

Permalink
It would be great if you could contribute the updated script that you made.

Wim Mostrey (Wed, 09/01/2010 - 12:27)

Add new comment

The content of this field is kept private and will not be shown publicly.