You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

backup.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Postfix Admin
  4. *
  5. * LICENSE
  6. * This source file is subject to the GPL license that is bundled with
  7. * this package in the file LICENSE.TXT.
  8. *
  9. * Further details on the project are available at http://postfixadmin.sf.net
  10. *
  11. * @version $Id$
  12. * @license GNU GPL v2 or later.
  13. *
  14. * File: backup.php
  15. * Used to save all settings - but only works for MySQL databases.
  16. * Template File: -none-
  17. *
  18. * Template Variables: -none-
  19. *
  20. * Form POST \ GET Variables: -none-
  21. */
  22. require_once('common.php');
  23. authentication_require_role('global-admin');
  24. (($CONF['backup'] == 'NO') ? header("Location: main.php") && exit : '1');
  25. // TODO: make backup supported for postgres
  26. if (db_pgsql()) {
  27. flash_error('Sorry: Backup is currently not supported for your DBMS ('.$CONF['database_type'].').');
  28. $smarty->assign('smarty_template', 'message');
  29. $smarty->display('index.tpl');
  30. die;
  31. }
  32. if (safeget('download') == "") {
  33. $smarty->assign('smarty_template', 'backupwarning');
  34. $smarty->display('index.tpl');
  35. die;
  36. }
  37. # Still here? Then let's create the database dump...
  38. /*
  39. SELECT attnum,attname,typname,atttypmod-4,attnotnull,atthasdef,adsrc
  40. AS def FROM pg_attribute,pg_class,pg_type,pg_attrdef
  41. WHERE pg_class.oid=attrelid AND pg_type.oid=atttypid
  42. AND attnum>0 AND pg_class.oid=adrelid AND adnum=attnum AND atthasdef='t' AND lower(relname)='admin'
  43. UNION SELECT attnum,attname,typname,atttypmod-4,attnotnull,atthasdef,''
  44. AS def FROM pg_attribute,pg_class,pg_type
  45. WHERE pg_class.oid=attrelid
  46. AND pg_type.oid=atttypid
  47. AND attnum>0
  48. AND atthasdef='f'
  49. AND lower(relname)='admin'
  50. $db = $_GET['db'];
  51. $cmd = "pg_dump -c -D -f /tix/miner/miner.sql -F p -N -U postgres $db";
  52. $res = `$cmd`;
  53. // Alternate: $res = shell_exec($cmd);
  54. echo $res;
  55. */
  56. if ($_SERVER['REQUEST_METHOD'] == "GET") {
  57. umask(077);
  58. $path = (ini_get('upload_tmp_dir') != '') ? ini_get('upload_tmp_dir') : '/tmp';
  59. date_default_timezone_set(@date_default_timezone_get()); # Suppress date.timezone warnings
  60. $filename = "postfixadmin-" . date("Ymd") . "-" . getmypid() . ".sql";
  61. $backup = $path . DIRECTORY_SEPARATOR . $filename;
  62. $header = "#\n# Postfix Admin $version\n# Date: " . date("D M j G:i:s T Y") . "\n#\n";
  63. if (!$fh = fopen($backup, 'w')) {
  64. flash_error("<div class=\"error_msg\">Cannot open file ($backup)</div>");
  65. $smarty->assign('smarty_template', 'message');
  66. $smarty->display('index.tpl');
  67. } else {
  68. fwrite($fh, $header);
  69. $tables = array(
  70. 'admin',
  71. 'alias',
  72. 'alias_domain',
  73. 'config',
  74. 'domain',
  75. 'domain_admins',
  76. 'fetchmail',
  77. 'log',
  78. 'mailbox',
  79. 'quota',
  80. 'quota2',
  81. 'vacation',
  82. 'vacation_notification'
  83. );
  84. for ($i = 0 ; $i < sizeof($tables) ; ++$i) {
  85. $result = db_query("SHOW CREATE TABLE " . table_by_key($tables[$i]));
  86. if ($result['rows'] > 0) {
  87. while ($row = db_array($result['result'])) {
  88. fwrite($fh, "$row[1];\n\n");
  89. }
  90. }
  91. }
  92. for ($i = 0 ; $i < sizeof($tables) ; ++$i) {
  93. $result = db_query("SELECT * FROM " . table_by_key($tables[$i]));
  94. if ($result['rows'] > 0) {
  95. while ($row = db_assoc($result['result'])) {
  96. $fields = array_keys($row);
  97. $values = array_values($row);
  98. $values = array_map('escape_string', $values);
  99. fwrite($fh, "INSERT INTO ". $tables[$i] . " (". implode(',', $fields) . ") VALUES ('" . implode('\',\'', $values) . "');\n");
  100. $fields = "";
  101. $values = "";
  102. }
  103. }
  104. }
  105. }
  106. header("Content-Type: text/plain");
  107. header("Content-Disposition: attachment; filename=\"$filename\"");
  108. header("Content-Transfer-Encoding: binary");
  109. header("Content-Length: " . filesize("$backup"));
  110. header("Content-Description: Postfix Admin");
  111. $download_backup = fopen("$backup", "r");
  112. unlink("$backup");
  113. fpassthru($download_backup);
  114. }
  115. /* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */