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


?>