Db Backup Plugin
This commit is contained in:
		| @@ -13,6 +13,13 @@ class CronApi extends ApiController { | |||||||
| 		  echo (new OpenWeatherMap)->fetch(''); | 		  echo (new OpenWeatherMap)->fetch(''); | ||||||
| 		  echo (new UsaElection)->fetch(''); | 		  echo (new UsaElection)->fetch(''); | ||||||
|  |  | ||||||
|  | 			//	Database Backup | ||||||
|  | 		  $filenames = []; | ||||||
|  | 		  $backupWorker = new DatabaseBackup; | ||||||
|  | 		  $filenames[] = $backupWorker->scheme(); | ||||||
|  | 		  $filenames[] = $backupWorker->data(); | ||||||
|  | 		  $backupWorker->compress($_SERVER['DOCUMENT_ROOT'] . BASEDIR . '/backup/'.date("Y-m-d", time()).'.zip', $filenames); | ||||||
|  |  | ||||||
|         $this->response(['Value' => 'OK']); |         $this->response(['Value' => 'OK']); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										34
									
								
								app/plugins/DatabaseBackup.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								app/plugins/DatabaseBackup.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | |||||||
|  | <?php | ||||||
|  | class DatabaseBackup { | ||||||
|  | 	public function scheme(){ | ||||||
|  | 		$backupfile = $_SERVER['DOCUMENT_ROOT'] . BASEDIR ."/backup/" . DBNAME.'_scheme_'.date("Y-m-d", time()).'.sql'; | ||||||
|  | 		$command = "mysqldump --skip-comments --no-create-info -h localhost -u ". DBUSER . " -p" . DBPASS ." ". DBNAME ." -r $backupfile 2>&1"; | ||||||
|  | 		$this->executeCommand($command); | ||||||
|  | 		return $backupfile; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	public function data(){ | ||||||
|  | 		$backupfile = $_SERVER['DOCUMENT_ROOT'] . BASEDIR ."/backup/" . DBNAME.'_data_'.date("Y-m-d", time()).'.sql'; | ||||||
|  | 		$command = "mysqldump --skip-comments --no-data -h localhost -u ". DBUSER . " -p" . DBPASS ." ". DBNAME ." -r $backupfile 2>&1"; | ||||||
|  | 		$this->executeCommand($command); | ||||||
|  | 		return $backupfile; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	private function executeCommand($command){ | ||||||
|  | 		ini_set('date.timezone', 'Europe/Prague'); | ||||||
|  | 		exec($command); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	public function compress($filename, $files = []) { | ||||||
|  | 		$zip = new ZipArchive(); | ||||||
|  | 		if($zip->open($filename,ZipArchive::CREATE|ZipArchive::OVERWRITE)) { | ||||||
|  | 			foreach ($files as $file) { | ||||||
|  | 				$zip->addFile($file); | ||||||
|  | 			} | ||||||
|  | 			echo $zip->close(); | ||||||
|  | 			foreach ($files as $file) { | ||||||
|  | 				unlink($file); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user