class.check_backup.php
Introduction and Scope
This class will finished below functions:
1)Get all files' name from a file
2).Use the command diff to compare all
files.
3).log all changed filenames
4).Use the command tar and gzip to backup
all old files.
5)Use the command cp to copy all new
files into backup directory
6)Send email to alert us which files are
changed.
Attributes
private $listfilename;
This file store all files’ name we want
to know wether have change.
private $backdir ;
This directory is used to backup new
files..
private $logfilename;
This file is used to log the changed
files’ name..
private $emailfilename;
This file store all emails’ address which
we want to told them the changed files’ name.
Methods
public
function set_listfilename($listfilename);
Set the
absolute path of the list file.
public
function set_backdir($backdir);
Set
the absolute path of backup directory.
public
function set_logfilename($logfilename);
Set the
absolute path of the log file.
public
function set_emailfilename($emailfilename);
Set the
absolute path of the email list file.
public function check_and_backup()
Check all list files’ differ and
log the differs and backup new files and send email.
How to use
$ck_bk=new
check_backup("/usr/local/www/apache22/CRON/Test_jevons");
$ck_bk->check_and_backup();
/*
This will general default configure base on
the directory of your inputing.
You must put the two files which are named
file.list and email.list in your inputing directory.
$this->listfilename = $defaultdir."/file.list";
$this->backdir
= $defaultdir."/backup";
$this->logfilename
=$defaultdir."/backup.diff.log";
$this->emailfilename
=$defaultdir."/email.list";
*/
file.list example
/usr/local/www/apache22/httpsdocs/inc_downloadhandler.php
/usr/local/www/apache22/httpsdocs/inc_downloadhandler_FT.php
/usr/local/www/apache22/httpdocs/about_us.php
/usr/local/www/apache22/httpdocs/index.php
email.list example
<?php @author Jevons zeng <zzz_781111@sina.com> @Introduction and Scope @email.list example @example } private function check_diff() $file_list = file($this->listfilename); $ret_list=""; } private function tar_gz_old() $targzfolder=$this->get_filename($this->backdir); $targzname=$targzpath."/".$today.".tar.gz"; $cmdline="cd $targzpath;tar zcvf $targzname $targzfolder"; $file_list = file($this->listfilename); for($i=0;$i<$total;$i++) $cmdline="cp -rf $ffull $backname"; } $total = sizeof ($emial_list); for($i=0;$i<$total;$i++) } function log_error_msg($err) } public function check_and_backup() //Tar and gzip old backup dir and remove all old list file //New backup all list file //Send email } ?>
/**
@version 1.0
This class will finished below functions:
1)Get all files' name from a file
2).Use the command diff to compare all files.
3).log all changed filenames
4).Use the command tar and gzip to backup all old files.
5)Use the command cp to copy all new files into backup directory
6)Send email to alert us which files are changed.
@Attributes
private $listfilename;
This file store all files' name we want to know wether have change.
private $backdir ;
This directory is used to backup new files..
private $logfilename;
This file is used to log the changed files' name..
private $emailfilename;
This file store all emails' address which we want to told them the changed files' name.
@Methods
public function set_listfilename($listfilename);
Set the absolute path of the list file.
public function set_backdir($backdir);
Set the absolute path of backup directory.
public function set_logfilename($logfilename);
Set the absolute path of the log file.
public function set_emailfilename($emailfilename);
Set the absolute path of the email list file.
public function check_and_backup()
Check all list files' differ and log the differs and backup new files and send email.
@file.list example
/usr/local/www/apache22/httpsdocs/inc_downloadhandler.php
/usr/local/www/apache22/httpsdocs/inc_downloadhandler_FT.php
/usr/local/www/apache22/httpdocs/about_us.php
/usr/local/www/apache22/httpdocs/index.php
zeng@sina.com
jevons@5124d.com
$ck_bk=new check_backup("/usr/local/www/apache22/CRON/Test_jevons");
$ck_bk->check_and_backup();
//This will general default configure of file.list and email.list and backup direcotry and log file base on the directory of your inputing.
//You must put the two files which are named file.list and email.list in your inputing directory.
**/
class check_backup
{
private $listfilename;
private $backdir ;
private $logfilename;
private $emailfilename;
public function __construct($defaultdir){
$this->listfilename = $defaultdir."/file.list";
$this->backdir = $defaultdir."/backup";
$this->logfilename =$defaultdir."/backup.diff.log";
$this->emailfilename =$defaultdir."/email.list";
echo $this->listfilename."/n";
echo $this->backdir."/n";
echo $this->logfilename."/n";
echo $this->emailfilename."/n";
}
public function set_listfilename($listfilename)
{
$this->listfilename=$listfilename;
}
public function set_backdir($backdir)
{
$this->backdir=$backdir;
}
public function set_logfilename($logfilename)
{
$this->logfilename=$logfilename;
}
public function set_emailfilename($emailfilename)
{
$this->emailfilename=$emailfilename;
}
private function get_backup_filename($srcfilename)
{ return str_replace("/","_",$srcfilename);
}
private 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 "";
}
private 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 "";
{
//check list file exist
if(!file_exists($this->listfilename))
{
echo "The file $this->listfilename is not exist./n";
return "";
}
$total = sizeof ($file_list);
for($i=0;$i<$total;$i++)
{
$ffull=trim($file_list[$i]);
$exfname=$this->get_backup_filename($ffull);
$cmpfile = $this->backdir."/".$exfname;
//echo $cmpfile."/n";
if(!file_exists($cmpfile)) continue;
//check diff
$cmdstring ="diff ".$ffull." ".$cmpfile;
echo $cmdstring."/n";
$retstring= `$cmdstring`;
$ret=strlen($retstring);
if($ret>0) $ret_list.=$ffull."/r/n";
return $ret_list;
}
{
if(!file_exists($this->backdir))
{
$cmdline="mkdir $this->backdir";
`$cmdline`;
}
$today=date("Y_m_d");
$targzpath=$this->get_filepath($this->backdir);
echo $cmdline."/n";
`$cmdline`;
}
private function backup_new()
{
//check list file exist
if(!file_exists($this->listfilename))
{
echo "The file $this->listfilename is not exist./n";
return;
}
$total = sizeof ($file_list);
{
$ffull=trim($file_list[$i]);
// $fname = $this->get_filename( $ffull);
$exfname= $this->get_backup_filename( $ffull);
$backname = $this->backdir."/".$exfname;
echo $cmdline."/n";
`$cmdline`;
}
private function send_email($ret_list)
{
echo $ret_list;
if(strlen($ret_list)>0)
{//send all email
// mail('jrubenstein@filetransfer.com.cn','File change list',$ret_list);
if(file_exists($this->emailfilename))
{
$emial_list = file("$this->emailfilename");
{
echo $emial_list[$i]."/n";
mail($emial_list[$i],'File change list',$ret_list);
}
}
}
{
error_log("/n$err", 3, $this->logfilename);
{
echo "Check the diff of the file list/n";
$ret_list=$this->check_diff();
//Log the result to log file
echo "Log the result to log file/n";
if(strlen($ret_list)>0)
{
//echo $ret_list;
$this->log_error_msg($ret_list);
}
echo "Tar and gzip old backup dir and remove all old list file/n";
$this->tar_gz_old();
echo "New backup all list file/n ";
$this->backup_new();
echo "Send all email /n";
$this->send_email($ret_list);
}
//$ck_bk=new check_backup("/usr/local/www/apache22/CRON/Test_jevons");
//$ck_bk->check_and_backup();