PHP File Serever Automatically Backup and Send Email And Last 3 Days Deleted
<?php
$rootPath = realpath('./');
// Initialize archive object
$zip = new ZipArchive();
$name=date("d-M-Y s")."_caytech.zip";
$destination="../backup/".$name;
$days =3;
$path = '../backup/';
$filetypes_to_delete = array("zip");
// Open the directory
if ($handle = opendir($path))
{
// Loop through the directory
while (false !== ($file = readdir($handle)))
{
// Check the file we're doing is actually a file
if (is_file($path.$file))
{
$file_info = pathinfo($path.$file);
if (isset($file_info['extension']) && in_array(strtolower($file_info['extension']), $filetypes_to_delete))
{
//echo filemtime($path.$file)." = ".( time() - ( $days * 24 * 60 * 60 ) )."<Br/>";
// Check if the file is older than X days old
if (filemtime($path.$file) < ( time() - ( $days * 24 * 60 * 60 ) ) )
{
$mystring=pathinfo($file);
$findme="_caytech";
if( strpos($mystring['filename'], '_caytech') !== false )
{
// Do the deletion
unlink($path.$file);
}
}
}
}
}
}
$zipName=file_newname($destination, $name);
function file_newname($path, $filename){
if ($pos = strrpos($filename, '.')) {
$name = substr($filename, 0, $pos);
$ext = substr($filename, $pos);
} else {
$name = $filename;
}
$newpath = $path.'/'.$filename;
$newname = $filename;
$counter = 0;
while (file_exists($newpath)) {
$newname = $name .'_'. $counter . $ext;
$newpath = $path.'/'.$newname;
$counter++;
}
return $newname;
}
$zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Initialize empty "delete list"
$filesToDelete = array();
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$cdname= basename(dirname($relativePath));
/***************************************************
Folder Name Exceptions
****************************************************/
//$foldername = array("Test","admin","catalog","download","images","paymill","system");
$foldername = array("Test");
if(in_array($cdname, $foldername))
{
$zip->addFile($filePath, $relativePath);
}
else
{
//$zip->addFile($filePath, $relativePath);
}
// Add current file to "delete list"
// delete it later cause ZipArchive create archive only after calling close function and ZipArchive lock files until archive created)
// echo strcmp("Hello","Hello")$file->getFilename();
if(pathinfo($file->getFilename(), PATHINFO_EXTENSION)=='zip' && strpos($file->getFilename(),'filebackup'))
{
if ($file->getFilename())
{
$filesToDelete[] = $filePath;
}
}
}
}
// Zip archive will be created only after closing object
$zip->close();
$path =$_SERVER['DOCUMENT_ROOT']."/backup/";
$recipients = array(
"jaydeep.king25@gmail.com",
"test@gmail.com."
// more emails
);
$to = implode(',', $recipients);
//echo $to;
$subject = "testAutomatic Backup of your $zipName installation";
$txt = "Hello sir/medam <br/><br/><p>The automatic backup of your <b>$zipName</b> installation was completed successfully.</p><br/>
The details are as follows :<br/><br/>
Backup Path : <b style='color:green'>$path</b><br/>
Backup file name : <b style='color:green'>$zipName</b><br/>
Status : <b style='color:green'>Success</b><br/>
<p>You can download the backup from the Backups Section in Softaculous.</p>
<br/>
Regards & Thanks<br/>
backup@caytechnology.fr
";
$headers = "From: test@gmkist.fr";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//mail($to,$subject,$txt,$headers);
foreach ($filesToDelete as $file)
{
//unlink($file);
}
?>
0 comments:
Post a Comment