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.

137 lines
4.1 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. $smarty->assign ('tMessage', '<p>Sorry: Backup is currently not supported for your DBMS ('.$CONF['database_type'].').</p>');
  30. $smarty->assign ('smarty_template', 'message');
  31. $smarty->display ('index.tpl');
  32. // print '<p>Sorry: Backup is currently not supported for your DBMS.</p>';
  33. die;
  34. }
  35. /*
  36. SELECT attnum,attname,typname,atttypmod-4,attnotnull,atthasdef,adsrc
  37. AS def FROM pg_attribute,pg_class,pg_type,pg_attrdef
  38. WHERE pg_class.oid=attrelid AND pg_type.oid=atttypid
  39. AND attnum>0 AND pg_class.oid=adrelid AND adnum=attnum AND atthasdef='t' AND lower(relname)='admin'
  40. UNION SELECT attnum,attname,typname,atttypmod-4,attnotnull,atthasdef,''
  41. AS def FROM pg_attribute,pg_class,pg_type
  42. WHERE pg_class.oid=attrelid
  43. AND pg_type.oid=atttypid
  44. AND attnum>0
  45. AND atthasdef='f'
  46. AND lower(relname)='admin'
  47. $db = $_GET['db'];
  48. $cmd = "pg_dump -c -D -f /tix/miner/miner.sql -F p -N -U postgres $db";
  49. $res = `$cmd`;
  50. // Alternate: $res = shell_exec($cmd);
  51. echo $res;
  52. */
  53. if ($_SERVER['REQUEST_METHOD'] == "GET")
  54. {
  55. umask (077);
  56. $path = (ini_get('upload_tmp_dir') != '') ? ini_get('upload_tmp_dir') : '/tmp';
  57. $filename = "postfixadmin-" . date ("Ymd") . "-" . getmypid() . ".sql";
  58. $backup = $path . DIRECTORY_SEPARATOR . $filename;
  59. $header = "#\n# Postfix Admin $version\n# Date: " . date ("D M j G:i:s T Y") . "\n#\n";
  60. if (!$fh = fopen ($backup, 'w'))
  61. {
  62. $tMessage = "<div class=\"error_msg\">Cannot open file ($backup)</div>";
  63. $smarty->assign ('tMessage', $tMessage);
  64. $smarty->assign ('smarty_template', 'message');
  65. $smarty->display ('index.tpl');
  66. // include ("templates/message.php");
  67. }
  68. else
  69. {
  70. fwrite ($fh, $header);
  71. $tables = array(
  72. 'admin',
  73. 'alias',
  74. 'alias_domain',
  75. 'config',
  76. 'domain',
  77. 'domain_admins',
  78. 'fetchmail',
  79. 'log',
  80. 'mailbox',
  81. 'quota',
  82. 'quota2',
  83. 'vacation',
  84. 'vacation_notification'
  85. );
  86. for ($i = 0 ; $i < sizeof ($tables) ; ++$i)
  87. {
  88. $result = db_query ("SHOW CREATE TABLE " . table_by_key($tables[$i]));
  89. if ($result['rows'] > 0)
  90. {
  91. while ($row = db_array ($result['result']))
  92. {
  93. fwrite ($fh, "$row[1];\n\n");
  94. }
  95. }
  96. }
  97. for ($i = 0 ; $i < sizeof ($tables) ; ++$i)
  98. {
  99. $result = db_query ("SELECT * FROM " . table_by_key($tables[$i]));
  100. if ($result['rows'] > 0)
  101. {
  102. while ($row = db_assoc ($result['result']))
  103. {
  104. foreach ($row as $key=>$val)
  105. {
  106. $fields[] = $key;
  107. $values[] = $val;
  108. }
  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. ?>