dump-database-d7.sh

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