From 2332b2279d8a8599b4f041370315edc9544b1560 Mon Sep 17 00:00:00 2001
From: tbrehm <t.brehm@ispconfig.org>
Date: Fri, 02 Aug 2013 10:48:38 -0400
Subject: [PATCH] Added support for mongodb. Many thanks to MaddinXx for the patch. http://www.howtoforge.com/forums/showthread.php?t=62691

---
 server/plugins-available/backup_plugin.inc.php |   66 +++++++++++++++++++++++----------
 1 files changed, 46 insertions(+), 20 deletions(-)

diff --git a/server/plugins-available/backup_plugin.inc.php b/server/plugins-available/backup_plugin.inc.php
index 78be959..bc91a91 100644
--- a/server/plugins-available/backup_plugin.inc.php
+++ b/server/plugins-available/backup_plugin.inc.php
@@ -29,48 +29,48 @@
 */
 
 class backup_plugin {
-	
+
 	var $plugin_name = 'backup_plugin';
 	var $class_name  = 'backup_plugin';
-	
+
 	//* This function is called during ispconfig installation to determine
 	//  if a symlink shall be created for this plugin.
 	public function onInstall() {
 		global $conf;
-		
+
 		return true;
-		
+
 	}
-	
-		
+
+
 	/*
 	 	This function is called when the plugin is loaded
 	*/
-	
+
 	public function onLoad() {
 		global $app;
-		
+
 		//* Register for actions
 		$app->plugins->registerAction('backup_download',$this->plugin_name,'backup_action');
 		$app->plugins->registerAction('backup_restore',$this->plugin_name,'backup_action');
-		
+
 	}
-	
+
 	//* Do a backup action
 	public function backup_action($action_name,$data) {
 		global $app,$conf;
-		
+
 		$backup_id = intval($data);
 		$backup = $app->dbmaster->queryOneRecord("SELECT * FROM web_backup WHERE backup_id = $backup_id");
-		
+
 		if(is_array($backup)) {
-		
+
 			$app->uses('ini_parser,file,getconf');
-			
+
 			$web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".$backup['parent_domain_id']);
 			$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
 			$backup_dir = $server_config['backup_dir'].'/web'.$web['domain_id'];
-			
+
 			//* Make backup available for download
 			if($action_name == 'backup_download') {
 				//* Copy the backup file to the backup folder of the website
@@ -80,12 +80,38 @@
 					$app->log('cp '.$backup_dir.'/'.$backup['filename'].' '.$web['document_root'].'/backup/'.$backup['filename'],LOGLEVEL_DEBUG);
 				}
 			}
-			
+
+			//* Restore a MongoDB backup
+			if($action_name == 'backup_restore' && $backup['backup_type'] == 'mongodb') {
+				if(file_exists($backup_dir.'/'.$backup['filename'])) {
+					//$parts = explode('_',$backup['filename']);
+					//$db_name = $parts[1];
+					preg_match('@^db_(.+)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.tar\.gz$@', $backup['filename'], $matches);
+					$db_name = $matches[1];
+
+					// extract tar.gz archive
+					$dump_directory = str_replace(".tar.gz", "", $backup['filename']);
+					$extracted = "/usr/local/ispconfig/server/temp";
+					exec("tar -xzvf ".escapeshellarg($backup_dir.'/'.$backup['filename'])." --directory=".escapeshellarg($extracted));
+					$restore_directory = $extracted."/".$dump_directory."/".$db_name;
+
+					// mongorestore -h 127.0.0.1 -u root -p 123456 --authenticationDatabase admin -d c1debug --drop ./toRestore
+					$command = "mongorestore -h 127.0.0.1 --port 27017 -u root -p 123456 --authenticationDatabase admin -d ".$db_name." --drop ".escapeshellarg($restore_directory);
+					exec($command);
+					exec("rm -rf ".escapeshellarg($extracted."/".$dump_directory));
+				}
+
+				unset($clientdb_host);
+				unset($clientdb_user);
+				unset($clientdb_password);
+				$app->log('Restored MongoDB backup '.$backup_dir.'/'.$backup['filename'],LOGLEVEL_DEBUG);
+			}
+
 			//* Restore a mysql backup
 			if($action_name == 'backup_restore' && $backup['backup_type'] == 'mysql') {
 				//* Load sql dump into db
 				include('lib/mysql_clientdb.conf');
-				
+
 				if(file_exists($backup_dir.'/'.$backup['filename'])) {
 					//$parts = explode('_',$backup['filename']);
 					//$db_name = $parts[1];
@@ -99,7 +125,7 @@
 				unset($clientdb_password);
 				$app->log('Restored MySQL backup '.$backup_dir.'/'.$backup['filename'],LOGLEVEL_DEBUG);
 			}
-			
+
 			//* Restore a web backup
 			if($action_name == 'backup_restore' && $backup['backup_type'] == 'web') {
 				if($backup['backup_mode'] == 'userzip') {
@@ -123,11 +149,11 @@
 					}
 				}
 			}
-			
+
 		} else {
 			$app->log('No backup with ID '.$backup_id.' found.',LOGLEVEL_DEBUG);
 		}
-		
+
 		return 'ok';
 	}
 

--
Gitblit v1.9.1