|
|
|
@@ -0,0 +1,84 @@ |
|
|
|
<?php |
|
|
|
|
|
|
|
const DATABASE_NAME = 'test'; |
|
|
|
const DATABASE_USER = 'root'; |
|
|
|
const DATABASE_PASS = ''; |
|
|
|
|
|
|
|
const NUM_BACKUPS = 3; |
|
|
|
const FILE_SUFFIX = 'backup_'; |
|
|
|
|
|
|
|
$excludeDirs = [ |
|
|
|
'.git' => 1, |
|
|
|
'.idea' => 1, |
|
|
|
'var' => 1, |
|
|
|
'vendor' => 1, |
|
|
|
]; |
|
|
|
|
|
|
|
$curDirName = basename(__DIR__); |
|
|
|
$curFile = basename(__FILE__, '.php'); |
|
|
|
$excludeDirs[$curDirName] = 1; |
|
|
|
|
|
|
|
$curFiles = scandir(__DIR__); |
|
|
|
$backupFileNames = []; |
|
|
|
$fileNameLowestDate = null; |
|
|
|
foreach ($curFiles as $cFileName) { |
|
|
|
$datePos = strpos($cFileName, FILE_SUFFIX); |
|
|
|
if ($datePos !== false) { |
|
|
|
if ($fileNameLowestDate === null || $cFileName < $fileNameLowestDate) { |
|
|
|
$fileNameLowestDate = $cFileName; |
|
|
|
} |
|
|
|
$backupFileNames[] = $cFileName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (count($backupFileNames) > NUM_BACKUPS) { |
|
|
|
unlink($fileNameLowestDate); |
|
|
|
} |
|
|
|
|
|
|
|
$b = __DIR__; |
|
|
|
$parentDir = substr(__DIR__, 0, strlen($curDirName) * -1); |
|
|
|
$shellCommand = 'mysqldump --user=' . DATABASE_USER . ' --password="' . DATABASE_PASS . '" ' . DATABASE_NAME . ' > dump.sql'; |
|
|
|
shell_exec($shellCommand); |
|
|
|
|
|
|
|
$zip = new ZipArchive(); |
|
|
|
$datetime = new DateTime(); |
|
|
|
$filename = FILE_SUFFIX . $datetime->format('Y-m-d') . '.zip'; |
|
|
|
|
|
|
|
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) { |
|
|
|
exit("cannot open <$filename>\n"); |
|
|
|
} |
|
|
|
$zip->addFile(__DIR__.'/dump.sql'); |
|
|
|
|
|
|
|
createZip($zip, $parentDir, $excludeDirs); |
|
|
|
|
|
|
|
$zip->close(); |
|
|
|
unlink('dump.sql'); |
|
|
|
|
|
|
|
function createZip($zip, $dir, $excludeDirs = []){ |
|
|
|
if (is_dir($dir) && !array_key_exists(basename($dir), $excludeDirs)){ |
|
|
|
if ($dh = opendir($dir)){ |
|
|
|
while (($file = readdir($dh)) !== false){ |
|
|
|
// If file |
|
|
|
if (is_file($dir.$file)) { |
|
|
|
if($file != '' && $file != '.' && $file != '..'){ |
|
|
|
$zip->addFile($dir.$file); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// If directory |
|
|
|
if(is_dir($dir.$file) ){ |
|
|
|
|
|
|
|
if($file != '' && $file != '.' && $file != '..'){ |
|
|
|
// Add empty directory |
|
|
|
$zip->addEmptyDir($dir.$file); |
|
|
|
$folder = $dir.$file.'/'; |
|
|
|
// Read data of the folder |
|
|
|
createZip($zip, $folder, $excludeDirs); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
closedir($dh); |
|
|
|
} |
|
|
|
} |
|
|
|
} |