From f979578bbcefed095d306e332fa6bd251b2920cf Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 15 Mar 2018 16:47:22 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Impl=C3=A9mentation=20de=20base=20des=20bac?= =?UTF-8?q?kups?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autoloader.php | 1 + backup/index.html | 0 .../api/module/departement/saveController.php | 94 +++++++++++++++++++ build/database/core/DatabaseDriver.php | 5 +- build/database/core/Repo.php | 4 + build/database/repo/departement.php | 10 ++ composer.json | 3 +- composer.lock | 54 ++++++++++- config/modules.json | 15 +++ 9 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 backup/index.html create mode 100644 build/api/module/departement/saveController.php diff --git a/autoloader.php b/autoloader.php index ce28dce..b204753 100755 --- a/autoloader.php +++ b/autoloader.php @@ -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) diff --git a/backup/index.html b/backup/index.html new file mode 100644 index 0000000..e69de29 diff --git a/build/api/module/departement/saveController.php b/build/api/module/departement/saveController.php new file mode 100644 index 0000000..05175aa --- /dev/null +++ b/build/api/module/departement/saveController.php @@ -0,0 +1,94 @@ +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]; + } + } + +} \ No newline at end of file diff --git a/build/database/core/DatabaseDriver.php b/build/database/core/DatabaseDriver.php index 391e0ed..7787f71 100755 --- a/build/database/core/DatabaseDriver.php +++ b/build/database/core/DatabaseDriver.php @@ -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 ]; } diff --git a/build/database/core/Repo.php b/build/database/core/Repo.php index 96fdd2e..3d2568e 100644 --- a/build/database/core/Repo.php +++ b/build/database/core/Repo.php @@ -81,6 +81,10 @@ return static::$driver->pdo()->prepare("USE $dbName")->execute(); } + public static function getDBConfig() : array{ + return static::$driver->getConfig(); + } + diff --git a/build/database/repo/departement.php b/build/database/repo/departement.php index 946b4fc..43dea11 100644 --- a/build/database/repo/departement.php +++ b/build/database/repo/departement.php @@ -167,4 +167,14 @@ class departement 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); + } + } \ No newline at end of file diff --git a/composer.json b/composer.json index b11b7ca..48161df 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ }, "require": { "phpoffice/phpspreadsheet": "^1.1", - "phpstan/phpstan": "^0.9.2" + "phpstan/phpstan": "^0.9.2", + "ifsnop/mysqldump-php": "2.*" } } diff --git a/composer.lock b/composer.lock index b860bbe..af1c277 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/config/modules.json b/config/modules.json index ec622c8..20affef 100644 --- a/config/modules.json +++ b/config/modules.json @@ -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} + } + } } }, From 5d130114cf8d82e9c0c196a83095df50877f572b Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 15 Mar 2018 16:47:43 +0100 Subject: [PATCH 2/2] ajout du gitignore des backups --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d3c95d8..1e4dc39 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /vendor /public_html/css /public_html/js -/node_modules \ No newline at end of file +/node_modules +/backup/*