Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. <?php
  2. /** @description: README.md
  3.  
  4. # Summary
  5. Dumps individual MySQL table structure and data files, organized into directories by database name.
  6. This makes it easier to keep track of updates (via GIT) and/or restore individual tables.
  7.  
  8. Example file structure:
  9. |_ mysql_dump.php
  10. |_ database1
  11. |_ table1-structure.sql
  12. |_ table1-data.sql
  13. |_ database2
  14. |_ table1-structure.sql
  15. |_ table1-data.sql
  16.  
  17. # Setup
  18. 1. Create a MySQL user with SELECT priveleges to your desired backup databases
  19. 2. Place this file in a main dump directory (e.g. /home/root/database-backups/)
  20. 3. Provide MySQL credentials below
  21. 4. Optionally specify a different directory to save the files in `$db_dump_dir`
  22. 5. Schedule a cron job or manually run this file on the command line
  23.  
  24. # Additional
  25. * Setup a GIT repository to track changes to the database(s)
  26.  
  27. eof: README.md */
  28.  
  29. /**
  30. * @description
  31. * Configure database credentials
  32. */
  33. define('DB_SERVER', 'localhost');
  34. define('DB_SERVER_USERNAME', 'user');
  35. define('DB_SERVER_PASSWORD', 'pass');
  36. /**
  37. * or use a config file:
  38. require('/path/to/mysql.conf.php');
  39. */
  40.  
  41. /**
  42. * @description
  43. * Optionally set a different directory to save the backups.
  44. * By default, it will save in this directory.
  45. */
  46. $db_dump_dir = dirname(__FILE__) . '/';
  47.  
  48. /**
  49. * @description
  50. * Connect to the database as priveleged user with limited access (READ-only)
  51. */
  52. $db = new mysqli(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD);
  53.  
  54. $sql = "
  55. SELECT
  56. `TABLE_SCHEMA`,
  57. `TABLE_NAME`
  58. FROM `information_schema`.`TABLES`
  59. WHERE `TABLE_SCHEMA` NOT IN ('information_schema')
  60. ORDER BY
  61. `TABLE_SCHEMA` ASC,
  62. `TABLE_NAME` ASC ";
  63. $result = $db->query($sql);
  64.  
  65. while ($row = $result->fetch_assoc($result)) {
  66. $table_schema = $row['TABLE_SCHEMA'];
  67. $table_name = $row['TABLE_NAME'];
  68.  
  69. /**
  70. * @description
  71. * Create directories by database name
  72. */
  73. $db_dir = $db_dump_dir . $table_schema . '/';
  74. if ( !is_dir($db_dir) ) {
  75. mkdir($db_dir);
  76. chmod($db_dir, 0777);
  77. }
  78.  
  79. /**
  80. * @description
  81. * Dump filenames with postfixes for table structure vs. data
  82. */
  83. $table_structure_filename = $db_dir . $row['TABLE_NAME'] . '-structure.sql';
  84. $table_data_filename = $db_dir . $row['TABLE_NAME'] . '-data.sql';
  85.  
  86. /**
  87. * @description
  88. * Dump the table schema
  89. */
  90. $table_structure = "mysqldump " .
  91. "--compact --skip-opt --add-drop-table --no-data " .
  92. "-u " . DB_SERVER_USERNAME . " --password=" . DB_SERVER_PASSWORD . " " .
  93. " " . $table_schema . " " . $table_name .
  94. " > " . $table_structure_filename;
  95. shell_exec($table_structure);
  96.  
  97. /**
  98. * @description
  99. * Dump the table data
  100. */
  101. $table_data = "mysqldump ".
  102. "--compact --complete-insert --no-create-info --order-by-primary " .
  103. "-u " . DB_SERVER_USERNAME . " --password=" . DB_SERVER_PASSWORD . " " .
  104. " " . $table_schema . " " . $table_name .
  105. " > " . $table_data_filename;
  106. shell_exec($table_data);
  107. }
  108.  
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement