Dear sir/ Madam,
I am attaching the files herewith with which i am not able to take the backup and restore the file. First of all it is not able to open the backup file. Secondly i want to restore also. Third and the main point is that my data is in binary form i.e. file size is big . I 've tried all the resources. Please help me in this urgently.
Regards,
Shraddha
backup.php
[php]
Stamba Backup & Restore
<!-- You can name the links with lowercase, they will be transformed to uppercase by CSS, we prefered to name them with uppercase to have the same effect with disabled stylesheet -->
<ul id="mainNav">
<li><a href="manage.php">DASHBOARD</a></li> <!-- Use the "active" class for the active menu item -->
<li><a href="backup.php" class="active">BACKUP</a></li>
<li><a href="restore.php">RESTORE</a></li>
<li class="logout"><a href="?logout=1">LOGOUT</a></li>
</ul>
<!-- // #end mainNav -->
<div id="containerHolder">
<div id="container">
<!-- h2 stays for breadcrumbs -->
<h2><a href="#" class="active">Create a Backup</a></h2>
<div id="main">
<form action="" class="jNice">
<h3>Backup Log</h3>
<table cellpadding="0" cellspacing="0"><td>
<?php
// Include settings
include("config.php");
// Set the suffix of the backup filename
if ($table == '*') {
$extname = 'all';
}else{
$extname = str_replace(",", "_", $table);
$extname = str_replace(" ", "_", $extname);
}
// Generate the filename for the backup file
$filess = 'backup/dbbackup_' . date("d.m.Y_H:i:s") . '_' . $extname;
// Call the backup function for all tables in a DB
backup_tables($DBhost,$DBuser,$DBpass,$DBName,$table,$extname,$filess);
// Backup the table and save it to a sql file
function backup_tables($host,$user,$pass,$name,$tables,$bckextname,$filess)
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
$return = "";
// Get all of the tables
if($tables == '*') {
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
} else {
if (is_array($tables)) {
$tables = explode(',', $tables);
}
}
// Cycle through each provided table
foreach($tables as $table) {
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
// First part of the output - remove the table
$return .= 'DROP TABLE ' . $table . ';<|||||||>';
// Second part of the output - create table
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return .= "\n\n" . $row2[1] . ";<|||||||>\n\n";
// Third part of the output - insert values into new table
for ($i = 0; $i < $num_fields; $i++) {
while($row = mysql_fetch_row($result)) {
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) {
$return .= '"' . $row[$j] . '"';
} else {
$return .= '""';
}
if ($j<($num_fields-1)) {
$return.= ',';
}
}
$return.= ");<|||||||>\n";
}
}
$return.="\n\n\n";
}
// Save the sql file
$handle = fopen($filess.'.sql','w+');
fwrite($handle,$return);
fclose($handle);
// Close MySQL Connection
mysql_close();
}
require_once('pclzip.lib.php');
$archive = new PclZip($filess.'.zip');
$v_dir = dirname(getcwd()); // or dirname(__FILE__);
$v_remove = $v_dir;
$v_list = $archive->create($v_dir, PCLZIP_OPT_REMOVE_PATH, $v_remove);
if ($v_list == 0) {
die("Error : ".$archive->errorInfo(true));
}
// Print the message
print('The backup has been created successfully. You can get MySQL dump file here.
' . "\n"); print('You can get Backed-up files archive here.
' . "\n"); ?>
</td></table>
<br />
</form>
</div>
<!-- // #main -->
<div class="clear"></div>
</div>
<!-- // #container -->
</div>
<!-- // #containerHolder -->
<p id="footer"> <a href="#"></a></p>
</div>
<!-- // #wrapper -->
[/php]
config.php
[php]<?php
// Settings
$table = ‘*’;
$DBhost = ‘localhost’;
$DBuser = ‘root’;
$DBpass = ‘’;
$DBName = ‘inoutreg’;
?>
cron.php
<?php $pass = $argv[2]; if ($pass != 'your_custom_long_password') { die('Password is incorrect!'); } foreach(glob('backup/' . '*.*') as $v){ if (is_file($v)) { unlink($v); } } // Include settings include("config.php"); // Set the suffix of the backup filename if ($table == '*') { $extname = 'all'; }else{ $extname = str_replace(",", "_", $table); $extname = str_replace(" ", "_", $extname); } // Generate the filename for the backup file $filess = 'backup/dbbackup_' . date("d.m.Y_H:i:s") . '_cron_' . $extname; // Call the backup function for all tables in a DB backup_tables($DBhost,$DBuser,$DBpass,$DBName,$table,$extname,$filess); // Backup the table and save it to a sql file function backup_tables($host,$user,$pass,$name,$tables,$bckextname,$filess) { $link = mysql_connect($host,$user,$pass); mysql_select_db($name,$link); $return = ""; // Get all of the tables if($tables == '*') { $tables = array(); $result = mysql_query('SHOW TABLES'); while($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } } else { if (is_array($tables)) { $tables = explode(',', $tables); } } // Cycle through each provided table foreach($tables as $table) { $result = mysql_query('SELECT * FROM '.$table); $num_fields = mysql_num_fields($result); // First part of the output – remove the table $return .= 'DROP TABLE ' . $table . ';<|||||||>'; // Second part of the output – create table $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $return .= "\n\n" . $row2[1] . ";<|||||||>\n\n"; // Third part of the output – insert values into new table for ($i = 0; $i < $num_fields; $i++) { while($row = mysql_fetch_row($result)) { $return.= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j<$num_fields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = ereg_replace("\n","\\n",$row[$j]); if (isset($row[$j])) { $return .= '"' . $row[$j] . '"'; } else { $return .= '""'; } if ($j<($num_fields-1)) { $return.= ','; } } $return.= ");<|||||||>\n"; } } $return.="\n\n\n"; } // Save the sql file $handle = fopen($filess.'.sql','w+'); fwrite($handle,$return); fclose($handle); // Close MySQL Connection mysql_close(); } require_once('pclzip.lib.php'); $archive = new PclZip($filess.'.zip'); $v_dir = dirname(getcwd()); // or dirname(__FILE__); $v_remove = $v_dir; $v_list = $archive->create($v_dir, PCLZIP_OPT_REMOVE_PATH, $v_remove); if ($v_list == 0) { die("Error : ".$archive->errorInfo(true)); } // Print the message print('The backup has been created successfully!'); ?>delete.php
<?php // Get the filename to be deleted $file=$_GET['file']; // Check if the file has needed args if ($file==NULL){ print(""); print(""); print("You have not provided a file to delete.Click here if your browser doesn't automatically redirect you."); die(); } // Delete the SQL file if (!is_dir("backup/" . $file . '.sql')) { unlink("backup/" . $file . '.sql'); } // Delete the ZIP file if (!is_dir("backup/" . $file . '.zip')) { unlink("backup/" . $file . '.zip'); } // Redirect header("Location: manage.php"); ?>
[/php]
manage.php
[php]
!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
Stamba Backup & Restore
<!-- You can name the links with lowercase, they will be transformed to uppercase by CSS, we prefered to name them with uppercase to have the same effect with disabled stylesheet -->
<ul id="mainNav">
<li><a href="manage.php" class="active">DASHBOARD</a></li> <!-- Use the "active" class for the active menu item -->
<li><a href="backup.php">BACKUP</a></li>
<li><a href="restore.php">RESTORE</a></li>
<li class="logout"><a href="?logout=1">LOGOUT</a></li>
</ul>
<!-- // #end mainNav -->
<div id="containerHolder">
<div id="container">
<!-- h2 stays for breadcrumbs -->
<h2><a href="#" class="active">Dashboard</a></h2>
<div id="main">
<form action="" class="jNice">
<h3>Available Backups</h3>
<table cellpadding="0" cellspacing="0">
<?php
// List the files
$dir = opendir ("./backup");
while (false !== ($file = readdir($dir))) {
// Print the filenames that have .sql extension
if (strpos($file,'.sql',1)) {
// Get time and date from filename
$date = substr($file, 9, 10);
$time = substr($file, 20, 8);
// Remove the sql extension part in the filename
$filenameboth = str_replace('.sql', '', $file);
// Print the cells
print(" </table>
<br />
</form>
</div>
<!-- // #main -->
<div class="clear"></div>
</div>
<!-- // #container -->
</div>
<!-- // #containerHolder -->
<p id="footer"> <a href="#"></a></p>
</div>
<!-- // #wrapper -->
[/php]
restore.php
[php]
Stamba Backup & RestoreStamba Backup & Restore
<!-- You can name the links with lowercase, they will be transformed to uppercase by CSS, we prefered to name them with uppercase to have the same effect with disabled stylesheet -->
<ul id="mainNav">
<li><a href="manage.php">DASHBOARD</a></li> <!-- Use the "active" class for the active menu item -->
<li><a href="backup.php">BACKUP</a></li>
<li><a href="restore.php" class="active">RESTORE</a></li>
<li class="logout"><a href="?logout=1">LOGOUT</a></li>
</ul>
<!-- // #end mainNav -->
<div id="containerHolder">
<div id="container">
<!-- h2 stays for breadcrumbs -->
<h2><a href="#" class="active">Restore a Backup</a></h2>
<div id="main">
<form action="" class="jNice">
<h3>Restore Log</h3>
<table cellpadding="0" cellspacing="0"><td>
<?php
// Get the provided arg
$id=$_GET['id'];
// Check if the file has needed args
if ($id==NULL){
print("");
print("");
print("You have not provided a backup to restore.Click here if your browser doesn't automatically redirect you."); die(); } // Include settings include("config.php"); // Generate filename and set error variables $filename = 'backup/' . $id . '.sql'; $sqlErrorText = ''; $sqlErrorCode = 0; $sqlStmt = ''; // Restore the backup $con = mysql_connect($DBhost,$DBuser,$DBpass); if ($con !== false){ // Load and explode the sql file mysql_select_db("$DBName"); $f = fopen($filename,"r+"); $sqlFile = fread($f,filesize($filename)); $sqlArray = explode(';<|||||||>',$sqlFile); // Process the sql file by statements foreach ($sqlArray as $stmt) { if (strlen($stmt)>3){ $result = mysql_query($stmt); } } } // Print message (error or success) if ($sqlErrorCode == 0){ print("Database restored successfully!
\n"); print("Backup used: " . $filename . "
\n"); } else { print("An error occurred while restoring backup!
\n"); print("Error code: $sqlErrorCode
\n"); print("Error text: $sqlErrorText
\n"); print("Statement:
$sqlStmt
"); } // Close the connection mysql_close(); // Change the filename from sql to zip $filename = str_replace('.sql', '.zip', $filename); // Include this library so we could delete the file include('pclzip.lib.php'); // Remove the current dir rrmdir(dirname(getcwd())); // Recursively remove dir function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != ".." && $object != "restore.php" && $object != $filename) { if (filetype($dir."/".$object) == "dir") { rrmdir($dir."/".$object); } else { unlink($dir."/".$object); } } } reset($objects); } } // Extract archive $archive = new PclZip($filename); if ($archive->extract(PCLZIP_OPT_PATH, "../") == 0) { die("Error : ".$archive->errorInfo(true)); } // Remove two left files unlink($filename); rmdir("backup"); unlink("restore.php"); rmdir(getcwd()); // Files restored successfully print("Files restored successfully!
\n"); print("Backup used: " . $filename . "
\n"); ?>
</td></table>
<br />
</form>
</div>
<!-- // #main -->
<div class="clear"></div>
</div>
<!-- // #container -->
</div>
<!-- // #containerHolder -->
<p id="footer"> <a href="#"></a></p>
</div>
<!-- // #wrapper -->
[/php]