Three Step Backup
1. The zip-maker:
#!/usr/bin/php
<?php
$dest='/mnt/sbu/snapshot/';
$source[]='/var/www/html/folder1/';
$source[]='/var/www/html/folder2/';
$source[]='/etc/';
function comp($str){return '7za a -mmt=on -mx0 -tzip '.$GLOBALS['dest'].basename($str).'.zip'.' '.$str;}
$commands = array_map('comp',$source);
foreach ($commands as $cmd){exec($cmd);}
exit();
?>
2. Copy with timestamp:
#!/usr/bin/php
<?php
$source='/mnt/sbu/snapshot/';
$dest='/var/www/html/example/uploads/cat/';
$ext_list=['zip','7z',];
echo PHP_EOL;
if (!is_dir($source) && !is_readable($source)){die('Source problem');}
if (!is_dir($dest) && !is_writable($dest)){die('Destination problem');}
foreach (scandir($source) as $file){
$ext = pathinfo($file, PATHINFO_EXTENSION);
$noext = pathinfo($file, PATHINFO_FILENAME);
if ($file{0}=='.' || !in_array($ext,$ext_list)){continue;}
$oldfile=$source.$file;
$newfile=$dest.$noext.'.'.date("Y-m-d").'.zip';
if (!copy($oldfile,$newfile)){die('Copy problem');}
else {echo $oldfile.' copied to '.$newfile.PHP_EOL;}
}
echo PHP_EOL;
exit();
?>
3. Delete old files:
#!/usr/bin/php
<?php
$dir='/var/www/html/example/uploads/cat/';
$ext_list=['zip','7z',];
$year=date("Y");
$mon=date("m");
$day=date("d");
$flag=0;
echo PHP_EOL;
if (!is_dir($dir) && !is_writable($dir)){die('Directory unreachable');}
foreach (scandir($dir) as $file){
$ext = pathinfo($file, PATHINFO_EXTENSION);
$noext = pathinfo($file, PATHINFO_FILENAME);
$fdate = substr($noext,-10);
$fyear = substr($fdate,0,4);
$fmon = substr($fdate,5,2);
$fday = substr($fdate,8,2);
if ($file{0}=='.' || !in_array($ext,$ext_list)){continue;}
if (!checkdate($fmon,$fday,$fyear)){continue;}
if ($fday=='01'){continue;}
if ($mon===$fmon){continue;}
if ($mon=='01' && $fmon=='12'){continue;}
if ($mon=='02' && $fmon=='01'){continue;}
if ($mon=='03' && $fmon=='02'){continue;}
if ($mon=='04' && $fmon=='03'){continue;}
if ($mon=='05' && $fmon=='04'){continue;}
if ($mon=='06' && $fmon=='05'){continue;}
if ($mon=='07' && $fmon=='06'){continue;}
if ($mon=='08' && $fmon=='07'){continue;}
if ($mon=='09' && $fmon=='08'){continue;}
if ($mon=='10' && $fmon=='09'){continue;}
if ($mon=='11' && $fmon=='10'){continue;}
if ($mon=='12' && $fmon=='11'){continue;}
$file_to_delete=$dir.$file;
$flag=1;
if (!unlink($file_to_delete)){echo 'Error deleting file: '.$file_to_delete.PHP_EOL;}
else {echo 'File deleted successfully: '.$file_to_delete.PHP_EOL;}
}
if ($flag==0){echo 'No file to delete.'.PHP_EOL;}
echo PHP_EOL;
exit();
?>