sql_backup.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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-2012
  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. //pdo database connection
  32. if (strlen($_REQUEST['id']) > 0) {
  33. require_once "v_sql_query_pdo.php";
  34. }
  35. //set the headers
  36. header('Content-type: application/octet-binary');
  37. header('Content-Disposition: attachment; filename=database_backup.sql');
  38. //get the list of tables
  39. if ($db_type == "sqlite") {
  40. $sql = "SELECT name FROM sqlite_master ";
  41. $sql .= "WHERE type='table' ";
  42. $sql .= "order by name;";
  43. }
  44. if ($db_type == "pgsql") {
  45. $sql = "select table_name as name ";
  46. $sql .= "from information_schema.tables ";
  47. $sql .= "where table_schema='public' ";
  48. $sql .= "and table_type='BASE TABLE' ";
  49. $sql .= "order by table_name ";
  50. }
  51. if ($db_type == "mysql") {
  52. $sql = "show tables";
  53. }
  54. $prep_statement = $db->prepare(check_sql($sql));
  55. $prep_statement->execute();
  56. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  57. foreach ($result as &$row) {
  58. $table_name = $row[0];
  59. //get the table data
  60. $sql = "select * from $table_name";
  61. if (strlen($sql) > 0) {
  62. $prep_statement_2 = $db->prepare(check_sql($sql));
  63. if ($prep_statement_2) {
  64. $prep_statement_2->execute();
  65. $result2 = $prep_statement_2->fetchAll(PDO::FETCH_ASSOC);
  66. }
  67. else {
  68. echo "<b>Error:</b>\n";
  69. echo "<pre>\n";
  70. print_r($db->errorInfo());
  71. echo "</pre>\n";
  72. }
  73. $x = 0;
  74. foreach ($result2[0] as $key => $value) {
  75. if ($row[$column] != "db") {
  76. $column_array[$x] = $key;
  77. $x++;
  78. }
  79. }
  80. $column_array_count = count($column_array);
  81. foreach ($result2 as &$row) {
  82. $sql = "INSERT INTO $table_name (";
  83. $x = 1;
  84. foreach ($column_array as $column) {
  85. if ($x < $column_array_count) {
  86. if (strlen($row[$column]) > 0) {
  87. $sql .= ''.$column.',';
  88. }
  89. }
  90. else {
  91. if (strlen($row[$column]) > 0) {
  92. $sql .= ''.$column.'';
  93. }
  94. }
  95. $x++;
  96. }
  97. $sql .= ") ";
  98. $sql .= "VALUES( ";
  99. $x = 1;
  100. foreach ($column_array as $column) {
  101. if ($x < $column_array_count) {
  102. if (strlen($row[$column])> 0) {
  103. $sql .= "'".check_str($row[$column])."',";
  104. }
  105. }
  106. else {
  107. if (strlen($row[$column])> 0) {
  108. $sql .= "'".check_str($row[$column])."'";
  109. }
  110. }
  111. $x++;
  112. }
  113. $sql .= ");\n";
  114. echo str_replace(",)", ")", $sql);
  115. }
  116. }
  117. unset($column_array);
  118. }
  119. ?>