irwan

backup database

Apr 26th, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.28 KB | None | 0 0
  1. db_backup.php
  2.  
  3.  
  4. <?php
  5.  
  6.     $GLOBALS["adminpage"] = "yes";
  7.    
  8.     include "../lib/.htconfig.php";
  9.    
  10.     $tml->RegisterVar("TITLE", "Database Backup");
  11.    
  12.     if(!$user->IsOperator() || !$user->IsLoggedIn())
  13.         exit($error->Report("Database Backup", "You can not access this page."));
  14.    
  15.     if($_GET["action"] == "download")
  16.     {
  17.         if(!$_GET["file"])
  18.             exit($error->Report("Database Backup", "An error has occured."));
  19.        
  20.         header("Content-Type: application/octet-stream");
  21.         header("Content-Disposition: attachment; filename=backup.sql");
  22.         header("Expires: 0");
  23.         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  24.         header("Pragma: public");
  25.        
  26.         $main->WriteToLog("backup", "Backup \"" . $_GET["file"] . "\" downloaded");
  27.        
  28.         readfile(_BACKUP_PATH . $_GET["file"]);
  29.     }
  30.     else
  31.     {
  32.         if($_GET["action"] == "backup")
  33.         {
  34.             $newfile    = "# Dump created with Create Your GetPaid " . _SYSTEM_VERSION . " on " . (date("Y-m-d H:i")) . "\r\n";
  35.             $tables     = mysql_list_tables(_DB_NAME);
  36.             $num_tables = @mysql_num_rows($tables);
  37.             $time0      = time();
  38.            
  39.             $i = 0;
  40.            
  41.             while($i < $num_tables)
  42.             {
  43.                 $table  = mysql_tablename($tables, $i);
  44.                
  45.                 $newfile .= "\r\n# ----------------------------------------------------------\r\n#\r\n";
  46.                 $newfile .= "# Table structure for table '$table'\r\n#\r\n\r\n";
  47.                
  48.                 $newfile .= $db->get_def($table);
  49.                
  50.                 $newfile .= "\r\n\r\n";
  51.                 $newfile .= "#\r\n# Dumping data for table '$table'\r\n#\r\n\r\n";
  52.                
  53.                 $newfile .= $db->get_content($table);
  54.                
  55.                 $newfile .= "\r\n\r\n";
  56.                
  57.                 $i++;
  58.                
  59.                 $time1  = time();
  60.                
  61.                 if($time1 >= $time0 + 30)
  62.                 {
  63.                     $time0  = $time1;
  64.                    
  65.                     header('X-pmaPing: Pong');
  66.                 }
  67.             }
  68.            
  69.             $fn = date("Y_m_d_H_i") . ".sql";
  70.            
  71.             $fp = fopen(_BACKUP_PATH . $fn, "w");
  72.            
  73.             fwrite($fp, $newfile);
  74.             fclose($fp);
  75.            
  76.             $main->WriteToLog("backup", "Backup \"$fn\" manually created");
  77.  
  78.             $main->printText("<B>Database Backup</B><BR><BR>Succesfully created database backup.", 1);
  79.         }
  80.         elseif($_GET["action"] == "restore")
  81.         {
  82.             if($_GET["file"] == "")
  83.                 exit($error->Report("Database Backup","An error has occured."));
  84.            
  85.             if($_GET["confirm"] == "yes")
  86.             {
  87.                 @set_time_limit(0);
  88.                
  89.                 $file           = fread(fopen(_BACKUP_PATH . $_GET["file"], "r"), filesize(_BACKUP_PATH . $_GET["file"]));
  90.                 $query          = explode(";#%%\r\n", $file);
  91.                 $errorCount     = 0;
  92.                
  93.                 for($i = 0; $i < count($query) - 1; $i++)
  94.                 {
  95.                     @mysql_query($query[$i]) or ($errorCount++);
  96.                    
  97.                     if($errorCount == 10)
  98.                         exit($error->Report("Database Backup", "<BR><BR><B><FONT COLOR=\"red\">" . $_GET["file"] . " couldn't be restored -> too many errors!</FONT></B>"));
  99.                 }
  100.                
  101.                 $main->WriteToLog("backup", "Backup \"" . $_GET["file"] . "\" restored");
  102.                
  103.                 $main->printText("<B>" . $_GET["file"] . " successfully restored!</B><BR><BR>$errorCount errors occured.", 1);
  104.             }
  105.             else
  106.             {
  107.                 $main->printText("<B>All previous settings will be replaced!</B><BR>Are you sure you want to restore the database?"
  108.                                 ."<BR><BR><A HREF=\""._ADMIN_URL."/db_backup.php?sid=" . $session->ID . "&action=restore&file=".$_GET["file"]."&confirm=yes\">Yes</A> - "
  109.                                 ."<A HREF=\""._ADMIN_URL."/db_backup.php?sid=" . $session->ID . "\">No</A><BR><BR>");
  110.             }
  111.         }
  112.         else
  113.         {
  114.             if($_GET["delete"] != "")
  115.             {
  116.                 $main->WriteToLog("backup", "Backup \"" . $_GET["delete"] . "\" deleted");
  117.                
  118.                 $text   .= !@unlink(_BACKUP_PATH . $_GET["delete"]) ? "<B><FONT COLOR=\"red\">" . $_GET["delete"] . " couldn't be deleted!</FONT></B><BR><BR>" : "<B>" . $_GET["delete"] . " successfully deleted!</B><BR><BR>";
  119.             }
  120.  
  121.             $text   .= "<TABLE WIDTH=\"100%\" STYLE=\"border: 1 solid #EAEAEA;\" BGCOLOR=\"#F0F0F0\">\n"
  122.                       ."<TR BGCOLOR=\"#D3D3D3\">"
  123.                       ."<TD>File</TD><TD>Size</TD><TD>Date</TD><TD>Action</TD></TR>\n";
  124.            
  125.             if(!$dir = @opendir(_BACKUP_PATH))
  126.                 exit($error->Report("Database Backup", "Failed to open backup directory, please create it and CHMOD it to 777."));
  127.            
  128.             $total  = 1;
  129.            
  130.             while($file = readdir($dir))
  131.             {
  132.                 if($file != "." && $file != ".." && eregi("\.sql", $file))
  133.                 {
  134.                     $fileid = explode(".", $file);
  135.                    
  136.                     $text   .= "<TR BGCOLOR=\"#EAEAEA\"><TD>backup$total.sql&nbsp;</TD><TD>&nbsp;" . number_format(filesize(_BACKUP_PATH . $file) / 1024, 0) . " kB&nbsp;</TD>\n"
  137.                               ."<TD>&nbsp;" . date(_SITE_DATESTAMP . " H:i", filemtime(_BACKUP_PATH . $file)) . "</TD>\n"
  138.                               ."<TD>&nbsp;<A HREF=\""._ADMIN_URL."/db_backup.php?sid=" . $session->ID . "&action=restore&file=$file\"><B>Restore</B></A> "
  139.                               ."<A HREF=\""._ADMIN_URL."/db_backup.php?sid=" . $session->ID . "&action=download&file=$file\"><B>Download</B></A> "
  140.                               ."<A HREF=\""._ADMIN_URL."/db_backup.php?sid=" . $session->ID . "&delete=$file\"><B>Delete</B></A></TD></TR>\n";
  141.                    
  142.                     $total  += 1;
  143.                 }
  144.             }
  145.  
  146.             closedir($dir);
  147.  
  148.             if($total == 1)
  149.                 $text   .= "<TR BGCOLOR=\"#EAEAEA\"><TD COLSPAN=\"4\">You haven't made any backup yet.</TD></TR>\n";
  150.  
  151.             $text   .= "</TABLE><TABLE WIDTH=\"100%\"><TR><TD COLSPAN=\"4\">&nbsp;</TD></TR>\n"
  152.                       ."<TR><TD COLSPAN=\"4\"><A HREF=\""._ADMIN_URL."/db_backup.php?sid=" . $session->ID . "&action=backup\">Click here to create a backup</A></TD></TR></TABLE>\n";
  153.  
  154.             $main->printText($text);
  155.         }
  156.     }
  157.  
  158. ?>
Advertisement
Add Comment
Please, Sign In to add comment