dump-database-d7.sh

Dumps a Drupal 7 database into a PHP script to test the upgrade process.

Run this script at the root of an existing Drupal 7 installation.

The output of this script is a PHP script that can be run inside Drupal 7 and recreates the Drupal 7 database as dumped. Transient data from cache, session, and watchdog tables are not recorded.

File

drupal/scripts/dump-database-d7.sh
View source
  1. #!/usr/bin/env php
  2. /**
  3. * @file
  4. * Dumps a Drupal 7 database into a PHP script to test the upgrade process.
  5. *
  6. * Run this script at the root of an existing Drupal 7 installation.
  7. *
  8. * The output of this script is a PHP script that can be run inside Drupal 7
  9. * and recreates the Drupal 7 database as dumped. Transient data from cache,
  10. * session, and watchdog tables are not recorded.
  11. */
  12. // Define default settings.
  13. define('DRUPAL_ROOT', getcwd());
  14. $cmd = 'index.php';
  15. $_SERVER['HTTP_HOST'] = 'default';
  16. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  17. $_SERVER['SERVER_SOFTWARE'] = NULL;
  18. $_SERVER['REQUEST_METHOD'] = 'GET';
  19. $_SERVER['QUERY_STRING'] = '';
  20. $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/';
  21. $_SERVER['HTTP_USER_AGENT'] = 'console';
  22. // Bootstrap Drupal.
  23. include_once './includes/bootstrap.inc';
  24. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  25. // Include the utility drupal_var_export() function.
  26. include_once dirname(__FILE__) . '/../includes/utility.inc';
  27. // Output the PHP header.
  28. $output = <<
  29. /**
  30. * @file
  31. * Filled installation of Drupal 7.0, for test purposes.
  32. *
  33. * This file was generated by the dump-database-d7.sh tool, from an
  34. * installation of Drupal 7, filled with data using the generate-d7-content.sh
  35. * tool. It has the following modules installed:
  36. ENDOFHEADER;
  37. foreach (module_list() as $module) {
  38. $output .= " * - $module\n";
  39. }
  40. $output .= " */\n\n";
  41. // Get the current schema, order it by table name.
  42. $schema = drupal_get_schema();
  43. ksort($schema);
  44. // Export all the tables in the schema.
  45. foreach ($schema as $table => $data) {
  46. // Remove descriptions to save time and code.
  47. unset($data['description']);
  48. foreach ($data['fields'] as &$field) {
  49. unset($field['description']);
  50. }
  51. // Dump the table structure.
  52. $output .= "db_create_table('" . $table . "', " . drupal_var_export($data) . ");\n";
  53. // Don't output values for those tables.
  54. if (substr($table, 0, 5) == 'cache' || $table == 'sessions' || $table == 'watchdog') {
  55. $output .= "\n";
  56. continue;
  57. }
  58. // Prepare the export of values.
  59. $result = db_query('SELECT * FROM {'. $table .'}', array(), array('fetch' => PDO::FETCH_ASSOC));
  60. $insert = '';
  61. foreach ($result as $record) {
  62. $insert .= '->values('. drupal_var_export($record) .")\n";
  63. }
  64. // Dump the values if there are some.
  65. if ($insert) {
  66. $output .= "db_insert('". $table . "')->fields(". drupal_var_export(array_keys($data['fields'])) .")\n";
  67. $output .= $insert;
  68. $output .= "->execute();\n";
  69. }
  70. $output .= "\n";
  71. }
  72. print $output;