Marius Cramer
2014-08-13 42539643c396f9d8865dcf9a51b13dc869709d16
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 {
7fe908 32
5a43e7 33     var $plugin_name = 'backup_plugin';
T 34     var $class_name  = 'backup_plugin';
7fe908 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;
7fe908 40
5a43e7 41         return true;
7fe908 42
5a43e7 43     }
7fe908 44
MC 45
5a43e7 46     /*
T 47          This function is called when the plugin is loaded
48     */
7fe908 49
5a43e7 50     public function onLoad() {
T 51         global $app;
7fe908 52
5a43e7 53         //* Register for actions
7fe908 54         $app->plugins->registerAction('backup_download', $this->plugin_name, 'backup_action');
MC 55         $app->plugins->registerAction('backup_restore', $this->plugin_name, 'backup_action');
56
5a43e7 57     }
7fe908 58
5a43e7 59     //* Do a backup action
7fe908 60     public function backup_action($action_name, $data) {
MC 61         global $app, $conf;
62
5a43e7 63         $backup_id = intval($data);
4bd960 64         $backup = $app->dbmaster->queryOneRecord("SELECT * FROM web_backup WHERE backup_id = $backup_id");
7fe908 65
5a43e7 66         if(is_array($backup)) {
7fe908 67
056465 68             $app->uses('ini_parser,file,getconf,system');
7fe908 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'];
056465 73             
FT 74             //* mount backup directory, if necessary
75             $backup_dir_is_ready = true;
76             $server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
77             if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
838593 78                 if(!$app->system->is_mounted($server_config['backup_dir'])){
056465 79                     exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
FT 80                     sleep(1);
838593 81                     if(!$app->system->is_mounted($server_config['backup_dir'])) $backup_dir_is_ready = false;
5a43e7 82                 }
T 83             }
7fe908 84
056465 85             if($backup_dir_is_ready){
FT 86                 //* Make backup available for download
87                 if($action_name == 'backup_download') {
88                     //* Copy the backup file to the backup folder of the website
89                     if(file_exists($backup_dir.'/'.$backup['filename']) && !stristr($backup_dir.'/'.$backup['filename'], '..') && !stristr($backup_dir.'/'.$backup['filename'], 'etc')) {
7fe908 90                         copy($backup_dir.'/'.$backup['filename'], $web['document_root'].'/backup/'.$backup['filename']);
MC 91                         chgrp($web['document_root'].'/backup/'.$backup['filename'], $web['system_group']);
056465 92                         $app->log('cp '.$backup_dir.'/'.$backup['filename'].' '.$web['document_root'].'/backup/'.$backup['filename'], LOGLEVEL_DEBUG);
5a43e7 93                     }
T 94                 }
056465 95
FT 96                 //* Restore a mysql backup
97                 if($action_name == 'backup_restore' && $backup['backup_type'] == 'mysql') {
98                     //* Load sql dump into db
99                     include 'lib/mysql_clientdb.conf';
100
101                     if(file_exists($backup_dir.'/'.$backup['filename'])) {
102                         //$parts = explode('_',$backup['filename']);
103                         //$db_name = $parts[1];
104                         preg_match('@^db_(.+)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.sql\.gz$@', $backup['filename'], $matches);
105                         $db_name = $matches[1];
106                         $command = "gunzip --stdout ".escapeshellarg($backup_dir.'/'.$backup['filename'])." | mysql -h '".escapeshellcmd($clientdb_host)."' -u '".escapeshellcmd($clientdb_user)."' -p'".escapeshellcmd($clientdb_password)."' '".$db_name."'";
5a43e7 107                         exec($command);
056465 108                     }
FT 109                     unset($clientdb_host);
110                     unset($clientdb_user);
111                     unset($clientdb_password);
112                     $app->log('Restored MySQL backup '.$backup_dir.'/'.$backup['filename'], LOGLEVEL_DEBUG);
113                 }
114
115                 //* Restore a web backup
116                 if($action_name == 'backup_restore' && $backup['backup_type'] == 'web') {
117                     if($backup['backup_mode'] == 'userzip') {
118                         if(file_exists($backup_dir.'/'.$backup['filename']) && $web['document_root'] != '' && $web['document_root'] != '/' && !stristr($backup_dir.'/'.$backup['filename'], '..') && !stristr($backup_dir.'/'.$backup['filename'], 'etc')) {
119                             if(file_exists($web['document_root'].'/backup/'.$backup['filename'])) rename($web['document_root'].'/backup/'.$backup['filename'], $web['document_root'].'/backup/'.$backup['filename'].'.bak');
120                             copy($backup_dir.'/'.$backup['filename'], $web['document_root'].'/backup/'.$backup['filename']);
121                             chgrp($web['document_root'].'/backup/'.$backup['filename'], $web['system_group']);
122                             //chown($web['document_root'].'/backup/'.$backup['filename'],$web['system_user']);
123                             $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';
124                             exec($command);
125                             unlink($web['document_root'].'/backup/'.$backup['filename']);
126                             if(file_exists($web['document_root'].'/backup/'.$backup['filename'].'.bak')) rename($web['document_root'].'/backup/'.$backup['filename'].'.bak', $web['document_root'].'/backup/'.$backup['filename']);
127                             $app->log('Restored Web backup '.$backup_dir.'/'.$backup['filename'], LOGLEVEL_DEBUG);
128                         }
129                     }
130                     if($backup['backup_mode'] == 'rootgz') {
131                         if(file_exists($backup_dir.'/'.$backup['filename']) && $web['document_root'] != '' && $web['document_root'] != '/' && !stristr($backup_dir.'/'.$backup['filename'], '..') && !stristr($backup_dir.'/'.$backup['filename'], 'etc')) {
132                             $command = 'tar xzf '.escapeshellarg($backup_dir.'/'.$backup['filename']).' --directory '.escapeshellarg($web['document_root']);
133                             exec($command);
134                             $app->log('Restored Web backup '.$backup_dir.'/'.$backup['filename'], LOGLEVEL_DEBUG);
135                         }
5a43e7 136                     }
T 137                 }
056465 138             } else {
FT 139                 $app->log('Backup directory not ready.', LOGLEVEL_DEBUG);
5a43e7 140             }
7fe908 141
5a43e7 142         } else {
7fe908 143             $app->log('No backup with ID '.$backup_id.' found.', LOGLEVEL_DEBUG);
5a43e7 144         }
7fe908 145
5a43e7 146         return 'ok';
T 147     }
148
149 } // end class
150
151 ?>