backup zipping improved

This commit is contained in:
Steffen Schröder 2014-05-07 14:36:17 +02:00
parent b5d781076d
commit ba01624765

View File

@ -24,20 +24,22 @@ abstract class BackupUtil {
*/ */
public static function performFullBackup() { public static function performFullBackup() {
$backupFolder = self::getBackupFolder(); $backupFolder = self::getBackupFolder();
$backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . date('y-m-d') . '_' . time() . '.zip'; if (!$backupFolder) {
return false;
}
$backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . date('y-m-d_H-i') . '_' . time() . '.zip';
$backupZip = new \ZipArchive(); $backupZip = new \ZipArchive();
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) { if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
trigger_error("Couldn't create Backup Zip!"); trigger_error("Couldn't create Backup Zip!");
return false; return false;
} }
$excludes = array('.', '..', 'backup', 'logs', 'ManiaControl.log'); $excludes = array('backup', 'logs', 'ManiaControl.log');
$pathInfo = pathInfo(ManiaControlDir); $pathInfo = pathInfo(ManiaControlDir);
$parentPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR; $parentPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR;
$dirName = $pathInfo['basename']; $dirName = $pathInfo['basename'];
$backupZip->addEmptyDir($dirName); $backupZip->addEmptyDir($dirName);
self::zipDirectory($backupZip, ManiaControlDir, strlen($parentPath), $excludes); self::zipDirectory($backupZip, ManiaControlDir, strlen($parentPath), $excludes);
$backupZip->close(); return $backupZip->close();;
return true;
} }
/** /**
@ -47,8 +49,13 @@ abstract class BackupUtil {
*/ */
private static function getBackupFolder() { private static function getBackupFolder() {
$backupFolder = ManiaControlDir . self::FOLDER_NAME_BACKUP; $backupFolder = ManiaControlDir . self::FOLDER_NAME_BACKUP;
if (!is_dir($backupFolder)) { if (!is_dir($backupFolder) && !mkdir($backupFolder)) {
mkdir($backupFolder); trigger_error("Couldn't create Backup Folder!");
return false;
}
if (!is_writeable($backupFolder)) {
trigger_error("ManiaControl doesn't have the necessary Writing Rights for the Backup Folder!");
return false;
} }
return $backupFolder; return $backupFolder;
} }
@ -64,11 +71,14 @@ abstract class BackupUtil {
*/ */
private static function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array()) { private static function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array()) {
$folderHandle = opendir($folderName); $folderHandle = opendir($folderName);
if (!$folderHandle) { if (!is_resource($folderHandle)) {
trigger_error("Couldn't open Folder '{$folderName}' for Backup!"); trigger_error("Couldn't open Folder '{$folderName}' for Backup!");
return false; return false;
} }
while (false !== ($file = readdir($folderHandle))) { while (false !== ($file = readdir($folderHandle))) {
if (substr($file, 0, 1) === '.') {
continue;
}
if (in_array($file, $excludes)) { if (in_array($file, $excludes)) {
continue; continue;
} }
@ -95,19 +105,21 @@ abstract class BackupUtil {
*/ */
public static function performPluginsBackup() { public static function performPluginsBackup() {
$backupFolder = self::getBackupFolder(); $backupFolder = self::getBackupFolder();
$backupFileName = $backupFolder . 'backup_plugins_' . date('y-m-d') . '_' . time() . '.zip'; if (!$backupFolder) {
return false;
}
$backupFileName = $backupFolder . 'backup_plugins_' . ManiaControl::VERSION . date('y-m-d_H-i') . '_' . time() . '.zip';
$backupZip = new \ZipArchive(); $backupZip = new \ZipArchive();
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) { if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
trigger_error("Couldn't create Backup Zip!"); trigger_error("Couldn't create Backup Zip!");
return false; return false;
} }
$excludes = array('.', '..'); $excludes = array();
$pathInfo = pathInfo(ManiaControlDir . 'plugins'); $pathInfo = pathInfo(ManiaControlDir . 'plugins');
$parentPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR; $parentPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR;
$dirName = $pathInfo['basename']; $dirName = $pathInfo['basename'];
$backupZip->addEmptyDir($dirName); $backupZip->addEmptyDir($dirName);
self::zipDirectory($backupZip, ManiaControlDir . 'plugins', strlen($parentPath), $excludes); self::zipDirectory($backupZip, ManiaControlDir . 'plugins', strlen($parentPath), $excludes);
$backupZip->close(); return $backupZip->close();;
return true;
} }
} }