PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
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.

131 lines
3.8 KiB

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