From 5fb9b989457b3be2b1629aaa28ebdcf3c034e137 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 19 Aug 2022 12:52:14 +0200 Subject: [PATCH] contao backup tool --- .gitignore | 1 + _contao_backup/README.md | 8 +++ _contao_backup/contao_backup.php | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 _contao_backup/README.md create mode 100644 _contao_backup/contao_backup.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57f1cb2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ \ No newline at end of file diff --git a/_contao_backup/README.md b/_contao_backup/README.md new file mode 100644 index 0000000..25d4236 --- /dev/null +++ b/_contao_backup/README.md @@ -0,0 +1,8 @@ +Installation: + +1. Create a folder named '_contao_backup' in your contao root directory on target system (webserver) +2. Copy the file 'contao_backup.php' into '_contao_backup' folder +3. Open this file on target system and edit DATABASE_NAME, -USER and -PASS according to target database credentials +4. Optional: edit number of backups (NUM_BACKUPS) +5. Add 'contao_backup.php' with path to crontab e.g. (every day at 1pm): + 0 1 * * * php /var/www/vhosts/memap.de/washnroll.memap.de/_contao_backup/contao_backup.php diff --git a/_contao_backup/contao_backup.php b/_contao_backup/contao_backup.php new file mode 100644 index 0000000..5ebfe9a --- /dev/null +++ b/_contao_backup/contao_backup.php @@ -0,0 +1,84 @@ + 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); + } + } +}