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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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: backup.php 1582 2013-11-16 00:00:53Z christian_boltz $
  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. {
  58. umask (077);
  59. $path = (ini_get('upload_tmp_dir') != '') ? ini_get('upload_tmp_dir') : '/tmp';
  60. date_default_timezone_set(@date_default_timezone_get()); # Suppress date.timezone warnings
  61. $filename = "postfixadmin-" . date ("Ymd") . "-" . getmypid() . ".sql";
  62. $backup = $path . DIRECTORY_SEPARATOR . $filename;
  63. $header = "#\n# Postfix Admin $version\n# Date: " . date ("D M j G:i:s T Y") . "\n#\n";
  64. if (!$fh = fopen ($backup, 'w'))
  65. {
  66. flash_error("<div class=\"error_msg\">Cannot open file ($backup)</div>");
  67. $smarty->assign ('smarty_template', 'message');
  68. $smarty->display ('index.tpl');
  69. }
  70. else
  71. {
  72. fwrite ($fh, $header);
  73. $tables = array(
  74. 'admin',
  75. 'alias',
  76. 'alias_domain',
  77. 'config',
  78. 'domain',
  79. 'domain_admins',
  80. 'fetchmail',
  81. 'log',
  82. 'mailbox',
  83. 'quota',
  84. 'quota2',
  85. 'vacation',
  86. 'vacation_notification'
  87. );
  88. for ($i = 0 ; $i < sizeof ($tables) ; ++$i)
  89. {
  90. $result = db_query ("SHOW CREATE TABLE " . table_by_key($tables[$i]));
  91. if ($result['rows'] > 0)
  92. {
  93. while ($row = db_array ($result['result']))
  94. {
  95. fwrite ($fh, "$row[1];\n\n");
  96. }
  97. }
  98. }
  99. for ($i = 0 ; $i < sizeof ($tables) ; ++$i)
  100. {
  101. $result = db_query ("SELECT * FROM " . table_by_key($tables[$i]));
  102. if ($result['rows'] > 0)
  103. {
  104. while ($row = db_assoc ($result['result']))
  105. {
  106. $fields = array_keys($row);
  107. $values = array_values($row);
  108. $values = array_map('escape_string', $values);
  109. fwrite ($fh, "INSERT INTO ". $tables[$i] . " (". implode (',',$fields) . ") VALUES ('" . implode ('\',\'',$values) . "');\n");
  110. $fields = "";
  111. $values = "";
  112. }
  113. }
  114. }
  115. }
  116. header ("Content-Type: text/plain");
  117. header ("Content-Disposition: attachment; filename=\"$filename\"");
  118. header ("Content-Transfer-Encoding: binary");
  119. header ("Content-Length: " . filesize("$backup"));
  120. header ("Content-Description: Postfix Admin");
  121. $download_backup = fopen ("$backup", "r");
  122. unlink ("$backup");
  123. fpassthru ($download_backup);
  124. }
  125. /* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */
  126. ?>