Преглед на файлове

contao backup tool

master
Daniel преди 3 години
ревизия
5fb9b98945
променени са 3 файла, в които са добавени 93 реда и са изтрити 0 реда
  1. +1
    -0
      .gitignore
  2. +8
    -0
      _contao_backup/README.md
  3. +84
    -0
      _contao_backup/contao_backup.php

+ 1
- 0
.gitignore Целия файл

@@ -0,0 +1 @@
/.idea/

+ 8
- 0
_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

+ 84
- 0
_contao_backup/contao_backup.php Целия файл

@@ -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);
}
}
}

Зареждане…
Отказ
Запис