sql_backup.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2013
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. include "root.php";
  22. require_once "includes/require.php";
  23. require_once "includes/checkauth.php";
  24. if (permission_exists('sql_query_backup')) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. //add multi-lingual support
  32. require_once "app_languages.php";
  33. foreach($text as $key => $value) {
  34. $text[$key] = $value[$_SESSION['domain']['language']['code']];
  35. }
  36. //pdo database connection
  37. if (strlen($_REQUEST['id']) > 0) {
  38. require_once "sql_query_pdo.php";
  39. }
  40. //set the headers
  41. header('Content-type: application/octet-binary');
  42. header('Content-Disposition: attachment; filename=database_backup.sql');
  43. //get the list of tables
  44. if ($db_type == "sqlite") {
  45. $sql = "SELECT name FROM sqlite_master ";
  46. $sql .= "WHERE type='table' ";
  47. $sql .= "order by name;";
  48. }
  49. if ($db_type == "pgsql") {
  50. $sql = "select table_name as name ";
  51. $sql .= "from information_schema.tables ";
  52. $sql .= "where table_schema='public' ";
  53. $sql .= "and table_type='BASE TABLE' ";
  54. $sql .= "order by table_name ";
  55. }
  56. if ($db_type == "mysql") {
  57. $sql = "show tables";
  58. }
  59. $prep_statement = $db->prepare(check_sql($sql));
  60. $prep_statement->execute();
  61. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  62. foreach ($result as &$row) {
  63. $row = array_values($row);
  64. $table_name = $row[0];
  65. //get the table data
  66. $sql = "select * from $table_name";
  67. if (strlen($sql) > 0) {
  68. $prep_statement_2 = $db->prepare(check_sql($sql));
  69. if ($prep_statement_2) {
  70. $prep_statement_2->execute();
  71. $result2 = $prep_statement_2->fetchAll(PDO::FETCH_ASSOC);
  72. }
  73. else {
  74. echo "<b>".$text['label-error'].":</b>\n";
  75. echo "<pre>\n";
  76. print_r($db->errorInfo());
  77. echo "</pre>\n";
  78. }
  79. $x = 0;
  80. foreach ($result2[0] as $key => $value) {
  81. if ($row[$column] != "db") {
  82. $column_array[$x] = $key;
  83. $x++;
  84. }
  85. }
  86. $column_array_count = count($column_array);
  87. foreach ($result2 as &$row) {
  88. $sql = "INSERT INTO $table_name (";
  89. $x = 1;
  90. foreach ($column_array as $column) {
  91. if ($x < $column_array_count) {
  92. if (strlen($row[$column]) > 0) {
  93. $sql .= ''.$column.',';
  94. }
  95. }
  96. else {
  97. if (strlen($row[$column]) > 0) {
  98. $sql .= ''.$column.'';
  99. }
  100. }
  101. $x++;
  102. }
  103. $sql .= ") ";
  104. $sql .= "VALUES( ";
  105. $x = 1;
  106. foreach ($column_array as $column) {
  107. if ($x < $column_array_count) {
  108. if (strlen($row[$column])> 0) {
  109. $sql .= "'".check_str($row[$column])."',";
  110. }
  111. }
  112. else {
  113. if (strlen($row[$column])> 0) {
  114. $sql .= "'".check_str($row[$column])."'";
  115. }
  116. }
  117. $x++;
  118. }
  119. $sql .= ");\n";
  120. echo str_replace(",)", ")", $sql);
  121. }
  122. }
  123. unset($column_array);
  124. }
  125. ?>