Changeset 2583

Show
Ignore:
Timestamp:
02/07/10 14:09:27 (6 weeks ago)
Author:
tomdooley
Message:

Enhancements and optimizations

Location:
branches/backup-restore/includes
Files:
3 modified

Legend:

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

    r2577 r2583  
    140140                $filename = ARCHIVE_PATH.'/'.$this->domain_name.'.tar.gz'; 
    141141                // TODO: only htdocs? 
    142                 $cmd = 'tar czvf '.$filename.' -C '.BACKUP_BASE_PATH.' tmp'. 
    143                                 ' -C '.ISPCP_VIRTUAL_PATH.'/'.$this->domain_name.' htdocs'; 
     142                $cmd = 'tar czf '.$filename.' -C '.BACKUP_BASE_PATH.' tmp'. 
     143                                ' -C '.ISPCP_VIRTUAL_PATH.' '.$this->domain_name. 
     144                                ' --exclude=logs --exclude=phptmp --exclude=backups'; 
     145 
    144146                // TODO: Error handling 
    145147                $a = array(); 
     
    166168                if ($result) { 
    167169                        // collect all data 
    168                         $this->setConfigData('domain',   $this->getDomainConfig()); 
    169                         $this->setConfigData('email',    $this->getEMailConfig()); 
    170                         $this->setConfigData('ftp',      $this->getFTPConfig()); 
    171                         $this->setConfigData('domain',   $this->getDomainAliasConfig()); 
    172                         $this->setConfigData('webuser',  $this->getWebUserConfig()); 
    173                         $this->setConfigData('webgroup', $this->getWebGroupConfig()); 
    174                         $this->setConfigData('dns',      $this->getDNSConfig()); 
    175                         $this->setConfigData('db',               $this->getDBConfig()); 
    176                         $this->setConfigData('dbuser',   $this->getDBUserConfig()); 
     170                        $this->setConfigData('domain',          $this->getDomainConfig()); 
     171                        $this->setConfigData('subdomain',       $this->getSubDomainConfig()); 
     172                        $this->setConfigData('email',           $this->getEMailConfig()); 
     173                        $this->setConfigData('ftp',                     $this->getFTPConfig()); 
     174                        $this->setConfigData('alias',           $this->getDomainAliasConfig()); 
     175                        $this->setConfigData('webuser',         $this->getWebUserConfig()); 
     176                        $this->setConfigData('webgroup',        $this->getWebGroupConfig()); 
     177                        $this->setConfigData('dns',                     $this->getDNSConfig()); 
     178                        $this->setConfigData('db',                      $this->getDBConfig()); 
     179                        $this->setConfigData('dbuser',          $this->getDBUserConfig()); 
    177180 
    178181                        // First create configuration file. If successful, create database 
  • branches/backup-restore/includes/BackupPackage_ispCP.php

    r2577 r2583  
    203203                while (!$rs->EOF) { 
    204204                        $row = $rs->FetchRow(); 
    205                         $row['alias'] = $this->getSubdomainAliasConfig($row['alias_id']); 
     205                        $row['subdomain'] = $this->getSubdomainAliasConfig($row['alias_id']); 
    206206                        $result[] = $row; 
    207207                        $rs->MoveNext(); 
  • branches/backup-restore/includes/RestorePackage_ispCP.php

    r2577 r2583  
    5353        protected $reseller_id = -1; 
    5454        /** 
     55         * New domain ID 
     56         */ 
     57        protected $domain_id = 0; 
     58        /** 
    5559         * linux user id 
    5660         */ 
     
    5963         * file name of gpg encrypted domain package 
    6064         */ 
     65        protected $gpg_archive = ''; 
     66        /** 
     67         * file name of tar.gz 
     68         */ 
    6169        protected $archive = ''; 
     70        /** 
     71         * array of configuration data (multi associative) 
     72         */ 
     73        protected $configurationData = array(); 
     74        /** 
     75         * database IDs (key = database name) 
     76         */ 
     77        protected $database_ids = array(); 
     78        /** 
     79         * new subdomain IDs (key = database name) 
     80         */ 
     81        protected $subdomain_ids = array(); 
    6282 
    6383        /** 
     
    7595                $this->ip = $option_ip; 
    7696                $this->reseller = $option_reseller; 
    77                 $this->archive = ARCHIVE_PATH.'/'.$this->domain_name.'.tar.gz.gpg'; 
     97                $this->gpg_archive = ARCHIVE_PATH.'/'.$this->domain_name.'.tar.gz.gpg'; 
     98                $this->archive = mb_substr($this->gpg_archive, 0, mb_strlen($this->gpg_archive)-4); 
     99                $this->target_path = ISPCP_VIRTUAL_PATH.'/'.$this->domain_name; 
    78100 
    79101                // untar and mysql can take a lot of time 
    80102                set_time_limit(0); 
     103        } 
     104 
     105        /** 
     106         * Destructor, clean up tmp folder and .tar.gz on exit 
     107         */ 
     108        public function __destruct() 
     109        { 
     110                if (file_exists($this->target_path.'/tmp')) { 
     111                        delTree($this->target_path.'/tmp'); 
     112                } 
     113                if (file_exists($this->archive)) { 
     114                        unlink($this->archive); 
     115                } 
    81116        } 
    82117 
     
    181216                $result = false; 
    182217 
    183                 if (!file_exists($this->archive)) { 
    184                         $this->addErrorMessage('Domain backup package file not found: '.$this->archive); 
     218                if (!file_exists($this->gpg_archive)) { 
     219                        $this->addErrorMessage('Domain backup package file not found: '.$this->gpg_archive); 
    185220                } else { 
    186221                        // IP detection 
     
    206241        } 
    207242 
     243        protected function unpackDomainPackage() 
     244        { 
     245                $reuslt = false; 
     246 
     247                // unprotect via gpg -c --passphrase ... file 
     248                $cmd = 'gpg --passphrase '.$this->password.' '.$this->gpg_archive; 
     249                $a = array(); 
     250                $this->shellExecute($cmd, $a); 
     251 
     252                if (file_exists($this->archive)) { 
     253 
     254                        if (!file_exists($this->target_path)) { 
     255                                mkdir($this->target_path, 0770, true); 
     256                        } 
     257 
     258                        $cmd = 'tar xzf -C '.$this->target_path.' '.$this->archive; 
     259                        $this->shellExecute($cmd, $a); 
     260 
     261                        if (file_exists($this->target_path.'/tmp/config.ser')) { 
     262                                $result = true; 
     263                        } else { 
     264                                $this->addErrorMessage('File not found: '.$this->target_path.'/tmp/config.ser'); 
     265                        } 
     266 
     267                        $result = true; 
     268                } else { 
     269                        $this->addErrorMessage('File not found: '.$this->archive.' - incorrect password?'); 
     270                } 
     271 
     272                return $result; 
     273        } 
     274 
     275        protected function getConfigData() 
     276        { 
     277                $result = false; 
     278 
     279                $config_file = $this->target_path.'/tmp/config.ser'; 
     280 
     281                $fp = fopen($config_file, 'w'); 
     282                if ($fp) { 
     283                        $s = fread($fp, filesize($config_file)); 
     284                        $this->configurationData = unserialize($s); 
     285                        fclose($fp); 
     286 
     287                        if (is_array($this->configurationData) && count($this->configurationData) > 0) { 
     288                                $result = true; 
     289                        } else { 
     290                                $this->addErrorMessage('Broken file '.$config_file); 
     291                        } 
     292 
     293                } else { 
     294                        $this->addErrorMessage('Could not open file '.$config_file); 
     295                } 
     296 
     297                return $result; 
     298        } 
     299 
     300        /** 
     301         * Create all databases 
     302         */ 
     303        protected function createDatabases() 
     304        { 
     305                foreach ($this->configurationData['db'] as $db) { 
     306                        $this->importMySQLDatabase($db['sqld_name']); 
     307 
     308                        // Insert database row into ispCP database 
     309                        $query = $this->db->Prepare( 
     310                                "INSERT INTO `sql_database`". 
     311                                " (`domain_id`, `sqld_name`)". 
     312                                " VALUES". 
     313                                " (?, ?)" 
     314                        ); 
     315                        $this->db->Execute($query, array($this->domain_id, $db['sqld_name'])); 
     316                        $this->database_ids[$db['sqld_name']] = $this->db->Insert_ID(); 
     317                } 
     318        } 
     319 
     320        /** 
     321         * Import single mysql database 
     322         * @param string $dbname 
     323         */ 
     324        private function importMySQLDatabase($dbname) 
     325        { 
     326                $query = $this->db->Prepare("CREATE DATABASE " . quoteIdentifier($dbname)); 
     327                $this->db->Execute($query); 
     328 
     329                $filename = $this->target_path.'/tmp/'.$dbname.'.sql'; 
     330                $cmd = 'mysql --user '.Config::get('DB_USER').' --password='.Config::get('DB_PASS'). 
     331                           ' '.$dbname. 
     332                           ' <'.$filename; 
     333                // TODO: Error handling 
     334                $a = array(); 
     335                $this->shellExecute($cmd, $a); 
     336 
     337                return true; 
     338        } 
     339 
     340        /** 
     341         * Create MySQL database users 
     342         */ 
     343        protected function createDatabaseUsers() 
     344        { 
     345                foreach ($this->configurationData['dbuser'] as $dbuser) { 
     346                        $this->createDatabaseUser($dbuser['database'], $dbuser['sqlu_name'], $dbuser['sqlu_pass']); 
     347 
     348                        // Insert database row into ispCP database 
     349                        $query = $this->db->Prepare( 
     350                                "INSERT INTO `sql_user`". 
     351                                " (`sqld_id`, `sqlu_name`, `sqlu_pass`)". 
     352                                " VALUES". 
     353                                " (:sqld_id, :sqlu_name, :sqlu_pass)" 
     354                        ); 
     355                        $this->db->Execute($query, array( 
     356                                'sqld_id'=>$this->database_ids[$dbuser['database']], 
     357                                'sqlu_name'=>$dbuser['sqlu_name'], 
     358                                'sqlu_pass'=>encrypt_db_password($dbuser['sqlu_pass']) 
     359                        )); 
     360                } 
     361        } 
     362 
     363        /** 
     364         * Create database user with all privileges to database 
     365         * @param string $db_name database name 
     366         * @param string $db_user database user name 
     367         * @param string $user_pass password of database user 
     368         */ 
     369        private function createDatabaseUser($db_name, $db_user, $user_pass) 
     370        { 
     371                $query = $this->db->Prepare( 
     372                        "GRANT ALL PRIVILEGES ON ". quoteIdentifier($db_name) . 
     373                        ".* TO :dbuser IDENTIFIED BY :dbpass" 
     374                ); 
     375                $this->db->Execute($query, array( 
     376                        'dbuser'=>$db_user.'@localhost', 
     377                        'dbpass'=>$user_pass 
     378                )); 
     379                $this->db->Execute($query, array( 
     380                        'dbuser'=>$db_user.'@%', 
     381                        'dbpass'=>$user_pass 
     382                )); 
     383        } 
     384 
     385        /** 
     386         * Create domain, sets $this->domain_user_id and $this->domain_id 
     387         * @return boolean true = creation successful 
     388         */ 
     389        protected function createDomain() 
     390        { 
     391                $result = false; 
     392 
     393                // TODO: createDomain() 
     394                // TODO: set domain_id 
     395                $this->createDomainAliases(); 
     396                $this->createSubDomains(); 
     397 
     398                // TODO: wait until daemon is ready 
     399                // TODO: set domain_user_id 
     400 
     401                return $result; 
     402        } 
     403 
     404        protected function createDomainAliases() 
     405        { 
     406                // TODO: createDomainAliases 
     407        } 
     408 
     409        protected function createSubDomains() 
     410        { 
     411                // TODO: createSubDomains 
     412        } 
     413 
     414        protected function createEMailAccounts() 
     415        { 
     416                // TODO: createEMailAccounts 
     417        } 
     418 
     419        protected function createFTPAccounts() 
     420        { 
     421                // TODO: createFTPAccounts 
     422        } 
     423 
     424        protected function createWebUsers() 
     425        { 
     426                // TODO: createWebUsers 
     427        } 
     428 
     429        protected function createWebGroups() 
     430        { 
     431                // TODO: createWebGroups 
     432        } 
     433 
    208434        /** 
    209435         * Run the restore, main method 
     436         * TODO: don't forget sub_id in mail accounts! 
    210437         * @return boolean true = restore successful, false = see error messages 
    211438         */ 
     
    215442 
    216443                if ($this->initRestore()) { 
    217                         // TODO: runRestore() 
    218                         // don't forget sub_id in mail accounts! 
     444                        if ($this->unpackDomainPackage()) { 
     445                                if ($this->getConfigData()) { 
     446 
     447                                        if ($this->createDomain()) { 
     448                                                $this->createDNSEntries(); 
     449                                                $this->createDatabases(); 
     450                                                $this->createDBUsers(); 
     451                                                $this->createEMailAccounts(); 
     452                                                $this->createWebUsers(); 
     453                                                $this->createWebGroups(); 
     454                                                $this->setDomainPermissions(); 
     455 
     456                                                $e = $this->getErrorMessages(); 
     457                                                $result = (count($e) == 0); 
     458                                        } 
     459                                } 
     460                        } 
    219461                } 
    220462