Changeset 2577

Show
Ignore:
Timestamp:
02/06/10 08:51:44 (6 weeks ago)
Author:
tomdooley
Message:

Small changes, code cleanup

Location:
branches/backup-restore
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • branches/backup-restore/backup.php

    r2562 r2577  
    2626        echo "OPTIONS:\n"; 
    2727        echo " -v ........... verbose mode\n"; 
     28        echo "\n"; 
     29        echo "Please ensure, there is enough free disk space available for\n"; 
     30        echo "this operation (approx. triple size of htdocs and databases)!\n"; 
    2831        exit(1); 
    2932} 
     
    3841$domain_name = $argv[$argc-2]; 
    3942$password = $argv[$argc-1]; 
    40 if (!file_exists(ISPCP_VIRTUAL_PATH.'/'.$domain_name)) { 
    41         echo "Domain not found in ".ISPCP_VIRTUAL_PATH.'/'.$domain_name."\n"; 
    42         exit(2); 
    43 } 
    44  
    45 // make clean temp path for every call 
    46 if (file_exists(BACKUP_TEMP_PATH)) { 
    47         delTree(BACKUP_TEMP_PATH); 
    48 } 
    49 mkdir(BACKUP_TEMP_PATH, 0700, true); 
    50  
    51 // create archive path if not exist 
    52 if (!file_exists(ARCHIVE_PATH)) { 
    53         mkdir(ARCHIVE_PATH, 0700, true); 
    54 } 
    5543 
    5644$exitcode = 0; 
     
    6856} 
    6957 
    70 // clean up temp path on exit 
    71 if (file_exists(BACKUP_TEMP_PATH)) { 
    72         delTree(BACKUP_TEMP_PATH); 
    73 } 
    74  
    7558exit($exitcode); 
  • branches/backup-restore/includes/BackupPackage.php

    r2576 r2577  
    4444        public function __construct($domain_name, $password) 
    4545        { 
     46                // make clean temp path for every call 
     47                if (file_exists(BACKUP_TEMP_PATH)) { 
     48                        delTree(BACKUP_TEMP_PATH); 
     49                } 
     50                mkdir(BACKUP_TEMP_PATH, 0700, true); 
     51 
     52                // create archive path for domain packages if not exist 
     53                if (!file_exists(ARCHIVE_PATH)) { 
     54                        mkdir(ARCHIVE_PATH, 0700, true); 
     55                } 
     56 
    4657                $this->password = $password; 
    4758                $this->domain_name = $domain_name; 
     59 
     60                // tar and mysqldump can take a lot of time 
     61                set_time_limit(0); 
     62        } 
     63 
     64        /** 
     65         * Destructor, clean up temp path on exit 
     66         */ 
     67        public function __destruct() 
     68        { 
     69                if (file_exists(BACKUP_TEMP_PATH)) { 
     70                        delTree(BACKUP_TEMP_PATH); 
     71                } 
    4872        } 
    4973 
     
    111135         * Create .tar.gz and protect by gpg symmetric encryption 
    112136         */ 
    113         private function createDomainArchive() 
     137        private function createDomainPackage() 
    114138        { 
    115139                // create .tar.gz 
     
    128152                $this->shellExecute($cmd, $a); 
    129153 
    130                 // delete .tar.gz 
     154                // delete remaining .tar.gz 
    131155                unlink($filename); 
    132156 
     
    152176                        $this->setConfigData('dbuser',   $this->getDBUserConfig()); 
    153177 
    154                         // first create configuration file 
    155                         // if successful, create databases and complete domain archive 
     178                        // First create configuration file. If successful, create database 
     179                        // dumps and create the complete domain package file 
    156180                        $result = $this->writeDomainConfig(); 
    157181                        if ($result) { 
    158182                                $result = $this->dumpDomainDatabases(); 
    159183                                if ($result) { 
    160                                         $result = $this->createDomainArchive(); 
     184                                        $result = $this->createDomainPackage(); 
    161185                                } 
    162186                        } 
  • branches/backup-restore/includes/BackupPackage_ispCP.php

    r2570 r2577  
    3939         * ispCP database IDs (name => id) 
    4040         */ 
    41         private $db_ids = array(); 
     41        protected $db_ids = array(); 
    4242 
    4343        public function __construct($domain_name, $password) 
     
    4747        } 
    4848 
     49        /** 
     50         * Get domain database id, validate if vhost path exists 
     51         * @return bool true = init ok, false = see error message 
     52         */ 
    4953        protected function initDomain() 
    5054        { 
    5155                $result = false; 
    5256 
    53                 $test = $this->getDomainID($this->domain_name); 
    54                 if ($test != -1) { 
    55                         $result = true; 
     57                if (!file_exists(ISPCP_VIRTUAL_PATH.'/'.$this->domain_name)) { 
     58                        $this->addErrorMessage('Domain not found in '.ISPCP_VIRTUAL_PATH.'/'.$this->domain_name); 
    5659                } else { 
    57                         $this->addErrorMessage('Domain not in database: '.$this->domain_name); 
     60                        $test = $this->getDomainID($this->domain_name); 
     61                        if ($test != -1) { 
     62                                $result = true; 
     63                        } else { 
     64                                $this->addErrorMessage('Domain not in database: '.$this->domain_name); 
     65                        } 
    5866                } 
    5967 
     
    6674         * @return integer domain id, -1 if not present 
    6775         */ 
    68         private function getDomainID($domain_name) 
     76        protected function getDomainID($domain_name) 
    6977        { 
    7078                $this->domain_id = -1; 
     
    148156                $rs = $this->db->Execute($query, array(':id'=>$this->domain_id)); 
    149157                while (!$rs->EOF) { 
    150                         // TODO: sub_id? 
    151158                        $row = $rs->FetchRow(); 
    152159                        $row['mail_pass'] = decrypt_db_password($row['mail_pass']); 
     
    172179                $rs = $this->db->Execute($query, array(':uid'=>$this->domain_user_id)); 
    173180                while ($rs && !$rs->EOF) { 
    174                         // TODO: passwd? Where is the magic? 
    175181                        $row = $rs->FetchRow(); 
    176182                        $result[] = $row; 
     
    205211        } 
    206212 
    207         private function getSubdomainAliasConfig($alias_id) 
     213        protected function getSubdomainAliasConfig($alias_id) 
    208214        { 
    209215                $result = array(); 
  • branches/backup-restore/includes/Base.php

    r2562 r2577  
    2525        public $verbose = false; 
    2626        /** 
    27          * array of error messages 
     27         * array of strings for error messages 
    2828         */ 
    2929        protected $errorMessages = array(); 
     
    5555        } 
    5656} 
     57 
  • branches/backup-restore/includes/RestorePackage_ispCP.php

    r2570 r2577  
    1919 */ 
    2020 
     21/** 
     22 * ispCP restore packager main class 
     23 */ 
    2124class RestorePackage_ispCP extends BaseController 
    2225{ 
     
    4245        protected $ip_id = -1; 
    4346        /** 
     47         * Reseller name or false for first reseller 
     48         */ 
     49        protected $reseller; 
     50        /** 
     51         * ID of reseller 
     52         */ 
     53        protected $reseller_id = -1; 
     54        /** 
    4455         * linux user id 
    4556         */ 
    4657        protected $domain_user_id = 0; 
    47  
    48         public function __construct($domain_name, $password, $specific_ip) 
     58        /** 
     59         * file name of gpg encrypted domain package 
     60         */ 
     61        protected $archive = ''; 
     62 
     63        /** 
     64         * Restore packager 
     65         * @param string $domain_name 
     66         * @param string $password password for gpg symmetric decryption 
     67         * @param mixed $option_ip false or string of target IP address 
     68         * @param mixed $option_reseller false or string of reseller name 
     69         */ 
     70        public function __construct($domain_name, $password, $option_ip, $option_reseller) 
    4971        { 
    5072                $this->password = $password; 
    5173                $this->domain_name = $domain_name; 
    5274                $this->db = Database::getInstance(); 
    53                 $this->ip = $specific_ip; 
    54         } 
    55  
    56         private function setDomainPermissions() 
     75                $this->ip = $option_ip; 
     76                $this->reseller = $option_reseller; 
     77                $this->archive = ARCHIVE_PATH.'/'.$this->domain_name.'.tar.gz.gpg'; 
     78 
     79                // untar and mysql can take a lot of time 
     80                set_time_limit(0); 
     81        } 
     82 
     83        /** 
     84         * Set permissions to vhost directory 
     85         */ 
     86        protected function setDomainPermissions() 
    5787        { 
    5888                // TODO: setDomainPermissions (chown) 
     
    6393         * @return integer ID of IP 
    6494         */ 
    65         private function getDefaultIP() 
     95        protected function getDefaultIPID() 
    6696        { 
    6797                $result = -1; 
     
    80110 
    81111        /** 
    82          * Get ID of specific IP 
     112         * Get ID of first reseller 
     113         * @return integer ID of reseller 
     114         */ 
     115        protected function getDefaultResellerID() 
     116        { 
     117                $result = -1; 
     118 
     119                $sql = "SELECT `admin_id` FROM `admin` WHERE `admin_type` = :admin_type" . 
     120                           " ORDER BY `admin_id` LIMIT 0, 1"; 
     121                $query = $this->db->Prepare($sql); 
     122                $rs = $this->db->Execute($query, array(':admin_type'=>'reseller')); 
     123                if ($rs && !$rs->EOF) { 
     124                        $result = $rs->fields['admin_id']; 
     125                } else { 
     126                        $this->addErrorMessage('No resellers found!?'); 
     127                } 
     128 
     129                return $result; 
     130        } 
     131 
     132        /** 
     133         * Get ID of reseller name 
     134         * @param string $reseller name of reseller (e.g. 'res1') 
     135         * @return integer ispCP database ID of reseller 
     136         */ 
     137        protected function getResellerID($reseller) 
     138        { 
     139                $result = -1; 
     140 
     141                $sql = "SELECT `admin_id` FROM `admin` WHERE `admin_type` = :admin_type" . 
     142                           " AND `admin_name` = :name" . 
     143                           " ORDER BY `admin_id` LIMIT 0, 1"; 
     144                $query = $this->db->Prepare($sql); 
     145                $rs = $this->db->Execute($query, array(':admin_type'=>'reseller', ':name'=>$reseller)); 
     146                if ($rs && !$rs->EOF) { 
     147                        $result = $rs->fields['admin_id']; 
     148                } else { 
     149                        $this->addErrorMessage('Reseller not found: '.$reseller); 
     150                } 
     151 
     152                return $result; 
     153        } 
     154 
     155        /** 
     156         * Get ID of IP 
    83157         * @param string $ip IP address 
    84          * @return integer ID of IP 
    85          */ 
    86         private function getSpecificIP($ip) 
     158         * @return integer ispCP database ID of IP 
     159         */ 
     160        protected function getIPID($ip) 
    87161        { 
    88162                $result = -1; 
     
    94168                        $result = $rs->fields['ip_id']; 
    95169                } else { 
    96                         $this->addErrorMessage('IP not found!?'); 
    97                 } 
    98  
    99                 return $result; 
    100         } 
    101  
     170                        $this->addErrorMessage('IP not found: '.$ip); 
     171                } 
     172 
     173                return $result; 
     174        } 
     175 
     176        /** 
     177         * Validate presence of gpg encrypted archive, get ID of IP and reseller 
     178         */ 
     179        protected function initRestore() 
     180        { 
     181                $result = false; 
     182 
     183                if (!file_exists($this->archive)) { 
     184                        $this->addErrorMessage('Domain backup package file not found: '.$this->archive); 
     185                } else { 
     186                        // IP detection 
     187                        if ($this->ip === false) { 
     188                                $this->ip_id = $this->getDefaultIPID(); 
     189                        } else { 
     190                                $this->ip_id = $this->getIPID($this->ip); 
     191                        } 
     192 
     193                        // Reseller detection 
     194                        if ($this->reseller === false) { 
     195                                $this->reseller_id = $this->getDefaultResellerID(); 
     196                        } else { 
     197                                $this->reseller_id = $this->getResellerID($this->reseller); 
     198                        } 
     199 
     200                        if ($this->ip_id != -1 && $this->reseller_id != -1) { 
     201                                $result = true; 
     202                        } 
     203                } 
     204 
     205                return $result; 
     206        } 
     207 
     208        /** 
     209         * Run the restore, main method 
     210         * @return boolean true = restore successful, false = see error messages 
     211         */ 
    102212        public function runRestore() 
    103213        { 
    104214                $result = false; 
    105215 
    106                 // IP detection 
    107                 if ($this->ip === false) { 
    108                         $this->ip_id = $this->getDefaultIP(); 
    109                 } else { 
    110                         $this->ip_id = $this->getSpecificIP($this->ip); 
    111                 } 
    112  
    113                 if ($this->ip_id != -1) { 
    114                         // TODO: got IP ID, go on... 
     216                if ($this->initRestore()) { 
     217                        // TODO: runRestore() 
     218                        // don't forget sub_id in mail accounts! 
    115219                } 
    116220 
  • branches/backup-restore/restore.php

    r2570 r2577  
    2525        echo "Usage: php restore.php [OPTIONS] domain archive-password\n"; 
    2626        echo "OPTIONS:\n"; 
    27         echo " -i IP ........ use specific IP for domain\n"; 
     27        echo " -i IP ........ use IP for domain (default = first found)\n"; 
     28        echo " -r RES ....... use reseller for domain (default = first found)\n"; 
    2829        echo " -v ........... verbose mode\n"; 
     30        echo "\n"; 
     31        echo "Please ensure, there is enough free disk space available for\n"; 
     32        echo "this operation (approx. triple size of htdocs and databases)!\n"; 
    2933        exit(1); 
    3034} 
    3135 
    32 $verbose = $specific_ip = false; 
     36$verbose = $option_ip = $option_res = false; 
    3337for ($i = 1; $i < $argc-2; $i++) { 
    3438        if ($argv[$i] == '-v') { 
    3539                $verbose = true; 
    3640        } elseif ($argv[$i] == '-i') { 
    37                 $specific_ip = $argv[++$i]; 
     41                $option_ip = $argv[++$i]; 
     42        } elseif ($argv[$i] == '-r') { 
     43                $option_res = $argv[++$i]; 
    3844        } 
    3945} 
     
    4147$domain_name = $argv[$argc-2]; 
    4248$password = $argv[$argc-1]; 
    43 if (!file_exists(ARCHIVE_PATH.'/'.$domain_name.'.tar.gz.gpg')) { 
    44         echo "Domain backup file not found in ".ARCHIVE_PATH.'/'.$domain_name.'.tar.gz.gpg'."\n"; 
    45         exit(2); 
    46 } 
    4749 
    4850$exitcode = 0; 
    49 $handler = new RestorePackage_ispCP($domain_name, $password, $specific_ip); 
     51$handler = new RestorePackage_ispCP($domain_name, $password, $option_ip); 
    5052$handler->verbose = $verbose; 
    5153if ($handler->runRestore() == false) {