| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- class backup
- {
- public static function create()
- {
- try {
- $nameNewFolder = date("Ymd-His");
- $newFolder = "/" . DIR_TEMP . $nameNewFolder;
- mkdir($newFolder, 0777);
- $backupFile = self::dumpMysql($newFolder);
- rename($backupFile, $newFolder . "/" . $backupFile);
- file::copyFolder("/" . DIR_DATAS, $newFolder . "/datas");
- $localZip = self::zip($newFolder, DIR_TEMP . $nameNewFolder);
- self::deleteFolder($newFolder);
- rename($localZip, DIR_DATAS_BACKUP . $nameNewFolder . ".zip");
- } catch (Exception $ex) {
- return FALSE;
- }
- }
- public static function deleteFolder(string $_dir)
- {
- if(is_dir($_dir)){
- $command = "rm -r " . $_dir;
- try {
- system($command);
- return TRUE;
- } catch (Exception $ex) {
- 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 . " > " . $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);
- } catch (Exception $ex) {
- return FALSE;
- }
- }
- public static function filesRestore()
- {
- json::create("salaries");
- json::create("excel");
- json::create("excel-proweb");
- json::create("events");
- json::create("users");
- json::create("salaries-proweb");
- }
- public static function zip(string $_dir, string $_name)
- {
- $zip = new ZipArchive();
- $zip_name = $_name . ".zip";
- $zip->open($zip_name, ZipArchive::CREATE);
- $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_dir), RecursiveIteratorIterator::LEAVES_ONLY);
- foreach ($files as $file) {
- if (!$file->isDir()) {
- $filePath = $file->getRealPath();
- $relativePath = substr($filePath, strlen($_dir));
- $zip->addFile($filePath, $relativePath);
- }
- }
- $zip->close();
- return $zip_name;
- }
- }
|