Compare files' differ and backup
来源:互联网 发布:蚁群算法 知乎 编辑:程序博客网 时间:2024/05/21 06:52
1)destination:
build a cron php program to compare some file with backup file ,if some files are changed,log these files' name and backup all old files.
2)how to do
.Get the all files' name of we want to compare
.Use the command diff to compare all files.
.log all changed filenames
.Use the command tar to backup all old files.
.Use the command cp to copy all new files into backup directory
.Send email to alert us which files are changed.
3) source code:
<?php
//calculate change
$listfilename="/usr/local/www/apache22/CRON/Test_jevons/backup.flist";
$backdir="/usr/local/www/apache22/backup";
$logfilename="/usr/local/www/apache22/CRON/Test_jevons/backup.diff.log";
$emialfilename="/usr/local/www/apache22/CRON/Test_jevons/email.list";
echo "Check the exist of the file $listfilename/n";
if(!file_exists($listfilename))
{
echo "$listfilename is not found/n";
exit;
}
`mkdir $backdir`;
//the file is exist,we should check the filelist
function get_filename($data){
$preg = '///([^//]*)$/si';
$match=array();
preg_match_all($preg, $data, $match);
$nlast = count($match[1])-1;
if($nlast>=0)
{
$ret = trim($match[1][$nlast]);
return $ret;
}
else return "";
}
function get_ex_filename($data){
return str_replace("/","_",$data);
}
function get_filepath($data){
$preg = '/(.*)///si';
$match=array();
preg_match_all($preg, $data, $match);
$nlast = count($match[1])-1;
if($nlast>=0)
{
$ret = trim($match[1][$nlast]);
return $ret;
}
else return "";
}
$file_list = file("$listfilename");
$total = sizeof ($file_list);
echo "Check the diff of the file list/n";
$ret_list="";
for($i=0;$i<$total;$i++)
{
$ffull=trim($file_list[$i]);
$fname = get_filename( $ffull);
$exfname=get_ex_filename($ffull);
$cmpfile = $backdir."/".$exfname;
// echo $cmpfile."--cmpfile/r/n";
if(!file_exists($cmpfile)) continue;
//calculate diff
$cmdstring ="diff ".$ffull." ".$cmpfile;
echo $cmdstring."/n";
$retstring= `$cmdstring`;
//echo $retstring."/n";
$ret=strlen($retstring);
if($ret>0) $ret_list.=$ffull."/r/n";
}
//Log the result to log file
echo "Log the result to log file/n";
if(strlen($ret_list)>0)
{
$fp = fopen($logfilename,'w');
if($fp)
{
fwrite($fp, $ret_list, strlen($ret_list));
fclose($fp);
}
}
//Tar and gzip old backup dir and remove all old list file
//might be i can use another way to compress
//tar zcvf FileName.tar.gz DirName
echo "Tar and gzip old backup dir and remove all old list file/n";
$today=date("Y_m_d");
echo $today."/n";
$targzpath=get_filepath($backdir);
$targzname=$targzpath."/".$today.".tar.gz";
$cmdline="tar zcvf $targzname $backdir";
//echo $cmdline."/n";
`$cmdline`;
//New backup all list file
echo "New backup all list file/n ";
for($i=0;$i<count($file_list);$i++)
{
$ffull=trim($file_list[$i]);
$fname = get_filename( $ffull);
$exfname= get_ex_filename( $ffull);
$backname = $backdir."/".$exfname;
//$fpath = get_filepath($ffull);
//echo $ffull."--fpath=/"".$fpath."/"/r/n";
$cmdline="cp -rf $ffull $backname";
echo $cmdline."/n";
`$cmdline`;
}
//Send email
echo "Send all email /n";
if(strlen($ret_list)>0)
{//send all email
echo $ret_list."/n";
// mail('jrubenstein@filetransfer.com.cn','File change list',$ret_list);
if(file_exists($emialfilename))
{
$emial_list = file("$emialfilename");
$total = sizeof ($emial_list);
for($i=0;$i<$total;$i++)
{
echo $emial_list[$i]."/n";
mail($emial_list[$i],'File change list',$ret_list);
}
}
}
?>
build a cron php program to compare some file with backup file ,if some files are changed,log these files' name and backup all old files.
2)how to do
.Get the all files' name of we want to compare
.Use the command diff to compare all files.
.log all changed filenames
.Use the command tar to backup all old files.
.Use the command cp to copy all new files into backup directory
.Send email to alert us which files are changed.
3) source code:
<?php
//calculate change
$listfilename="/usr/local/www/apache22/CRON/Test_jevons/backup.flist";
$backdir="/usr/local/www/apache22/backup";
$logfilename="/usr/local/www/apache22/CRON/Test_jevons/backup.diff.log";
$emialfilename="/usr/local/www/apache22/CRON/Test_jevons/email.list";
echo "Check the exist of the file $listfilename/n";
if(!file_exists($listfilename))
{
echo "$listfilename is not found/n";
exit;
}
`mkdir $backdir`;
//the file is exist,we should check the filelist
function get_filename($data){
$preg = '///([^//]*)$/si';
$match=array();
preg_match_all($preg, $data, $match);
$nlast = count($match[1])-1;
if($nlast>=0)
{
$ret = trim($match[1][$nlast]);
return $ret;
}
else return "";
}
function get_ex_filename($data){
return str_replace("/","_",$data);
}
function get_filepath($data){
$preg = '/(.*)///si';
$match=array();
preg_match_all($preg, $data, $match);
$nlast = count($match[1])-1;
if($nlast>=0)
{
$ret = trim($match[1][$nlast]);
return $ret;
}
else return "";
}
$file_list = file("$listfilename");
$total = sizeof ($file_list);
echo "Check the diff of the file list/n";
$ret_list="";
for($i=0;$i<$total;$i++)
{
$ffull=trim($file_list[$i]);
$fname = get_filename( $ffull);
$exfname=get_ex_filename($ffull);
$cmpfile = $backdir."/".$exfname;
// echo $cmpfile."--cmpfile/r/n";
if(!file_exists($cmpfile)) continue;
//calculate diff
$cmdstring ="diff ".$ffull." ".$cmpfile;
echo $cmdstring."/n";
$retstring= `$cmdstring`;
//echo $retstring."/n";
$ret=strlen($retstring);
if($ret>0) $ret_list.=$ffull."/r/n";
}
//Log the result to log file
echo "Log the result to log file/n";
if(strlen($ret_list)>0)
{
$fp = fopen($logfilename,'w');
if($fp)
{
fwrite($fp, $ret_list, strlen($ret_list));
fclose($fp);
}
}
//Tar and gzip old backup dir and remove all old list file
//might be i can use another way to compress
//tar zcvf FileName.tar.gz DirName
echo "Tar and gzip old backup dir and remove all old list file/n";
$today=date("Y_m_d");
echo $today."/n";
$targzpath=get_filepath($backdir);
$targzname=$targzpath."/".$today.".tar.gz";
$cmdline="tar zcvf $targzname $backdir";
//echo $cmdline."/n";
`$cmdline`;
//New backup all list file
echo "New backup all list file/n ";
for($i=0;$i<count($file_list);$i++)
{
$ffull=trim($file_list[$i]);
$fname = get_filename( $ffull);
$exfname= get_ex_filename( $ffull);
$backname = $backdir."/".$exfname;
//$fpath = get_filepath($ffull);
//echo $ffull."--fpath=/"".$fpath."/"/r/n";
$cmdline="cp -rf $ffull $backname";
echo $cmdline."/n";
`$cmdline`;
}
//Send email
echo "Send all email /n";
if(strlen($ret_list)>0)
{//send all email
echo $ret_list."/n";
// mail('jrubenstein@filetransfer.com.cn','File change list',$ret_list);
if(file_exists($emialfilename))
{
$emial_list = file("$emialfilename");
$total = sizeof ($emial_list);
for($i=0;$i<$total;$i++)
{
echo $emial_list[$i]."/n";
mail($emial_list[$i],'File change list',$ret_list);
}
}
}
?>
- Compare files' differ and backup
- Compare to files,printing the first line where they differ
- Backup application files and DB
- LINUX 下diff 比较文件时出现错误:Binary files file1.txt and file2.txt differ
- How and JSTL Differ
- How Do I Compare Two Files and Show The Differences?
- about git-merge "Binary files differ"
- Where C and C++ Differ
- Nand and Nor Flash differ..
- Where C and C++ Differ
- vim# swap backup files
- 图片展示 differ between DW and DM
- Rman delete expried backup files
- Backup / delete event log files
- BACKUP RECOVERY AREA与BACKUP RECOVERY FILES;的区别
- backup and recover
- 4.Backup and Recovery
- XCloner - Backup and Restore
- 解决回车刷新页面问题
- php分页代码
- Delphi和C++数据类型对照表
- AIX下如何正确卸载DB2
- 用Xml2OleDb将XML文件插入到数据库
- Compare files' differ and backup
- 开源软件SOA解决方案对企业的三大好处
- 截屏的代码(JAVA)
- 《PetShop》系列之二 PetShop数据访问层之数据库访问设计
- 在线视频的过去,现在和未来
- AJAX的EMAIL验证
- SIP简介,第1部分:SIP初探
- 懂得递归和剪枝,遥远就不远了,一个编程题的感受
- Perils in RMT