home *** CD-ROM | disk | FTP | other *** search
/ Exame Informatica 139 / Exame Informatica 139.iso / Revista / Flash / Uniform Server / diskw / home / admin / www / phpMyBackupPro / backup.php next >
Encoding:
PHP Script  |  2005-07-21  |  11.5 KB  |  258 lines

  1. <?php
  2. /*
  3.  +--------------------------------------------------------------------------+
  4.  | phpMyBackupPro                                                           |
  5.  +--------------------------------------------------------------------------+
  6.  | Copyright (c) 2004-2005 by Dirk Randhahn                                 |
  7.  | http://www.phpMyBackupPro.net                                            |
  8.  | version information can be found in definitions.php.                     |
  9.  |                                                                          |
  10.  | This program is free software; you can redistribute it and/or            |
  11.  | modify it under the terms of the GNU General Public License              |
  12.  | as published by the Free Software Foundation; either version 2           |
  13.  | of the License, or (at your option) any later version.                   |
  14.  |                                                                          |
  15.  | This program is distributed in the hope that it will be useful,          |
  16.  | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
  17.  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            |
  18.  | GNU General Public License for more details.                             |
  19.  |                                                                          |
  20.  | You should have received a copy of the GNU General Public License        |
  21.  | along with this program; if not, write to the Free Software              |
  22.  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.|
  23.  +--------------------------------------------------------------------------+
  24. */
  25.  
  26. // login is not loaded, because of scheduled backups
  27. // session_start() is needed for multi db mode
  28. session_start();
  29. require_once("definitions.php");
  30.  
  31. // used variables
  32. if (!isset($argv)) $argv=FALSE;
  33. if (!isset($_POST['db'])) $_POST['db']=FALSE;
  34. if (!isset($_POST['tables'])) $_POST['tables']=FALSE;
  35. if (!isset($_POST['data'])) $_POST['data']=FALSE;
  36. if (!isset($_POST['zip'])) $_POST['zip']=FALSE;
  37. if (!isset($_POST['drop'])) $_POST['drop']=FALSE;
  38. if (!isset($_POST['man_dirs'])) $_POST['man_dirs']=FALSE;
  39. if (!isset($_POST['comments'])) $_POST['comments']=FALSE;
  40.  
  41. // session
  42. if (!isset($_SESSION['LOGGED_IN'])) $_SESSION['LOGGED_IN']=FALSE;
  43.  
  44. // is shell mode or web-based mode or export-script mode used or is user not logged in?
  45. if ($argv) {
  46.     echo "+======>\n| phpMyBackupPro ".PMBP_VERSION." (c)2004-2005 by Dirk Randhahn\n| ".PMBP_WEBSITE."\n+======>\n";
  47.     if (!isset($argv[1])) {
  48.         echo PMBP_EX_NO_ARGV;
  49.         exit;
  50.     }
  51.     $mode="shell";
  52.     $_POST['db']=$argv[1];
  53.     if (isset($argv[2])) {
  54.         if ($argv[2]) $_POST['tables']="on"; else $_POST['tables']="off";
  55.     } else $_POST['tables']="off";
  56.     if (isset($argv[3])) {
  57.         if ($argv[3]) $_POST['data']="on"; else $_POST['data']="off";
  58.     } else $_POST['data']="off";
  59.     if (isset($argv[4])) {
  60.         if ($argv[4]) $_POST['drop']="on"; else $_POST['drop']="off";
  61.     } else $_POST['drop']="off";
  62.     if (isset($argv[5])) {
  63.         if ($argv[5]) $_POST['zip']="gzip"; else $_POST['zip']="off";
  64.     } else $_POST['zip']="off";
  65.     if (isset($argv[6])) $_POST['man_dirs']=$argv[6]; else $_POST['man_dirs']=FALSE;
  66.     
  67. // backups get only triggered if security key is set right
  68. // older versions of the backup script hould not cause error output, but will no trigger backups!
  69. } elseif (isset($security_key) || (substr(PMBP_VERSION,4)<6 && isset($_GET['incl']))) {
  70.     $mode="incl";
  71.     if(!isset($_SESSION['wss'])) $_SESSION['wss']=-1;
  72.     // is a databse or a file directory selected?
  73.     if ($_POST['db'] || isset($_POST['dirs']) || strlen($_POST['man_dirs'])) {
  74.         // backups can only be triggered with the right security key!
  75.         if ($security_key==$PMBP_SYS_VAR['security_key']) {
  76.             // add new 'last_scheduled' system variables if it does not exist for the current account
  77.             if ($_SESSION['wss']>=0) {
  78.                 if (!isset($PMBP_SYS_VAR['last_scheduled_'.$_SESSION['wss']])) $PMBP_SYS_VAR['last_scheduled_'.$_SESSION['wss']]=0;
  79.             }
  80.                 
  81.             // check if it is already time for a new backup
  82.             if ( ($_SESSION['wss']<0 && $PMBP_SYS_VAR['last_scheduled']>(time()-$period) ) ||
  83.             ($_SESSION['wss']>=0 && $PMBP_SYS_VAR['last_scheduled_'.$_SESSION['wss']]>(time()-$period)) ) {
  84.                 if($PMBP_SYS_VAR['scheduled_debug'])
  85.                     echo "It's not time for a new backups yet! Set period to 'At each call' for debugging.<br>\n";
  86.                 return FALSE;
  87.             }
  88.         } else {
  89.             if($PMBP_SYS_VAR['scheduled_debug'])
  90.                 echo "The security key is wrong. It must have the same value as in global_conf.php. Create a new script!<br>\n";
  91.             return FALSE;
  92.         }
  93.     } else {
  94.         if($PMBP_SYS_VAR['scheduled_debug'])
  95.             echo "No database or directory was selected to backup.<br>\n";
  96.         return FALSE;
  97.     }
  98. } else {
  99.     $mode="web";
  100.     include("login.php");
  101. }
  102.  
  103. // set the timelimit
  104. @set_time_limit($CONF['timelimit']);
  105. @ignore_user_abort(TRUE);
  106.  
  107. // print html if web mode
  108. if ($mode=="web") {
  109.     PMBP_print_header(ereg_replace(".*/","",$_SERVER['SCRIPT_NAME']));
  110.  
  111.     // if first use or no db-connection possible
  112.     if (!@mysql_connect($CONF['sql_host'],$CONF['sql_user'],$CONF['sql_passwd'])) echo "<div class=\"red\">".I_SQL_ERROR."</div><br>\n";
  113.  
  114.     // check if ftp connection is possible
  115.     if ($CONF['ftp_use'] || $CONF['dir_backup']) {
  116.         if (!$CONF['ftp_server']) {
  117.             echo "<div class=\"red\">".C_WRONG_FTP."</div>";
  118.         } elseif (!$conn_id=@ftp_connect($CONF['ftp_server'],$CONF['ftp_port'])) {
  119.             echo "<div class=\"red\">".F_FTP_1." '".$CONF['ftp_server']."'!</div>";
  120.         }
  121.     }
  122. }
  123.  
  124. // if pressed 'backup' backup the db
  125. if ($_POST['db'] || isset($_POST['dirs']) || strlen($_POST['man_dirs'])) {
  126.     $out="";
  127.     
  128.     // get start time to calculate duration
  129.     if (function_exists("microtime")) {
  130.         $microtime=explode(" ",microtime());
  131.         $starttime=($microtime[0]+$microtime[1]);
  132.     } else {
  133.         $starttime=time();
  134.     }    
  135.     
  136.     // delete old backup files
  137.     PMBP_get_backup_files();
  138.  
  139.     // save PMBP_SYS_VARS
  140.     if ($mode=="web") PMBP_save_export_settings();
  141.     
  142.     // in shell mode the dbs must be seperated with commas
  143.     if ($mode=="shell") $db_list=explode(",",$_POST['db']);
  144.         elseif(!isset($_POST['db'][0])) $db_list=FALSE;
  145.             else $db_list=$_POST['db'];
  146.  
  147.     // get all available databases
  148.     $available_dbs=PMBP_get_db_list();
  149.     
  150.     // is a database selected?
  151.     if ($db_list) {
  152.         foreach($db_list as $export_db) {
  153.         
  154.             // check if $export_db is available
  155.             if (in_array($export_db,$available_dbs)) {
  156.  
  157.                 // generate db dump
  158.                 $backupfile=PMBP_dump($export_db,($_POST['tables']=="on"),($_POST['data']=="on"),($_POST['drop']=="on"),$_POST['zip'],$_POST['comments']);
  159.  
  160.                 // is there no db connection or a not existing db?
  161.                 if ($backupfile && $backupfile!=="DB_ERROR") {
  162.                     // change mod to 0777
  163.                     chmod("./".PMBP_EXPORT_DIR.$backupfile,0777);
  164.  
  165.                     // remind filenames if backup by ftp or email is selected
  166.                     if (($CONF['email_use'] && function_exists("mail")) || ($CONF['ftp_use'] && function_exists("ftp_connect"))) $store_files[]=$backupfile;
  167.  
  168.                     if ($mode=="web") echo "<div class=\"green\">".EX_SAVED." ".$backupfile."</div>\n";
  169.                         elseif ($mode=="shell") echo EX_SAVED." ".$backupfile."\n";
  170.                             elseif($mode=="incl") $out.=EX_SAVED." ".$backupfile."<br>\n";
  171.                 } elseif($backupfile==="DB_ERROR") {
  172.                     if ($mode=="web") echo "<div class=\"red\">".C_WRONG_SQL."</div>\n";
  173.                         elseif ($mode=="shell") echo C_WRONG_SQL."\n";
  174.                             elseif($mode=="incl") $out.=C_WRONG_SQL."<br>\n";
  175.                 } else {
  176.                     if ($mode=="web") printf("<div class=\"red\">".EX_NOT_SAVED."</div>\n",$export_db);
  177.                         elseif ($mode=="shell") printf(EX_NOT_SAVED."\n",$export_db);
  178.                             elseif($mode=="incl") $out.=sprintf(EX_NOT_SAVED."<br>\n",$export_db);
  179.                 }
  180.                 
  181.             } else {
  182.                 if ($mode=="web") printf("<div class=\"red\">".PMBP_EX_NO_AVAILABLE."</div>\n",$export_db);
  183.                     elseif ($mode=="shell") printf(PMBP_EX_NO_AVAILABLE."\n",$export_db);
  184.                         elseif($mode=="incl") $out.=sprintf(PMBP_EX_NO_AVAILABLE."<br>\n",$export_db);
  185.             }
  186.         }
  187.     } else {
  188.         if ($mode=="web") echo "<div class=\"red\">".EX_NO_DB."</div><br>\n";
  189.             elseif ($mode=="shell") echo EX_NO_DB."\n";
  190.                 elseif ($mode=="incl") $out.=EX_NO_DB."<br>\n";
  191.             
  192.     }
  193.  
  194.     // start backup using ftp or email
  195.     $backup_info=array("comments"=>$_POST['comments'],"tables"=>($_POST['tables']=="on"),"data"=>($_POST['data']=="on"),"drop"=>($_POST['drop']=="on"),"comp"=>$_POST['zip']);
  196.     if ($CONF['ftp_use'] && isset($store_files)) $out=PMBP_ftp_store($store_files)."\n";
  197.     if ($CONF['email_use'] && isset($store_files)) $out.=PMBP_email_store($store_files,$backup_info);
  198.     
  199.     // file backup per FTP
  200.     if ($CONF['dir_backup'] && (isset($_POST['dirs']) || strlen($_POST['man_dirs']))) {
  201.         $_POST['man_dirs']= str_replace("\n",",",$_POST['man_dirs']);
  202.         $files=array();
  203.         if (isset($_POST['dirs'])) {
  204.             $_POST['dirs']=array_merge(explode(",",$_POST['man_dirs']),$_POST['dirs']);
  205.             foreach($_POST['dirs'] as $dir) $files=array_merge($files,PMBP_get_files($dir));
  206.         } else {
  207.             foreach(explode("|",$_POST['man_dirs']) as $dir) $files=array_merge($files,PMBP_get_files($dir));
  208.         }
  209.         $out.=PMBP_save_FTP($files);
  210.     }
  211.     
  212.     // show execution duration
  213.     if (function_exists("microtime")) {
  214.         $microtime=explode(" ",microtime());
  215.         $endtime=($microtime[0]+$microtime[1]);
  216.     } else {
  217.         $endtime=time();
  218.     }
  219.  
  220.     if ($mode=="web") {
  221.         echo $out."\n";
  222.         echo "<div class=\"bold\">".F_DURATION.": ".number_format($endtime-$starttime,3)." ".F_SECONDS."</div>\n";
  223.     } elseif ($mode=="shell") {
  224.         echo $out;
  225.         echo F_DURATION.": ".number_format($endtime-$starttime,3)." ".F_SECONDS;
  226.     } elseif($mode=="incl") {
  227.          if ($PMBP_SYS_VAR['scheduled_debug']) {
  228.             echo $out;
  229.             echo F_DURATION.": ".number_format($endtime-$starttime,3)." ".F_SECONDS;
  230.          }
  231.     }
  232.  
  233. // show the form for selecting a db to backup
  234. } elseif (!$_POST['db'] && $mode=="web") {
  235.     echo "<form action=\"backup.php\" method=\"post\" name=\"backup\">\n<div>\n";
  236.     PMBP_print_export_form();
  237.     echo "\n<input type=\"submit\" value=\"".EX_EXPORT."\" class=\"button\">\n</div>\n</form>\n";
  238. }
  239.  
  240. // update file list (deletes new generated files if $CONS[del_numer]=0)
  241. PMBP_get_backup_files();
  242.  
  243. // print html if web mode
  244. if ($mode=="web") {
  245.     PMBP_print_footer();
  246. } elseif ($mode=="incl") {
  247.     // save the new timestamp
  248.     if ($_SESSION['wss']<0) {
  249.         $PMBP_SYS_VAR['last_scheduled']=time();
  250.     } else {
  251.         $PMBP_SYS_VAR['last_scheduled_'.$_SESSION['wss']]=time();
  252.     }
  253.     // update global_conf.php
  254.     PMBP_save_global_conf();
  255.     return TRUE;
  256. }
  257. ?>
  258.