TrackManiaControl/application/core/Files/BackupUtil.php

114 lines
3.2 KiB
PHP
Raw Normal View History

2014-05-02 04:02:20 +02:00
<?php
namespace ManiaControl\Files;
2014-05-02 17:40:47 +02:00
use ManiaControl\ManiaControl;
2014-05-02 04:02:20 +02:00
/**
* Backup Utility Class
2014-05-02 17:40:47 +02:00
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-05-02 04:02:20 +02:00
*/
abstract class BackupUtil {
/*
* Constants
*/
2014-05-03 21:37:28 +02:00
const FOLDER_NAME_BACKUP = 'backup/';
2014-05-02 04:02:20 +02:00
/**
* Perform a Full Backup of ManiaControl
2014-05-02 17:40:47 +02:00
*
2014-05-02 04:02:20 +02:00
* @return bool
*/
public static function performFullBackup() {
2014-05-02 17:40:47 +02:00
$backupFolder = self::getBackupFolder();
2014-05-02 04:02:20 +02:00
$backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . date('y-m-d') . '_' . time() . '.zip';
2014-05-02 17:40:47 +02:00
$backupZip = new \ZipArchive();
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
2014-05-02 04:02:20 +02:00
trigger_error("Couldn't create Backup Zip!");
return false;
}
2014-05-02 17:40:47 +02:00
$excludes = array('.', '..', 'backup', 'logs', 'ManiaControl.log');
$pathInfo = pathInfo(ManiaControlDir);
2014-05-03 21:37:28 +02:00
$parentPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR;
2014-05-02 17:40:47 +02:00
$dirName = $pathInfo['basename'];
2014-05-02 04:02:20 +02:00
$backupZip->addEmptyDir($dirName);
2014-05-02 05:04:55 +02:00
self::zipDirectory($backupZip, ManiaControlDir, strlen($parentPath), $excludes);
2014-05-02 04:02:20 +02:00
$backupZip->close();
return true;
}
/**
* Get the Backup Folder Path and create it if necessary
2014-05-02 17:40:47 +02:00
*
2014-05-02 04:02:20 +02:00
* @return string
*/
private static function getBackupFolder() {
$backupFolder = ManiaControlDir . self::FOLDER_NAME_BACKUP;
if (!is_dir($backupFolder)) {
mkdir($backupFolder);
}
return $backupFolder;
}
/**
* Add a complete Directory to the ZipArchive
2014-05-02 17:40:47 +02:00
*
2014-05-02 04:02:20 +02:00
* @param \ZipArchive $zipArchive
2014-05-02 17:40:47 +02:00
* @param string $folderName
* @param int $prefixLength
* @param array $excludes
2014-05-02 04:02:20 +02:00
* @return bool
*/
private static function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array()) {
$folderHandle = opendir($folderName);
if (!$folderHandle) {
trigger_error("Couldn't open Folder '{$folderName}' for Backup!");
return false;
}
while (false !== ($file = readdir($folderHandle))) {
if (in_array($file, $excludes)) {
continue;
}
2014-05-03 21:37:28 +02:00
$filePath = $folderName . DIRECTORY_SEPARATOR . $file;
2014-05-02 04:02:20 +02:00
$localPath = substr($filePath, $prefixLength);
if (is_file($filePath)) {
$zipArchive->addFile($filePath, $localPath);
continue;
}
if (is_dir($filePath)) {
$zipArchive->addEmptyDir($localPath);
2014-05-02 15:12:28 +02:00
self::zipDirectory($zipArchive, $filePath, $prefixLength, $excludes);
2014-05-02 04:02:20 +02:00
continue;
}
}
closedir($folderHandle);
return true;
}
2014-05-02 17:40:47 +02:00
/**
* Perform a Backup of the Plugins
*
* @return bool
*/
public static function performPluginsBackup() {
$backupFolder = self::getBackupFolder();
$backupFileName = $backupFolder . 'backup_plugins_' . date('y-m-d') . '_' . time() . '.zip';
$backupZip = new \ZipArchive();
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
trigger_error("Couldn't create Backup Zip!");
return false;
}
$excludes = array('.', '..');
2014-05-03 21:37:28 +02:00
$pathInfo = pathInfo(ManiaControlDir . 'plugins');
$parentPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR;
2014-05-02 17:40:47 +02:00
$dirName = $pathInfo['basename'];
$backupZip->addEmptyDir($dirName);
2014-05-03 21:37:28 +02:00
self::zipDirectory($backupZip, ManiaControlDir . 'plugins', strlen($parentPath), $excludes);
2014-05-02 17:40:47 +02:00
$backupZip->close();
return true;
}
2014-05-02 04:02:20 +02:00
}