| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- class backup
- {
- public static function create()
- {
- try {
- $nameNewFolder = date("Ymd-His");
- $newFolder = "/" . DIR_TEMP . $nameNewFolder;
- mkdir($newFolder, 0777);
- self::dumpMysql($newFolder);
- file::copyFolder("/" . DIR_DATAS, $newFolder . "/datas");
- $localZip = file::zip($newFolder, DIR_TEMP . $nameNewFolder);
- file::deleteFolder($newFolder);
- rename($localZip, DIR_BACKUP . $nameNewFolder . ".zip");
- return TRUE;
- } catch (Exception $ex) {
- return FALSE;
- }
- }
- public static function restore(string $_zip)
- {
- $tmpZip = str_replace(DIR_BACKUP, DIR_TEMP, $_zip);
- $tmpDatas = date("Ymd-His") . "-datas";
- copy($_zip, $tmpZip);
- $folder = file::unzip($tmpZip, DIR_TEMP);
- if(self::dumpMysqlRestore(DIR_TEMP . $folder . "/" . DB_NAME . ".Mysql")){
- rename(DIR_DATAS, DIR_TEMP . $tmpDatas);
- file::copyFolder(DIR_TEMP . $folder . "/datas", DIR_DATAS);
- if(is_dir(DIR_DATAS)){
- file::deleteFolder(DIR_TEMP . $tmpDatas);
- unlink($tmpZip);
- file::deleteFolder(DIR_TEMP . $folder);
- }
- return TRUE;
- }
- return FALSE;
- }
- public static function dumpMysql(string $_dir)
- {
- $backupFile = DB_NAME . ".Mysql";
- $command = "mysqldump --opt -h " . DB_HOST . " -u " . DB_USER . " -p" . DB_PASS . " " . DB_NAME . " > " . $_dir . "/" . $backupFile;
- try {
- system($command);
- return $backupFile;
- } catch (Exception $ex) {
- return FALSE;
- }
- }
- public static function dumpMysqlRestore(string $_backupFile)
- {
- $command = "mysql -h " . DB_HOST . " -u " . DB_USER . " -p" . DB_PASS . " " . DB_NAME . " < " . $_backupFile;
- try {
- system($command);
- return TRUE;
- } catch (Exception $ex) {
- return FALSE;
- }
- }
- public static function jsonRestore()
- {
- json::create("salaries");
- json::create("excel");
- json::create("excel-proweb");
- json::create("events");
- json::create("users");
- json::create("salaries-proweb");
- }
- }
|