MMCT TEAM
Server IP : 103.53.40.154  /  Your IP : 18.118.144.98
Web Server : Apache
System : Linux md-in-35.webhostbox.net 4.19.286-203.ELK.el7.x86_64 #1 SMP Wed Jun 14 04:33:55 CDT 2023 x86_64
User : ppcad7no ( 715)
PHP Version : 8.2.25
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0750) :  /home2/ppcad7no/holyevents.in/../sssktrust.com/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home2/ppcad7no/holyevents.in/../sssktrust.com/backup-site-plus-table.php
<?php
ob_start();
require_once("includes/dbsmain.inc.php");
/*********************** To create backup of website and database as well *****************/
/*********************** CODE CREATED AND DEVELOPED BY AMITABH KUMAR SINHA DATE:25:09:2013 *****************/
//1) Set execution time to 0 if in case your site is huge and will take much time for back-up as default execution time for PHP is 30 seconds.
ini_set("max_execution_time", 0);
ini_set('memory_limit', '256M');
//if ($_SERVER['HTTP_HOST'] == "localhost") {
//    define('LOCL_MDE', true);
//} else {
// 	define('LOCL_MDE', false);
//}
//if (LOCL_MDE) {
//$host = "localhost"; //host name
//$username = "root"; //username
//$password = ""; // your password
//$dbname = "tki_food"; // database name
//} else {
//$host = "localhost"; //host name
//$username = "ei_tkifood"; //username
//$password = "eFNH,L~KNE?R"; // your password
//$dbname = "ei_tkifood"; // database name
//}
//4) We are going to save our backup in zip format so create an object of zip.
$zip = new ZipArchive();
//5) Call the function backup_tables.
backup_tables($ARR_CFGS["db_host"], $ARR_CFGS["db_user"], $ARR_CFGS["db_pass"], $ARR_CFGS["db_name"]);
//6) Define the function backup_tables which will create a database sql file.
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*'){
	$link = mysqli_connect($host,$user,$pass);
	mysqli_select_db($name,$link);
	//get all of the tables
	if($tables == '*'){
		$tables = array();
		$result = mysqli_query('SHOW TABLES');
		while($row = mysqli_fetch_row($result)){
			$tables[] = $row[0];
		}
	}else{
		$tables = is_array($tables) ? $tables : explode(',',$tables);
	}
	//cycle through
	foreach($tables as $table){
	    $filename = $table.'.sql';
	    $handle = fopen($filename,'w');		
		$result = mysqli_query('SELECT * FROM '.$table);
		$num_fields = mysqli_num_fields($result);
		fwrite( $handle, 'DROP TABLE '.$table.';' );
		$row2 = mysqli_fetch_row(mysqli_query('SHOW CREATE TABLE '.$table));
		fwrite( $handle, "\n\n".$row2[1].";\n\n" );
		for ($i = 0; $i < $num_fields; $i++){
			while($row = mysqli_fetch_row($result)){
				fwrite( $handle, '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])) { fwrite( $handle, '"'.$row[$j].'"' ) ; } else { fwrite( $handle, '""' ); }
					if ($j<($num_fields-1)) { fwrite( $handle, ',' ); }
				}
				fwrite( $handle, ");\n" );
			}
		}
		fwrite( $handle, "\n\n\n" );
	}
	//save file
	fclose($handle);
}
//7) Convert .sql file in .sql.zip file and remove the .sql file.
if (glob("*.sql") != false){
$filecount = count(glob("*.sql"));
$arr_file = glob("*.sql");
for($j=0;$j<$filecount;$j++){
$res = $zip->open($arr_file[$j].".zip", ZipArchive::CREATE);
if ($res === TRUE){
$zip->addFile($arr_file[$j]);
$zip->close();
unlink($arr_file[$j]);
  }
 }
}
//8) Now we have to get current folder name in which our php file is present. This is because when we have to move all folders and files from root directory to our zip.
//get the current folder name-start
$path = dirname($_SERVER['PHP_SELF']);
$position = strrpos($path,'/') + 1;
$folder_name = substr($path,$position);
//get the current folder name-end
//9) Create a name for zip file. I have created it based on today�s date so that we can easily find date of last backup. Also I have append �amitabh-� to a name of zip file which we use in next procedures.
$zipname = date('Y/m/d');
$str = "amitabh-".$zipname.".zip";
$str = str_replace("/", "-", $str);
//10) Add all files from current folder in newly created zip file.
// open archive
if ($zip->open($str, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
// initialize an iterator
// pass it the directory to be processed
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("../$folder_name/"));
// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
if( strstr(realpath($key), "amitabh") == FALSE) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
 }
}
// close and save archive
$zip->close();
if(glob("*.sql.zip") != false) {
$filecount = count(glob("*.sql.zip"));
$arr_file = glob("*.sql.zip");
for($j=0;$j<$filecount;$j++){
unlink($arr_file[$j]);
 }
}
//12) Now move a copy of our zip file to a direcotry which we have created in step 2.
//get the array of zip files
if(glob("*.zip") != false) {
$arr_zip = glob("*.zip");
}
//13) Delete a zip file from root directory as we moved it in another directory.
for ($i=0; $i < count($delete_zip); $i++) {
unlink($delete_zip[$i]);
}
//14) Below code will download the zip forcly.////////////////////////////////
header("Content-type: application/zip"); 
	header("Content-Disposition: attachment; filename=$str"); 
	header("Pragma: no-cache"); 
	header("Expires: 0"); 
	readfile("$str");
	exit;
?>

MMCT - 2023