2010年6月13日 星期日

文件掃描系統備份

備份在不同的電腦(掃描備份.bat)

@echo off
rem // copy to local (different directory)
cd d:\
xcopy d:\檢驗科\*.* c:\檢驗科scan備份\ /s /y

rem // copy to web directory (before transform)
NET USE o: \\192.168.xx.xx\d$\檢驗科 /user:lis2k
xcopy d:\檢驗科\*.* O:\ /y /s
NET USE O: /delete

rem // rename d:\檢驗科 加上日期
rem cd d:\
rename 檢驗科 檢驗科%Date:~4,4%%date:~9,2%%date:~12,2%

  • 備份檔案轉入網頁資料庫
(1) filetransfer.bat
cd c:\appserv\php5
php -f filetransfer.php >> filetransfer.log

(2) filetransfer.php
//include mdb2
require_once("config.inc.php");
$scanDir="d:/檢驗科";
//check $dest_dir_name in function getDirectory

getDirectory( $scanDir );
for ($i=1;$i<=5;$i++){
//每次刪減一個level的目錄
delEmptyDirectory( $scanDir );
}
echo "Files Transfered";

//test empty directory
function is_empty_dir($dir)
{
if (($files = @scandir($dir)) && count($files) <= 2) {
return true;
}
return false;
}

//delete empty directory
function delEmptyDirectory($path){
$ignore = array( 'cgi-bin', '.', '..' );
$dh = @opendir( $path );
while( false !== ( $file = readdir( $dh ) ) ){
if( !in_array( $file, $ignore ) ){
if( is_dir( "$path/$file" ) ){
if (($files = @scandir("$path/$file")) && count($files) <= 2) {
rmdir("$path/$file");
}else{
delEmptyDirectory( "$path/$file", ($level+1) );
} //if 空目錄刪除, else 往下scan
} //if is_dir
} //if !in_array
} // while( false !== ( $file = readdir( $dh ) ) )
closedir( $dh );
} //function delEmptyDirectory

//test recursive directory list
function getDirectory( $path = '.', $level = 0 ){
global $mdb2;
$today=date("Ymd");
echo "$today files transfer start....\n";
//if backup to different dir, change the path below
$dest_dir_name="i:/backup/$today/";
//backup destination directory with date 2010(Y)04(m)22(d) sub-directory
if (!is_dir($dest_dir_name)){
mkdir($dest_dir_name);
echo "create dir $dest_dir_name \n";
//如果沒有這個目錄,mkdir
}
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.

$dh = @opendir( $path );
// Open the directory to the handle $dh

while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory

if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored

// $spaces = str_repeat( ' ', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.

if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...

// echo $i++."$spaces $path/$file
";
// unmark to echo directory name
getDirectory( "$path/$file", ($level+1) );
echo "go subDir $path/$file \n";
// Re-call this same function but on a new directory.
// this is what makes function recursive.

} else {
$full_path="$path/$file";
// echo $i++."$spaces $path/$file
";
$path_parts = pathinfo($full_path);
$file_mod_time=date ("YmdHis", filemtime($full_path));
$source_dir_name= $path_parts['dirname'];//directory
$source_file_name= $path_parts['basename'];//filename with ext
$dest_file_name= md5_file($full_path);//filename change to md5
$file_ext=$path_parts['extension'];//file ext
$source_file_name_no_ext= $path_parts['filename']; // since PHP 5.2.0, filename without ext
rename($full_path,"$dest_dir_name/$dest_file_name.$file_ext");
// exec_sql("insert into file_search(source_dir_name,source_file_name,dest_dir_name,dest_file_name,file_ext) values('$source_dir_name','$source_file_name','$dest_dir_name','$dest_file_name','$file_ext')");
exec_sql("insert into file_search(source_file_name,dest_dir_name,dest_file_name,file_ext,file_mod_time) values('$full_path','$today/','$dest_file_name','$file_ext','$file_mod_time')");
// echo "$path/$file".md5_file("$path/$file")."
";
// Just print out the filename
} //if is_dir
} //if !in_array
} // while( false !== ( $file = readdir( $dh ) ) )
closedir( $dh );
// Close the directory handle
} //function getDirectory

?>

沒有留言: