=2){ $_remote_hd_=explode(',',$_remote_hds_[0]); }else{ $_remote_hd_[0]=trim($_remote_hds_[0]); $_remote_hd_[1]='z'; } if(strlen(trim($_locahost_hds_[0])) >=2){ $_locahost_hd_=explode(',',$_locahost_hds_[0]); }else{ $_locahost_hd_[0]=trim($_locahost_hds_[0]); $_locahost_hd_[1]='z'; } $_remote_hd_left=trim($_remote_hds_[1]); //unit: GB $host=trim($_remote_hds_[2]).'\\video'; // folder format D:\d20060611\asus\video // file format: c0101060518110000.vgz -->c01 第二張卡, 01第二個檔案 ,06年, 05月, 18日, 11時 0000 $today=date("Ymd"); $today2=date("ymd"); $hour=date("H"); $file_path="d".$today; //佳鋒's formate if($hour==='00'){//整點時需要調整時間目錄 $today=before_date(1,2); } $local_file_path="Database\\d$today\\$host"; //佳鋒's formate $remote_dir_path="Database"; $now_time_file=$today2.$hour; //---------------------------------------------------------------------------------------------------------------- backup_check(); //---------------------------------------------------------------------------------------------------------------- function backup_check() { $i=0; $remote_file_list= remote_file_list(check_remote()); $remote_path= check_remote(); $local_path=check_local(); foreach(local_file_list() as $val) { if($val != $remote_file_list[$i]){ echo system("copy /y $local_path\\$val $remote_path\\$val")."\n
"; //copy to remote // echo $val."
"; } $i++; } } //---------------------------------------------------------------------------------------------------------------- function check_local() { global $_locahost_hd_,$now_time_file,$local_file_path; foreach($_locahost_hd_ as $val) { if(is_dir($val.":\\$local_file_path")) //match today folder { $data=$val.":\\$local_file_path"; } } return $data; } //---------------------------------------------------------------------------------------------------------------- function local_file_list() { global $_locahost_hd_,$now_time_file,$local_file_path; $data=array(); $i=0; foreach($_locahost_hd_ as $val) { $handle=@opendir($val.":\\$local_file_path"); while ($file = @readdir($handle)) { if((@!eregi("^\.",$file))AND(@!eregi("^[a-zA-Z0-9]....$now_time_file",$file))) { $data[$i].= $file; $i++; } } @closedir($handle); } @sort($data); return $data; } //---------------------------------------------------------------------------------------------------------------- function check_remote() { global $_remote_hd_,$_remote_hd_left,$remote_dir_path,$host,$today; $hds=array(); $data=array(); $i=0; $j=0; //check all valid hd foreach($_remote_hd_ as $val) //grab all record HD lebel { //check if $remote_dir_path wherether exist if(!is_dir("$val:\\$remote_dir_path")) { mkdir("$val".":\\$remote_dir_path",0777); } //check all dir name $handle=@opendir($val.":\\$remote_dir_path\\"); while ($file = @readdir($handle)) { if(@eregi("^[a-zA-Z0-9]",$file)) { $dir= $val.":\\$remote_dir_path\\$file"; //check all file name $handle=@opendir("$dir\\$host"); while ($file = @readdir($handle)) { if(@eregi("^[a-zA-Z0-9]",$file)) { $data[$i].= "$file,$dir,$val"; $i++; } } } } @closedir($handle); //check all hd $_hd_free_=dfs_gb($val,1); if($_hd_free_ >= $_remote_hd_left){ //if full HD $hds[$j]=$val; $j++; } } //------------------------------------------------- $hds_total=@count($hds); @sort($hds); if($hds_total >=1){// more 1 hd $valid_hd=$hds[0]; }else{ $valid_hd=''; // all hd was full sort($data); $datas=explode(",",$data[0]); } //------------------------------------------------- if(!$valid_hd) //hd was full, to check oldest file { //delete oldest folder if(is_dir($datas[1])) { @system("rd /S /Q $datas[1]"); //delete one day's data } $valid_hd=$datas[2]; } // creat new folder, if today folder not exist $hosts=explode("\\",$host); $valid_dir="$valid_hd".":\\$remote_dir_path\\d$today\\$host"; if(!is_dir($valid_dir)) { mkdir("$valid_hd".":\\$remote_dir_path\\d$today",0777); mkdir("$valid_hd".":\\$remote_dir_path\\d$today\\$hosts[0]",0777); mkdir("$valid_hd".":\\$remote_dir_path\\d$today\\$hosts[0]\\$hosts[1]",0777); } return $valid_dir; } //---------------------------------------------------------------------------------------------------------------- function remote_file_list($dir) { global $_remote_hd_,$_remote_hd_left; $data=array(); $i=0; if(is_dir($dir)) { //check all file name $handle=@opendir($dir); while ($file = @readdir($handle)) { if(@eregi("^[a-zA-Z0-9]",$file)) { $data[$i].= $file; $i++; } } @closedir($handle); } return $data; } //---------------------------------------------------------------------------------------------------------------- function dfs_gb($drive,$info) //0=total, 1=free space { if($drive){ if($info==1){//free return round(disk_free_space("$drive:")/1024/1024/1024,2); //To Get Human Result in GB we divid it By 1024 Byte & 1024 KiloByte & 1024 MegaByte }else{ //total return round(disk_total_space("$drive:")/1024/1024/1024,2); //To Get Human Result in GB we divid it By 1024 Byte & 1024 KiloByte & 1024 MegaByte } } } //delDir("E:\Database\d20060614"); //---------------------------------------------------------------------------------------------------------------- function before_date($ago,$method) { if($method==1){ $timestamp = time() - ($ago * 86400); }elseif($method==2){ $timestamp = date("Ymd",time() - ($ago * 86400)); }elseif($method==3){ $timestamp = date("ymdH",time() - ($ago * 3600)); } return $timestamp; } //---------------------------------------------------------------------------------------------------------------- function delDir($dirName) { if(empty($dirName)) { return; } if(file_exists($dirName)) { $dir = dir($dirName); while($file = $dir->read()) { if($file != '.' && $file != '..') { if(is_dir($dirName.'/'.$file)) { delDir($dirName.'/'.$file); } else { @unlink($dirName.'/'.$file) or die('File '.$dirName.'/'.$file.' couldn\'t be deleted!'); } } } @rmdir($dirName.'/'.$file) or die('Folder '.$dirName.'/'.$file.' couldn\'t be deleted!'); // @system("rd /S /Q $dirName"); //delete one day's data } else { echo 'Folder "'.$dirName.'" doesn\'t exist.'; } } ?>