get all backup file // create=>backup // update=>restore // delete=>delete backup file //Setting Time zone for date() date_default_timezone_set('Asia/Hong_Kong'); //For testing class Backup{ const LOG_TYPE='ADMIN'; const DB_TABLE='N/A'; //const BACKUP_FILE_NAME="HKUYA_LadiesClub_bak.zip"; //const BACKUP_FILE_TEMP_NAME="Temp.zip"; const BACKUP_DIR_PATH="system/backup/"; const RESTORE_DIR_PATH="tmp/restore/"; const CONFIG_FILE_NAME="config.php"; const CONFIG_FILE_PATH="system/config/"; const ALBUM_DIR_NAME="album"; const ALBUM_DIR_PATH="images/"; private static $file_collection=array(); public static function get_all_backup_file_info(){ $rights=Permission::get_backup_restore_permission(); if($rights['read']){ $dir=Backup::BACKUP_DIR_PATH; $bakAry=array(); $files=@scandir($dir); foreach($files as $file){ //bak_09032011_235346.zip if(file_exists($dir.$file) && substr($file,0,4)=='bak_' && substr($file,strlen($file)-4)=='.zip'){ $date=substr($file,4,8); $time=substr($file,13,6); $bak_name=$file; $dateTime=Backup::convert_to_date($date,$time); $aBak=array('filename'=>$bak_name,'dateTime'=>$dateTime); array_push($bakAry,$aBak); } } return $bakAry; } return false; } public static function delete_backup_file($filename){ $start_at=time(); $rights=Permission::get_backup_restore_permission(); if($rights['delete']){ $filename=trim($filename); $isFill=CoreFunc::valid($filename); $isFill=(substr($filename,0,4)=='bak_' && substr($filename,strlen($filename)-4)=='.zip')?true:false; if($isFill && @file_exists(Backup::BACKUP_DIR_PATH.$filename)){ if(@unlink(Backup::BACKUP_DIR_PATH.$filename)){ $detail='Delete backup Data File (File Name: '.$filename.').'; Logs::record_event($start_at,'delete',Backup::LOG_TYPE,Backup::DB_TABLE,$detail); Backup::clear_backup_data(); return true; } } } return false; } public static function backup_data($database=true,$db_reqTblLst='ALL',$db_reqData=true,$album=true,$config=true){ $start_at=time(); $rights=Permission::get_backup_restore_permission(); if($rights['create']){ Backup::clear_backup_data(); $thisBakName="bak_".date('dmY')."_".date('His').".zip"; $is_copied=true; if($database) $is_copied=($is_copied)?Backup::backup_db($db_reqTblLst,$db_reqData):false; if($album){ $is_copied=($is_copied)?Backup::backup_album():false; } if($config) $is_copied=($is_copied)?Backup::backup_config_file():false; if(Backup::create_desc_file($thisBakName,$database,$album,$config,$is_copied)) if($is_copied){ $backupFileInfo=Backup::zipping_file(Backup::BACKUP_DIR_PATH,$thisBakName); if(is_array($backupFileInfo)){ $backupFileInfo['file_name']=$thisBakName; $backupFileInfo['file_path']=Backup::BACKUP_DIR_PATH.$thisBakName; $detail='Backup Data File (File Name: '.$thisBakName.').'; Logs::record_event($start_at,'create',Backup::LOG_TYPE,Backup::DB_TABLE,$detail); Backup::clear_backup_data(); return $backupFileInfo; } } } Backup::clear_backup_data(); return false; } public static function restore_data($zipBakFileName){ $start_at=time(); $rights=Permission::get_backup_restore_permission(); if($rights['update']){ //if User has permission to retore the database $is_ok=true; $thisRestoreFilePath=Backup::RESTORE_DIR_PATH.substr($zipBakFileName,0,strlen($zipBakFileName)-4); if(Backup::unzipping_file(Backup::RESTORE_DIR_PATH.$zipBakFileName,$thisRestoreFilePath)){ $files=scandir($thisRestoreFilePath); foreach($files as $file){ if($is_ok && $file==Backup::CONFIG_FILE_NAME) //restore config file $is_ok=($is_ok)?rename($thisRestoreFilePath."/".$file,Backup::CONFIG_FILE_PATH.Backup::CONFIG_FILE_NAME):false; else if($is_ok && is_dir($thisRestoreFilePath."/".$file) && $file==Backup::ALBUM_DIR_NAME) //restore album $is_ok=($is_ok)?Backup::restore_album($thisRestoreFilePath."/".$file."/"):false; else if($is_ok && substr($file,strlen($file)-4)==".sql") //restore database $is_ok=($is_ok)?Backup::restore_db($thisRestoreFilePath."/".$file):false; } if($is_ok){ $detail='Restore Data (Backup File: .'.$zipBakFileName.'.).'; Logs::record_event($start_at,'update',Backup::LOG_TYPE,Backup::DB_TABLE,$detail); if(Backup::clear_recovery_data()) if(!@file_exists($thisRestoreFilePath)) return true; } } } return false; } private static function convert_to_date($date,$time){ $day=substr($date,0,2); $month=substr($date,2,2); $year=substr($date,4); $hour=substr($time,0,2); $minute=substr($time,2,2); $second=substr($time,4); return mktime($hour,$minute,$second,$month,$day,$year); } private static function create_desc_file($bakName,$database,$album,$config,$is_success){ //Get user info $user=User::get_current_user(); $fname=$user->get_fname(); $lname=$user->get_lname(); $uid=$user->get_id(); //Printing Header $content="Backup File Name: ".$bakName."\r\n"; $content.="Backup on ".date('Y-m-d H:i:s')." by $fname $lname (ID: $uid);"."\r\n\r\n"; //Printing Backup List $content.="Below data have been backed up:\r\n"; $content.=($database)?"\t- Database (Details are list in the sql.)\r\n":""; $content.=($album)?"\t- Album with Photos\r\n":""; $content.=($config)?"\t- Configuration File\r\n":""; //Printing Footer $content.=($is_success)?"\r\nBackup Success!":"Error occurred.\r\nNot prefer to use this backup file to restore data!"; //Creating file $descFile=@fopen(Backup::BACKUP_DIR_PATH."Description.txt", "x+"); fwrite($descFile,$content); fclose($descFile); if(file_exists(Backup::BACKUP_DIR_PATH."Description.txt")) return true; return false; } private static function get_all_db_tables(){ return dbController::get_all_db_tables(); } private static function backup_db($tblLst='ALL',$data=true){ //$rights=Permission::get_backup_restore_permission(); //if($rights['create']){ //if User has permission to backup the database //Garthering GET data $reqTbl=(isset($tblLst) && !empty($tblLst))?$tblLst:'ALL'; $reqData=($data===false)?false:true; $reqTbl=explode("|",$reqTbl); //array_pop($reqTbl); $reqTbl=(in_array('ALL',$reqTbl))?'ALL':$reqTbl; //Getting User Info $user=User::get_current_user(); $fname=$user->get_fname(); $lname=$user->get_lname(); $uid=$user->get_id(); //Gathering Tables $tblAry=dbController::get_all_db_tables(); if(!$tblAry) return false; if($reqTbl!='ALL'){ $tempTbl=array(); foreach($tblAry as $table) if($reqTbl!='' && $reqTbl!=null && in_array($table,$reqTbl)) //bug fixed array_push($tempTbl,$table); $tblAry=$tempTbl; $tempTbl=null; } //Printing Header for sql file $tblStr=""; //Contain the Tables List for the sql file header for($i=0;$i0){ $zip = new ZipArchive(); if($zip->open($zipFileInfo['file_path'],ZIPARCHIVE::OVERWRITE)===true){ foreach(Backup::$file_collection as $file){ $filePath=explode('/',$file); $filePath=$filePathAry[sizeof($filePathAry)-1]; //get filename -> /system/backup/abc.zip, so value is abc.zip if(substr($file,0,4)!='bak_' && substr($file,strlen($file)-4)!='.zip') //if file is a backed up file (other zipped backup), it will not zip into current backup $zip->addFile($file,str_replace(Backup::BACKUP_DIR_PATH,'',$file)); } return $zipFileInfo; } } return false; } private static function unzipping_file($zipFilePath, $unzipDirPath){ $zip=new ZipArchive() ; if ($zip->open($zipFilePath)===true){ $zip->extractTo($unzipDirPath); $zip->close(); return true; } return false; } private static function grathering_files_of_dir($dir){ $files=@scandir($dir); foreach($files as $file){ $current_filePath=$dir.$file; if(!is_dir($current_filePath) && substr($file,0,1)!='.') array_push(Backup::$file_collection,$current_filePath); else if(is_dir($current_filePath) && substr($file,0,1)!='.') Backup::grathering_files_of_dir($current_filePath.'/'); } } private static function copy_dir($src,$dst) { //ref php.net $src=$src."/"; $dst=$dst."/"; $dir = opendir($src); @mkdir($dst); while(false !== ( $file = readdir($dir)) ) { if (( substr($file,0,1) != '.' ) && ( substr($file,0,1) != '..' )) if(is_dir($src.$file)) { Backup::copy_dir($src . $file,$dst . $file); } else copy($src . $file, $dst . $file); } closedir($dir); return true; } private static function rrmdir($dir) { //ref php.net if(is_dir($dir)){ $objects=scandir($dir); foreach($objects as $object){ if($object!="." && $object!="..") if (filetype($dir."/".$object) == "dir"){ Backup::rrmdir($dir."/".$object); }else unlink($dir."/".$object); } reset($objects); rmdir($dir); } } private static function clear_backup_data($dir=null){ $dir=($dir==null)?Backup::BACKUP_DIR_PATH:$dir; $files=@scandir($dir); foreach($files as $file){ if(@substr($file,0,4)!='bak_' && @substr($file,strlen($file)-4)!='.zip'){ if(is_dir($dir.$file) && substr($file,0,1)!='.'){ Backup::rrmdir($dir.$file."/"); } else if(file_exists($dir.$file) && substr($file,0,1)!='.') @unlink($dir.$file); } } return true; } private static function clear_recovery_data(){ $restoreDirPath=Backup::RESTORE_DIR_PATH; $files=@scandir($restoreDirPath); foreach($files as $file){ if(is_dir($restoreDirPath.$file) && substr($file,0,1)!='.') Backup::rrmdir($restoreDirPath.$file); else @unlink($restoreDirPath.$file); } return true; } } ?>