commit | author | age
|
5a43e7
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
Copyright (c) 2012, Till Brehm, ISPConfig UG |
|
5 |
All rights reserved. |
|
6 |
|
|
7 |
Redistribution and use in source and binary forms, with or without modification, |
|
8 |
are permitted provided that the following conditions are met: |
|
9 |
|
|
10 |
* Redistributions of source code must retain the above copyright notice, |
|
11 |
this list of conditions and the following disclaimer. |
|
12 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
13 |
this list of conditions and the following disclaimer in the documentation |
|
14 |
and/or other materials provided with the distribution. |
|
15 |
* Neither the name of ISPConfig nor the names of its contributors |
|
16 |
may be used to endorse or promote products derived from this software without |
|
17 |
specific prior written permission. |
|
18 |
|
|
19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
22 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
26 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
*/ |
|
30 |
|
|
31 |
class backup_plugin { |
2332b2
|
32 |
|
5a43e7
|
33 |
var $plugin_name = 'backup_plugin'; |
T |
34 |
var $class_name = 'backup_plugin'; |
2332b2
|
35 |
|
5a43e7
|
36 |
//* This function is called during ispconfig installation to determine |
T |
37 |
// if a symlink shall be created for this plugin. |
|
38 |
public function onInstall() { |
|
39 |
global $conf; |
2332b2
|
40 |
|
5a43e7
|
41 |
return true; |
2332b2
|
42 |
|
5a43e7
|
43 |
} |
2332b2
|
44 |
|
T |
45 |
|
5a43e7
|
46 |
/* |
T |
47 |
This function is called when the plugin is loaded |
|
48 |
*/ |
2332b2
|
49 |
|
5a43e7
|
50 |
public function onLoad() { |
T |
51 |
global $app; |
2332b2
|
52 |
|
5a43e7
|
53 |
//* Register for actions |
T |
54 |
$app->plugins->registerAction('backup_download',$this->plugin_name,'backup_action'); |
|
55 |
$app->plugins->registerAction('backup_restore',$this->plugin_name,'backup_action'); |
2332b2
|
56 |
|
5a43e7
|
57 |
} |
2332b2
|
58 |
|
5a43e7
|
59 |
//* Do a backup action |
T |
60 |
public function backup_action($action_name,$data) { |
|
61 |
global $app,$conf; |
2332b2
|
62 |
|
5a43e7
|
63 |
$backup_id = intval($data); |
4bd960
|
64 |
$backup = $app->dbmaster->queryOneRecord("SELECT * FROM web_backup WHERE backup_id = $backup_id"); |
2332b2
|
65 |
|
5a43e7
|
66 |
if(is_array($backup)) { |
2332b2
|
67 |
|
5a43e7
|
68 |
$app->uses('ini_parser,file,getconf'); |
2332b2
|
69 |
|
5a43e7
|
70 |
$web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".$backup['parent_domain_id']); |
T |
71 |
$server_config = $app->getconf->get_server_config($conf['server_id'], 'server'); |
|
72 |
$backup_dir = $server_config['backup_dir'].'/web'.$web['domain_id']; |
2332b2
|
73 |
|
5a43e7
|
74 |
//* Make backup available for download |
T |
75 |
if($action_name == 'backup_download') { |
|
76 |
//* Copy the backup file to the backup folder of the website |
|
77 |
if(file_exists($backup_dir.'/'.$backup['filename']) && !stristr($backup_dir.'/'.$backup['filename'],'..') && !stristr($backup_dir.'/'.$backup['filename'],'etc')) { |
|
78 |
copy($backup_dir.'/'.$backup['filename'],$web['document_root'].'/backup/'.$backup['filename']); |
|
79 |
chgrp($web['document_root'].'/backup/'.$backup['filename'],$web['system_group']); |
|
80 |
$app->log('cp '.$backup_dir.'/'.$backup['filename'].' '.$web['document_root'].'/backup/'.$backup['filename'],LOGLEVEL_DEBUG); |
|
81 |
} |
|
82 |
} |
2332b2
|
83 |
|
T |
84 |
//* Restore a MongoDB backup |
|
85 |
if($action_name == 'backup_restore' && $backup['backup_type'] == 'mongodb') { |
|
86 |
if(file_exists($backup_dir.'/'.$backup['filename'])) { |
|
87 |
//$parts = explode('_',$backup['filename']); |
|
88 |
//$db_name = $parts[1]; |
|
89 |
preg_match('@^db_(.+)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.tar\.gz$@', $backup['filename'], $matches); |
|
90 |
$db_name = $matches[1]; |
|
91 |
|
|
92 |
// extract tar.gz archive |
|
93 |
$dump_directory = str_replace(".tar.gz", "", $backup['filename']); |
|
94 |
$extracted = "/usr/local/ispconfig/server/temp"; |
|
95 |
exec("tar -xzvf ".escapeshellarg($backup_dir.'/'.$backup['filename'])." --directory=".escapeshellarg($extracted)); |
|
96 |
$restore_directory = $extracted."/".$dump_directory."/".$db_name; |
|
97 |
|
|
98 |
// mongorestore -h 127.0.0.1 -u root -p 123456 --authenticationDatabase admin -d c1debug --drop ./toRestore |
|
99 |
$command = "mongorestore -h 127.0.0.1 --port 27017 -u root -p 123456 --authenticationDatabase admin -d ".$db_name." --drop ".escapeshellarg($restore_directory); |
|
100 |
exec($command); |
|
101 |
exec("rm -rf ".escapeshellarg($extracted."/".$dump_directory)); |
|
102 |
} |
|
103 |
|
|
104 |
unset($clientdb_host); |
|
105 |
unset($clientdb_user); |
|
106 |
unset($clientdb_password); |
|
107 |
$app->log('Restored MongoDB backup '.$backup_dir.'/'.$backup['filename'],LOGLEVEL_DEBUG); |
|
108 |
} |
|
109 |
|
5a43e7
|
110 |
//* Restore a mysql backup |
T |
111 |
if($action_name == 'backup_restore' && $backup['backup_type'] == 'mysql') { |
|
112 |
//* Load sql dump into db |
|
113 |
include('lib/mysql_clientdb.conf'); |
2332b2
|
114 |
|
5a43e7
|
115 |
if(file_exists($backup_dir.'/'.$backup['filename'])) { |
cc6568
|
116 |
//$parts = explode('_',$backup['filename']); |
H |
117 |
//$db_name = $parts[1]; |
|
118 |
preg_match('@^db_(.+)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.sql\.gz$@', $backup['filename'], $matches); |
|
119 |
$db_name = $matches[1]; |
5a43e7
|
120 |
$command = "gunzip --stdout ".escapeshellarg($backup_dir.'/'.$backup['filename'])." | mysql -h '".escapeshellcmd($clientdb_host)."' -u '".escapeshellcmd($clientdb_user)."' -p'".escapeshellcmd($clientdb_password)."' '".$db_name."'"; |
T |
121 |
exec($command); |
|
122 |
} |
|
123 |
unset($clientdb_host); |
|
124 |
unset($clientdb_user); |
|
125 |
unset($clientdb_password); |
|
126 |
$app->log('Restored MySQL backup '.$backup_dir.'/'.$backup['filename'],LOGLEVEL_DEBUG); |
|
127 |
} |
2332b2
|
128 |
|
5a43e7
|
129 |
//* Restore a web backup |
T |
130 |
if($action_name == 'backup_restore' && $backup['backup_type'] == 'web') { |
|
131 |
if($backup['backup_mode'] == 'userzip') { |
|
132 |
if(file_exists($backup_dir.'/'.$backup['filename']) && $web['document_root'] != '' && $web['document_root'] != '/' && !stristr($backup_dir.'/'.$backup['filename'],'..') && !stristr($backup_dir.'/'.$backup['filename'],'etc')) { |
|
133 |
if(file_exists($web['document_root'].'/backup/'.$backup['filename'])) rename($web['document_root'].'/backup/'.$backup['filename'],$web['document_root'].'/backup/'.$backup['filename'].'.bak'); |
|
134 |
copy($backup_dir.'/'.$backup['filename'],$web['document_root'].'/backup/'.$backup['filename']); |
|
135 |
chgrp($web['document_root'].'/backup/'.$backup['filename'],$web['system_group']); |
|
136 |
//chown($web['document_root'].'/backup/'.$backup['filename'],$web['system_user']); |
|
137 |
$command = 'sudo -u '.escapeshellarg($web['system_user']).' unzip -qq -o '.escapeshellarg($web['document_root'].'/backup/'.$backup['filename']).' -d '.escapeshellarg($web['document_root']).' 2> /dev/null'; |
|
138 |
exec($command); |
|
139 |
unlink($web['document_root'].'/backup/'.$backup['filename']); |
|
140 |
if(file_exists($web['document_root'].'/backup/'.$backup['filename'].'.bak')) rename($web['document_root'].'/backup/'.$backup['filename'].'.bak',$web['document_root'].'/backup/'.$backup['filename']); |
|
141 |
$app->log('Restored Web backup '.$backup_dir.'/'.$backup['filename'],LOGLEVEL_DEBUG); |
|
142 |
} |
|
143 |
} |
|
144 |
if($backup['backup_mode'] == 'rootgz') { |
|
145 |
if(file_exists($backup_dir.'/'.$backup['filename']) && $web['document_root'] != '' && $web['document_root'] != '/' && !stristr($backup_dir.'/'.$backup['filename'],'..') && !stristr($backup_dir.'/'.$backup['filename'],'etc')) { |
|
146 |
$command = 'tar xzf '.escapeshellarg($backup_dir.'/'.$backup['filename']).' --directory '.escapeshellarg($web['document_root']); |
|
147 |
exec($command); |
|
148 |
$app->log('Restored Web backup '.$backup_dir.'/'.$backup['filename'],LOGLEVEL_DEBUG); |
|
149 |
} |
|
150 |
} |
|
151 |
} |
2332b2
|
152 |
|
5a43e7
|
153 |
} else { |
T |
154 |
$app->log('No backup with ID '.$backup_id.' found.',LOGLEVEL_DEBUG); |
|
155 |
} |
2332b2
|
156 |
|
5a43e7
|
157 |
return 'ok'; |
T |
158 |
} |
|
159 |
|
|
160 |
} // end class |
|
161 |
|
|
162 |
?> |