Till Brehm
2014-09-10 80262299f00d3c5a3bc0e5ef73f3d6d24792d2c1
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
802622 75             /*
056465 76             $backup_dir_is_ready = true;
FT 77             $server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
78             if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
838593 79                 if(!$app->system->is_mounted($server_config['backup_dir'])){
056465 80                     exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
FT 81                     sleep(1);
838593 82                     if(!$app->system->is_mounted($server_config['backup_dir'])) $backup_dir_is_ready = false;
5a43e7 83                 }
802622 84             }*/
TB 85             $backup_dir_is_ready = true;
86             $backup_dir_mount_cmd = '/usr/local/ispconfig/server/scripts/backup_dir_mount.sh';
87             if(    $server_config['backup_dir_is_mount'] == 'y' && 
88                 is_file($backup_dir_mount_cmd) && 
89                 is_executable($backup_dir_mount_cmd) &&
90                 fileowner($backup_dir_mount_cmd) === 0
91             ){
92                 if(!$app->system->is_mounted($backup_dir)){
93                     exec($backup_dir_mount_cmd);
94                     sleep(1);
95                     if(!$app->system->is_mounted($server_config['backup_dir'])) $backup_dir_is_ready = false;
96                 }
5a43e7 97             }
7fe908 98
056465 99             if($backup_dir_is_ready){
FT 100                 //* Make backup available for download
101                 if($action_name == 'backup_download') {
102                     //* Copy the backup file to the backup folder of the website
103                     if(file_exists($backup_dir.'/'.$backup['filename']) && !stristr($backup_dir.'/'.$backup['filename'], '..') && !stristr($backup_dir.'/'.$backup['filename'], 'etc')) {
7fe908 104                         copy($backup_dir.'/'.$backup['filename'], $web['document_root'].'/backup/'.$backup['filename']);
MC 105                         chgrp($web['document_root'].'/backup/'.$backup['filename'], $web['system_group']);
056465 106                         $app->log('cp '.$backup_dir.'/'.$backup['filename'].' '.$web['document_root'].'/backup/'.$backup['filename'], LOGLEVEL_DEBUG);
5a43e7 107                     }
T 108                 }
056465 109
FT 110                 //* Restore a mysql backup
111                 if($action_name == 'backup_restore' && $backup['backup_type'] == 'mysql') {
112                     //* Load sql dump into db
113                     include 'lib/mysql_clientdb.conf';
114
115                     if(file_exists($backup_dir.'/'.$backup['filename'])) {
116                         //$parts = explode('_',$backup['filename']);
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];
120                         $command = "gunzip --stdout ".escapeshellarg($backup_dir.'/'.$backup['filename'])." | mysql -h '".escapeshellcmd($clientdb_host)."' -u '".escapeshellcmd($clientdb_user)."' -p'".escapeshellcmd($clientdb_password)."' '".$db_name."'";
5a43e7 121                         exec($command);
056465 122                     }
FT 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                 }
128
129                 //* Restore a web backup
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                         }
5a43e7 150                     }
T 151                 }
056465 152             } else {
FT 153                 $app->log('Backup directory not ready.', LOGLEVEL_DEBUG);
5a43e7 154             }
7fe908 155
5a43e7 156         } else {
7fe908 157             $app->log('No backup with ID '.$backup_id.' found.', LOGLEVEL_DEBUG);
5a43e7 158         }
7fe908 159
5a43e7 160         return 'ok';
T 161     }
162
163 } // end class
164
165 ?>