Compare files' differ and backup

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);
        }
    }
}


?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值