Implémentation de base des backups

This commit is contained in:
Unknown 2018-03-15 16:47:22 +01:00 committed by SeekDaSky
parent e1bdac7f5b
commit 959a1047d8
9 changed files with 183 additions and 3 deletions

View File

@ -6,6 +6,7 @@
if( !defined('__CONFIG__') ) define('__CONFIG__', __ROOT__.'/config' );
if( !defined('__BUILD__') ) define('__BUILD__', __ROOT__.'/build' );
if( !defined('__PUBLIC__') ) define('__PUBLIC__', __ROOT__.'/public_html' );
if( !defined('__BACKUP__') ) define('__BACKUP__', __ROOT__.'/backup' );
/* ACTIVE LE DEBUGGAGE (WARNING + EXCEPTION)

0
backup/index.html Normal file
View File

View File

@ -0,0 +1,94 @@
<?php
/**
* Created by PhpStorm.
* User: lucas
* Date: 15/03/18
* Time: 15:50
*/
namespace api\module\departement;
use database\core\Repo;
use database\repo\departement;
use Ifsnop\Mysqldump\Mysqldump;
class saveController
{
private $backupPath = "";
private function initDir(string $dbName){
if(!is_dir(__BACKUP__."/$dbName/")){
mkdir(__BACKUP__."/$dbName/");
}
$this->backupPath =__BACKUP__."/$dbName/";
}
private function scandir(string $path) : array {
//scan the directory
$arr = scandir($path);
//strip the useless "." and ".."
unset($arr[0],$arr[1]);
//make the arry start at 0 again
$arr = array_values($arr);
return $arr;
}
public function get($args){
$this->initDir($_SESSION["CurrentDatabase"]);
//strip extensions
$backupNames = array_map(function($e){
return pathinfo($e, PATHINFO_FILENAME);
}, $this->scandir($this->backupPath));
return ["data" => $backupNames];
}
public function post($args){
$this->initDir($_SESSION["CurrentDatabase"]);
$backupName = "";
extract($args);
//if the backup name is empty we create it
if($backupName == ""){
try {
$conf = Repo::getDBConfig();
$dump = new Mysqldump("mysql:host={$conf["host"]};dbname={$conf["dbname"]}", $conf["username"], $conf["password"],
[
"compress" => Mysqldump::GZIP
]);
$date = date("Y-W-d");
$dump->start($this->backupPath.$date.".sql");
return ["success" => true,"backupName" => $date];
} catch (\Exception $e) {
return ["success" => false];
}
}else{
//read the backup
ob_start();
readgzfile($this->backupPath.$backupName.".sql");
$sql = ob_get_clean();
/** @var departement $depRepo */
$depRepo = Repo::getRepo("departement");
$depRepo->restore($sql);
return ["success" => true];
}
}
}

View File

@ -128,6 +128,8 @@
if(isset($_SESSION['CurrentDatabase']) && is_string($_SESSION['CurrentDatabase'])){
$conf[$label]['local']['dbname'] = $_SESSION['CurrentDatabase'];
}else{
$_SESSION["CurrentDatabase"] = $conf[$label]['local']['dbname'];
}
self::$instance[$label] = new DatabaseDriver($conf[$label]['local']['host'], $conf[$label]['local']['dbname'], $conf[$label]['local']['user'], $conf[$label]['local']['password'],$conf[$label]['local']['debug']);
@ -208,7 +210,8 @@
return [
'host' => $this->host,
'dbname' => $this->dbname,
'username' => $this->username
'username' => $this->username,
'password' => $this->password
];
}

View File

@ -81,6 +81,10 @@
return static::$driver->pdo()->prepare("USE $dbName")->execute();
}
public static function getDBConfig() : array{
return static::$driver->getConfig();
}

View File

@ -167,4 +167,14 @@ class department extends Repo_i
}
public function restore(string $SQL) : bool{
//get the list of command to execute
$this->pdo->exec("DROP DATABASE ".$_SESSION['CurrentDatabase'].";");
$this->pdo->exec("CREATE DATABASE ".$_SESSION['CurrentDatabase'].";");
$this->pdo->exec("USE ".$_SESSION['CurrentDatabase'].";");
return $this->pdo->exec($SQL);
}
}

View File

@ -26,6 +26,7 @@
},
"require": {
"phpoffice/phpspreadsheet": "^1.1",
"phpstan/phpstan": "^0.9.2"
"phpstan/phpstan": "^0.9.2",
"ifsnop/mysqldump-php": "2.*"
}
}

54
composer.lock generated
View File

@ -4,8 +4,60 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "18a1824304295027ad9aa6050280670b",
"content-hash": "28e31e7da9313d9726a26a4f36bf06a3",
"packages": [
{
"name": "ifsnop/mysqldump-php",
"version": "v2.4",
"source": {
"type": "git",
"url": "https://github.com/ifsnop/mysqldump-php.git",
"reference": "2a633b3da5db1e5bdc39c1b71c697ef197c39eeb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ifsnop/mysqldump-php/zipball/2a633b3da5db1e5bdc39c1b71c697ef197c39eeb",
"reference": "2a633b3da5db1e5bdc39c1b71c697ef197c39eeb",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"squizlabs/php_codesniffer": "1.*"
},
"type": "library",
"autoload": {
"psr-4": {
"Ifsnop\\": "src/Ifsnop/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Diego Torres",
"homepage": "https://github.com/ifsnop",
"role": "Developer"
}
],
"description": "This is a php version of linux's mysqldump in terminal \"$ mysqldump -u username -p...\"",
"homepage": "https://github.com/ifsnop/mysqldump-php",
"keywords": [
"backup",
"database",
"dump",
"export",
"mysql",
"mysqldump",
"pdo",
"sqlite"
],
"time": "2018-03-07T22:27:23+00:00"
},
{
"name": "jean85/pretty-package-versions",
"version": "1.1",

View File

@ -95,6 +95,21 @@
"par": {
}
}
},
"save":{
"GET": {
"des": "Get the list of the saves of the department database",
"per": [],
"par": {
}
},
"POST": {
"des": "Create a backup if the name is empty, execute the backup if the name is set",
"per": [],
"par": {
"backupName": {"des": "Backup name", "typ": "varchar(10,10,alphanumeric)", "opt" : true}
}
}
}
},