2018-03-03 15:03:28 +01:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-03-03 15:03:28 +01:00
|
|
|
|
2018-09-19 16:51:37 +02:00
|
|
|
use Ifsnop\Mysqldump\Mysqldump;
|
2021-09-17 12:18:21 +02:00
|
|
|
use Util\FileSystem;
|
2020-09-19 10:18:39 +02:00
|
|
|
use Util\Generator;
|
2018-12-29 12:03:22 +01:00
|
|
|
use Util\Zip;
|
2018-09-19 16:51:37 +02:00
|
|
|
|
2018-03-03 15:03:28 +01:00
|
|
|
/**
|
|
|
|
* Classe per la gestione dei backup.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
class Backup
|
|
|
|
{
|
|
|
|
/** @var string Pattern per i nomi dei backup */
|
2024-03-03 18:42:16 +01:00
|
|
|
public const PATTERN = 'OSM backup YYYY-m-d H_i_s AAAAAAA';
|
2018-03-03 15:03:28 +01:00
|
|
|
|
2019-01-04 11:39:30 +01:00
|
|
|
/** @var array Elenco delle variabili che identificano i backup giornalieri */
|
2018-03-03 15:03:28 +01:00
|
|
|
protected static $daily_replaces = [
|
2018-03-19 15:30:16 +01:00
|
|
|
'YYYY', 'm', 'd',
|
2018-03-03 15:03:28 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il percorso su previsto per il salvataggio dei backup.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getDirectory()
|
|
|
|
{
|
|
|
|
$result = App::getConfig()['backup_dir'];
|
|
|
|
|
|
|
|
$result = rtrim($result, '/');
|
2019-02-09 14:44:30 +01:00
|
|
|
if (!directory($result) || !is_writable($result)) {
|
2024-01-15 15:30:45 +01:00
|
|
|
// throw new UnexpectedValueException();
|
2018-03-03 15:03:28 +01:00
|
|
|
}
|
|
|
|
|
2018-06-26 16:33:15 +02:00
|
|
|
return slashes($result);
|
2018-03-03 15:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce l'elenco dei backup disponibili.
|
|
|
|
*
|
|
|
|
* @param string $pattern Eventuale pattern alternativo
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getList($pattern = null)
|
|
|
|
{
|
2020-03-09 15:29:58 +01:00
|
|
|
$directory = self::getDirectory();
|
|
|
|
if (!is_writable($directory) || !is_readable($directory)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-03-03 15:03:28 +01:00
|
|
|
// Costruzione del pattern
|
|
|
|
if (empty($pattern)) {
|
|
|
|
$replaces = self::getReplaces();
|
|
|
|
$regexs = array_column($replaces, 'regex');
|
|
|
|
|
|
|
|
$pattern = str_replace(array_keys($replaces), array_values($regexs), self::PATTERN);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Individuazione dei backup
|
|
|
|
$backups = Symfony\Component\Finder\Finder::create()
|
|
|
|
->name('/^'.$pattern.'/')
|
|
|
|
->sortByName()
|
2020-03-09 15:29:58 +01:00
|
|
|
->in($directory)
|
2019-02-09 14:44:30 +01:00
|
|
|
->depth('== 0');
|
2018-03-03 15:03:28 +01:00
|
|
|
|
|
|
|
$results = [];
|
|
|
|
foreach ($backups as $backup) {
|
|
|
|
$results[] = $backup->getRealPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce i valori utilizzati sulle variabili sostituite.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function readName($string)
|
|
|
|
{
|
2020-09-19 10:18:39 +02:00
|
|
|
return Generator::read(self::PATTERN, basename($string));
|
2018-03-03 15:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-26 18:02:05 +02:00
|
|
|
* Controlla se il backup giornaliero è stato effettuato.
|
2018-03-03 15:03:28 +01:00
|
|
|
*
|
2019-03-04 18:42:56 +01:00
|
|
|
* @return bool|null
|
2018-03-03 15:03:28 +01:00
|
|
|
*/
|
2019-08-26 18:02:05 +02:00
|
|
|
public static function isDailyComplete()
|
2018-03-03 15:03:28 +01:00
|
|
|
{
|
|
|
|
// Costruzione del pattern
|
|
|
|
$replaces = self::getReplaces();
|
|
|
|
|
|
|
|
foreach ($replaces as $key => $replace) {
|
|
|
|
if (in_array($key, self::$daily_replaces)) {
|
|
|
|
$replaces[$key] = $replace['value'];
|
|
|
|
} else {
|
|
|
|
$replaces[$key] = $replace['regex'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$pattern = str_replace(array_keys($replaces), array_values($replaces), self::PATTERN);
|
|
|
|
|
|
|
|
// Individuazione dei backup
|
|
|
|
$backups = self::getList($pattern);
|
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
return !empty($backups);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Effettua il backup giornaliero.
|
|
|
|
*
|
|
|
|
* @return bool|null
|
|
|
|
*/
|
|
|
|
public static function daily()
|
|
|
|
{
|
2018-03-03 15:03:28 +01:00
|
|
|
// Creazione del backup eventuale
|
2019-08-26 18:02:05 +02:00
|
|
|
if (!self::isDailyComplete()) {
|
2018-03-03 15:03:28 +01:00
|
|
|
return self::create();
|
|
|
|
}
|
2019-08-27 15:42:13 +02:00
|
|
|
|
|
|
|
return false;
|
2018-03-03 15:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Esegue il backup del progetto.
|
|
|
|
*
|
2024-03-04 02:07:58 +01:00
|
|
|
* @param array $ignores eventuali dirs o files da ignorare
|
2024-03-02 23:04:03 +01:00
|
|
|
*
|
2018-03-03 15:03:28 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2024-03-04 02:07:58 +01:00
|
|
|
public static function create($ignores)
|
2018-03-03 15:03:28 +01:00
|
|
|
{
|
2021-09-17 12:18:21 +02:00
|
|
|
self::checkSpace();
|
|
|
|
|
2018-03-03 15:03:28 +01:00
|
|
|
$backup_dir = self::getDirectory();
|
2024-03-04 02:07:58 +01:00
|
|
|
$backup_name = tr(self::getNextName(), [ 'AAAAAAA' => ($ignores['dirs'] || $ignores['files'] )? 'PARTIAL' : 'FULL' ]);
|
2018-03-03 15:03:28 +01:00
|
|
|
|
|
|
|
set_time_limit(0);
|
|
|
|
|
|
|
|
// Backup del database
|
|
|
|
$database_file = self::getDatabaseDirectory().'/database.sql';
|
|
|
|
self::database($database_file);
|
|
|
|
|
2024-03-04 02:07:58 +01:00
|
|
|
// Files e dirs da ignorare di default
|
|
|
|
$default_ignores = [
|
2018-03-03 15:03:28 +01:00
|
|
|
'files' => [
|
|
|
|
'config.inc.php',
|
2022-09-23 14:55:00 +02:00
|
|
|
'*.lock',
|
|
|
|
'*.phar',
|
|
|
|
'*.log',
|
2018-03-03 15:03:28 +01:00
|
|
|
],
|
|
|
|
'dirs' => [
|
|
|
|
'node_modules',
|
|
|
|
'tests',
|
2018-11-02 13:18:32 +01:00
|
|
|
'tmp',
|
2022-09-23 14:55:00 +02:00
|
|
|
'.git',
|
2024-03-02 23:04:03 +01:00
|
|
|
'.github',
|
2018-03-03 15:03:28 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2024-03-04 02:07:58 +01:00
|
|
|
$ignores = array_merge_recursive($ignores, $default_ignores);
|
2024-03-02 23:04:03 +01:00
|
|
|
|
2024-03-04 02:07:58 +01:00
|
|
|
// Escludo la directory dei backup
|
2020-10-29 16:48:37 +01:00
|
|
|
if (string_starts_with($backup_dir, slashes(base_dir()))) {
|
2018-12-12 17:26:25 +01:00
|
|
|
$ignores['dirs'][] = basename($backup_dir);
|
|
|
|
}
|
|
|
|
|
2018-03-03 15:03:28 +01:00
|
|
|
// Creazione backup in formato ZIP
|
|
|
|
if (extension_loaded('zip')) {
|
2018-09-19 16:51:37 +02:00
|
|
|
$result = Zip::create([
|
2020-09-23 13:36:37 +02:00
|
|
|
base_dir(),
|
2018-09-25 16:47:44 +02:00
|
|
|
self::getDatabaseDirectory(),
|
2018-09-19 16:51:37 +02:00
|
|
|
], $backup_dir.'/'.$backup_name.'.zip', $ignores);
|
2018-03-03 15:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creazione backup attraverso la copia dei file
|
|
|
|
else {
|
2018-09-19 16:51:37 +02:00
|
|
|
$result = copyr([
|
2020-09-23 13:36:37 +02:00
|
|
|
base_dir(),
|
2018-09-25 16:47:44 +02:00
|
|
|
self::getDatabaseDirectory(),
|
2018-09-19 16:51:37 +02:00
|
|
|
], $backup_dir.'/'.$backup_name.'.zip', $ignores);
|
2018-03-03 15:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rimozione cartella temporanea
|
|
|
|
delete($database_file);
|
|
|
|
|
|
|
|
self::cleanup();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Effettua il dump del database.
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
public static function database($file)
|
|
|
|
{
|
|
|
|
$config = App::getConfig();
|
|
|
|
|
2018-09-19 16:51:37 +02:00
|
|
|
$dump = new Mysqldump('mysql:host='.$config['db_host'].';dbname='.$config['db_name'], $config['db_username'], $config['db_password'], [
|
2018-03-03 15:03:28 +01:00
|
|
|
'add-drop-table' => true,
|
2019-03-04 18:42:56 +01:00
|
|
|
'add-locks' => false,
|
2018-03-03 15:03:28 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dump->start($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rimuove i backup più vecchi.
|
|
|
|
*/
|
|
|
|
public static function cleanup()
|
|
|
|
{
|
2018-07-09 10:44:54 +02:00
|
|
|
$max_backups = intval(setting('Numero di backup da mantenere'));
|
2018-03-03 15:03:28 +01:00
|
|
|
|
|
|
|
$backups = self::getList();
|
|
|
|
$count = count($backups);
|
|
|
|
|
|
|
|
// Rimozione dei backup più vecchi
|
|
|
|
for ($i = 0; $i < $count - $max_backups; ++$i) {
|
|
|
|
delete($backups[$i]);
|
|
|
|
}
|
|
|
|
}
|
2018-09-19 16:51:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ripristina un backup esistente.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
|
|
|
public static function restore($path, $cleanup = true)
|
|
|
|
{
|
|
|
|
$database = database();
|
|
|
|
$extraction_dir = is_dir($path) ? $path : Zip::extract($path);
|
|
|
|
|
2019-11-13 19:27:25 +01:00
|
|
|
// TODO: Forzo il log out di tutti gli utenti e ne impedisco il login
|
|
|
|
// fino a ripristino ultimato
|
|
|
|
|
2018-09-19 16:51:37 +02:00
|
|
|
// Rimozione del database
|
2020-09-23 13:36:37 +02:00
|
|
|
$tables = include base_dir().'/update/tables.php';
|
2018-09-19 16:51:37 +02:00
|
|
|
|
|
|
|
// Ripristino del database
|
2018-09-24 18:10:16 +02:00
|
|
|
$database_file = $extraction_dir.'/database.sql';
|
|
|
|
if (file_exists($database_file)) {
|
|
|
|
$database->query('SET foreign_key_checks = 0');
|
2019-01-03 11:04:28 +01:00
|
|
|
foreach ($tables as $table) {
|
|
|
|
$database->query('DROP TABLE IF EXISTS `'.$table.'`');
|
2018-09-24 18:10:16 +02:00
|
|
|
}
|
2018-12-14 09:46:35 +01:00
|
|
|
$database->query('DROP TABLE IF EXISTS `updates`');
|
2018-09-24 18:10:16 +02:00
|
|
|
|
|
|
|
// Ripristino del database
|
|
|
|
$database->multiQuery($database_file);
|
|
|
|
$database->query('SET foreign_key_checks = 1');
|
|
|
|
}
|
2018-09-19 16:51:37 +02:00
|
|
|
|
|
|
|
// Salva il file di configurazione
|
2020-09-23 13:36:37 +02:00
|
|
|
$config = file_get_contents(base_dir().'/config.inc.php');
|
2018-09-19 16:51:37 +02:00
|
|
|
|
|
|
|
// Copia i file dalla cartella temporanea alla root
|
2020-09-23 13:36:37 +02:00
|
|
|
copyr($extraction_dir, base_dir());
|
2018-09-19 16:51:37 +02:00
|
|
|
|
|
|
|
// Ripristina il file di configurazione dell'installazione
|
2020-09-23 13:36:37 +02:00
|
|
|
file_put_contents(base_dir().'/config.inc.php', $config);
|
2018-09-19 16:51:37 +02:00
|
|
|
|
|
|
|
// Pulizia
|
|
|
|
if (!empty($cleanup)) {
|
|
|
|
delete($extraction_dir);
|
|
|
|
}
|
2020-09-23 13:36:37 +02:00
|
|
|
delete(base_dir().'/database.sql');
|
2018-09-19 16:51:37 +02:00
|
|
|
}
|
2018-12-29 12:03:22 +01:00
|
|
|
|
2021-09-17 12:18:21 +02:00
|
|
|
/**
|
|
|
|
* Effettua i controlli relativi allo spazio disponibile per l'esecuzione del backup;.
|
|
|
|
*/
|
|
|
|
public static function checkSpace()
|
|
|
|
{
|
|
|
|
$scarto = 1.1;
|
|
|
|
|
|
|
|
// Informazioni di base sui limiti di spazio
|
|
|
|
$spazio_libero = disk_free_space('.');
|
|
|
|
if (!empty(setting('Soft quota'))) {
|
|
|
|
$soft_quota = (float) setting('Soft quota'); // Impostazione in GB
|
|
|
|
$soft_quota = $soft_quota * (1024 ** 3); // Trasformazione in GB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Informazioni sullo spazio occupato
|
|
|
|
$spazio_occupato = $spazio_necessario = FileSystem::folderSize(base_dir(), ['htaccess']);
|
|
|
|
$cartelle_ignorate = [
|
|
|
|
self::getDirectory(),
|
|
|
|
'node_modules',
|
|
|
|
'tests',
|
|
|
|
'tmp',
|
|
|
|
];
|
|
|
|
foreach ($cartelle_ignorate as $path) {
|
|
|
|
$spazio_necessario -= FileSystem::folderSize($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errori visualizzati
|
|
|
|
if (isset($soft_quota) && $soft_quota < ($spazio_necessario + $spazio_occupato) * $scarto) {
|
|
|
|
throw new InvalidArgumentException('Spazio disponibile in esaurimento');
|
2024-01-15 15:30:45 +01:00
|
|
|
} elseif ($spazio_libero < $spazio_necessario * $scarto) {
|
2021-09-17 12:18:21 +02:00
|
|
|
throw new InvalidArgumentException('Spazio del server in esaurimento');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:03:22 +01:00
|
|
|
/**
|
2019-01-04 11:39:30 +01:00
|
|
|
* Restituisce il percorso su cui salvare temporaneamente il dump del database.
|
2018-12-29 12:03:22 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getDatabaseDirectory()
|
|
|
|
{
|
|
|
|
$result = self::getDirectory().'/database';
|
|
|
|
|
|
|
|
if (!directory($result)) {
|
|
|
|
throw new UnexpectedValueException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return slashes($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce l'elenco delle variabili da sostituire normalizzato per l'utilizzo.
|
|
|
|
*/
|
|
|
|
protected static function getReplaces()
|
|
|
|
{
|
2020-09-19 10:18:39 +02:00
|
|
|
return Generator::getReplaces();
|
2018-12-29 12:03:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il nome previsto per il backup successivo.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getNextName()
|
|
|
|
{
|
2020-09-19 10:18:39 +02:00
|
|
|
return Generator::generate(self::PATTERN);
|
2018-12-29 12:03:22 +01:00
|
|
|
}
|
2018-03-03 15:03:28 +01:00
|
|
|
}
|