0xV3NOMx
Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64



Your IP : 3.145.97.1


Current Path : /var/www/html/gcg/src/
Upload File :
Current File : /var/www/html/gcg/src/backup_restore.php

<?php
/*Author : M.Srikanth   
    Date    :  2009-07-13
    Project : paycheck
    Description :  This class is for to backup db
*/

//Start :  'backup_restore'  Class 

	class backup_restore
	{
	  	
		//Start :  Constructor
	           function __construct($aobj_context)
	              {
		              session_start();
		              $this->aobj_context=$aobj_context;
		              $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
					   $this->user_id = $_SESSION['user_id'];
					   $this->BackupSetup();
		           }
		//End : Constructor 
		
				
		        /* This function is for to get market modules names*/
				function BackupSetup()
				{
				 $get_data ="select suse.internal_code,concat('module_',ref_s_user_schema_code) as table_name,
								name,system_name,db_field,concat('module_',ref_module_code) ref_module_table,
								if(ref_s_user_schema_group_code>0,
								concat('user_schema_',ref_s_user_schema_code,'_group_',ref_s_user_schema_group_code),0) as group_table
								from s_user_schema_elements suse inner join
								s_user_schema sus on sus.internal_code=ref_s_user_schema_code
								where module_name='BackUp Setup'";
			
			$lobj_get_data = $this->aobj_context->mobj_db->GetAll($get_data);
			
			foreach($lobj_get_data as $key=>$value)
					{
							if($value[system_name]=='backup_storage_path')
							{
							$this->backup_setup_backup_storage_path=$value[db_field];
							$this->backup_setup_table=$value[table_name];
							}		
							else if($value[system_name]=='mysql_path')
							{
							$this->backup_setup_mysql_path=$value[db_field];
							}					
					}
				}
		         
              function createBackupDb()
                {
                  include_once($this->aobj_context->main_src.'/config.php');
				  $confg_obj=new CConfig();
				 	 $dbhost = $confg_obj->mstr_host;
				     $dbuser =$confg_obj->mstr_user;
				     $dbpass =$confg_obj->mstr_password;
				     $db_name =$confg_obj->mstr_db;
				       if($dbpass == '')
				        $pass_field = "";
				     else
                        $pass_field = " -p";			  
					$get_paths = "select internal_code,{$this->backup_setup_backup_storage_path} as folder_path,
				                            {$this->backup_setup_mysql_path} as mysql_path
					                        from {$this->backup_setup_table} ";
					 $lobj_get_paths = $this->aobj_context->mobj_db->GetRow($get_paths);
			 
						$folder_path = $lobj_get_paths[folder_path];
						$mysql_path = $lobj_get_paths[mysql_path];
                        
				 		system(mkdir($folder_path.date("Ymd")));	 	     		
                     	$folder_path=$folder_path.date("Ymd")."\\";		
						
						$backupFile = $db_name .'_'. date("Ymd").'.sql';
						$command = "{$mysql_path}mysqldump --opt -h {$dbhost}  -u {$dbuser}{$pass_field}{$dbpass} {$db_name}  > {$folder_path}{$backupFile}";
						 //echo $command;
						system(($command),$retval);
	                 }
					 
					function getZipBackup()
					{
					 include_once($this->aobj_context->main_src.'/config.php');
					 	$easy_zip_file_path=$this->aobj_context->main_src."/easy_zip/easy_zip.php";
						require_once($easy_zip_file_path);
					$confg_obj=new CConfig();
				 	 $dbhost = $confg_obj->mstr_host;
				     $dbuser =$confg_obj->mstr_user;
				     $dbpass =$confg_obj->mstr_password;
				     $db_name =$confg_obj->mstr_db;
				       if($dbpass == '')
				        $pass_field = "";
				     else
                        $pass_field = " -p";
						$get_paths = "select internal_code,{$this->backup_setup_backup_storage_path} as folder_path,
				                            {$this->backup_setup_mysql_path} as mysql_path
					                        from {$this->backup_setup_table} ";
					 $lobj_get_paths = $this->aobj_context->mobj_db->GetRow($get_paths);
			 
						$folder_path = $lobj_get_paths[folder_path];
						$mysql_path = $lobj_get_paths[mysql_path];
						
						$zip_path= $this->aobj_context->main_src."/imported_files/inventory_db_".date("Ymd").".sql";
						$command = "{$mysql_path}mysqldump --opt -h {$dbhost}  -u {$dbuser}{$pass_field}{$dbpass} {$db_name}  > {$zip_path}";
						system(($command),$retval);
						$zipobj = new EasyZIP;
						$zipobj -> addFile($zip_path);						 
						$zipobj -> zipFile("{$zip_path}.zip");	 
						header("Expires: 0");
						header("Content-type: application/zip");
						header("Content-Disposition: attachment; filename=$db_name.zip");
						print file_get_contents($zip_path.".zip");
						 unlink(("{$zip_path}.zip"));
						 unlink($zip_path);
						
					}	
                 function restoreBackupDb()
                {

                    include_once($this->aobj_context.'/config.php');
					$confg_obj=new CConfig();
				 	 $dbhost = $confg_obj->mstr_host;
				     $dbuser =$confg_obj->mstr_user;
					 $dbpass =$confg_obj->mstr_password;
					   $db_name =$confg_obj->mstr_db;
				     if($dbpass == '')
				      $pass_field = "";
				     else
                     $pass_field = " -p";	
					  $get_paths = "select internal_code,{$this->backup_setup_backup_storage_path} as folder_path,
				                            {$this->backup_setup_mysql_path} as mysql_path
					                        from {$this->backup_setup_table} ";
					 $lobj_get_paths = $this->aobj_context->mobj_db->GetRow($get_paths);
			  
						$folder_path = $lobj_get_paths[folder_path];
						$mysql_path = $lobj_get_paths[mysql_path];                  
						  
						   $backupFile = $db_name .'_'. $this->inputdate.'.sql';
						   $file_path = $folder_path.$this->inputdate.'\\'.$backupFile;
						    if(!file_exists($file_path)) 
						    return $db_name;

						
							$view_path=$this->aobj_context->main_src."/src/create_all_views.php";
							include_once($view_path);
						
 						    $backupFile = $db_name .'_'. $this->inputdate.'.sql';
							$file_path = $folder_path.$this->inputdate.'\\'.$backupFile;							 
						   if(file_exists($file_path)) 
						   {
						     $create_db = "create database  if not exists {$db_name} ";
							 		 							 
							 $lobj_create_db = $this->aobj_context->mobj_db->execute($create_db);					 
						      
							 $command = "{$mysql_path}mysql -u $dbuser {$pass_field}{$dbpass} {$db_name} < {$file_path}";
	                         
							// echo $command;
							// die();
							 
							 system($command);
							 CreateView($this->aobj_context);							
							}                           				
					     
					return 0;	
                }				
		 	  		
		
   }
 
//End :  Class file

	 
	function GetDbBackup($aobj_context)
      {
		ini_set("memory_limit",-1);
		ini_set('max_execution_time',2900);
		$class_obj=new backup_restore($aobj_context);	
		$class_obj->backup_date = $aobj_context->mobj_data["date"];
		$backup_db_result = $class_obj->createBackupDb();
		 if(!empty($backup_db_result))
		         {
				     $data = $backup_db_result . " Not found";
					 echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"Success");
		          }
		      else if($backup_db_result == 0)
				 {
				     $data = "Database is dumped successfully";
		         	 echo $aobj_context->mobj_output->ToJSONEnvelope($data,0,"Success");
				 }	 
      }
	  function getZipBackup($aobj_context)
      {
		ini_set("memory_limit",-1);
		ini_set('max_execution_time',2900);
		$class_obj=new backup_restore($aobj_context);	
		$class_obj->backup_date = $aobj_context->mobj_data["date"];
		$backup_db_result = $class_obj->getZipBackup();
	 }	  
	 function PutDbRestore($aobj_context)
	 {
		ini_set("memory_limit",-1);
		ini_set('max_execution_time',2900);
		$class_obj=new backup_restore($aobj_context);	
		$class_obj->backup_date = $aobj_context->mobj_data["date"];
		$date_arr = explode('/',$class_obj->backup_date);
		$day = $date_arr[0];
		$month = $date_arr[1];
		$year = $date_arr[2];
		$class_obj->inputdate = $year.$month.$day;

		$restore_db_result = $class_obj->restoreBackupDb(); 
		if(!empty($restore_db_result))
		{
		$data = $restore_db_result . " database file not found";
		echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"Success");
		}
		else if($restore_db_result == 0)
		{
		// $data = "Database is restored successfully";
		// echo $aobj_context->mobj_output->ToJSONEnvelope($data,0,"Success");
		}
	 }
	 
//End :  uploadMarketDeatilsIntoDB()


		
?>