示例#1
0
 public function init($myRole, $drivers)
 {
     //parent::init($myRole, $drivers);
     $this->_out->logNotice(">>>init " . get_class($this) . " driver as {$myRole}");
     // Amazon library SSL Connection Issues
     if (!defined('AWS_CERTIFICATE_AUTHORITY')) {
         define('AWS_CERTIFICATE_AUTHORITY', $this->_options['certificate_authority']);
     } else {
         $this->_out->logNotice("option 'certificate_authority' was already set, it can't be changed");
     }
     // receive information about the RDS instance
     $rds = new AmazonRDS(array('key' => $this->_options['key']['access'], 'secret' => $this->_options['key']['secret']));
     if ($this->_options['region']) {
         $r = new ReflectionObject($rds);
         $rds->set_region($r->getConstant($this->_options['region']));
     }
     if ($this->_options['dbinstance']) {
         $response = $rds->describe_db_instances(array('DBInstanceIdentifier' => $this->_options['dbinstance']));
         if (!$response->isOK()) {
             throw new Core_StopException("Not possible to get information about RDS instance '" . $this->_options['dbinstance'] . "'.", "MysqlAmazonRdsInit");
         }
         $instance = $response->body->DescribeDBInstancesResult->DBInstances[0]->DBInstance;
     } else {
         throw new Core_StopException("You have to provide parameter 'dbinstance', finding server based on server name/IP is not supported at this moment.", "MysqlAmazonRdsInit");
         $response = $rds->describe_db_instances();
         if (!$response->isOK()) {
             throw new Core_StopException("Not possible to get information about RDS instances.", "MysqlAmazonRdsInit");
         }
         // find instance name with mysql server configured in mysql storage
         foreach ($response->body->DescribeDBInstancesResult->DBInstances->children() as $instance) {
             if ($instance->Endpoint->Address == "") {
                 die;
             }
         }
     }
     //DBInstanceStatus
     $backupRetentionPeriod = $this->_fixGet($instance, 'BackupRetentionPeriod') * 1;
     if (!$backupRetentionPeriod > 0) {
         throw new Core_StopException("You need to set BackupRetentionPeriod>0 on RDS instance. Otherwise use the MySql and not the MysqlAmazonRds storage class.", "MysqlAmazonRdsInit");
     }
     $engine = $this->_fixGet($instance, 'Engine');
     if ($engine !== "mysql") {
         throw new Core_StopException("RDS instances has to use MySql, the current engine is '{$engine}'.", "MysqlAmazonRdsInit");
     }
     if ($this->_options['tempname']) {
         $tempName = $this->_options['tempname'];
     } else {
         $tempName = $this->_fixGet($instance, 'DBInstanceIdentifier') . "-BAK";
     }
     $response = $rds->describe_db_instances(array('DBInstanceIdentifier' => $tempName));
     $bakExists = $response->isOK();
     if ($bakExists && $this->_options['ifexists'] == 'exit') {
         throw new Core_StopException("There is already RDS instance named '{$tempName}', this name should be used for temporary DB instance.", "MysqlAmazonRdsInit");
     }
     if (!$bakExists) {
         // create temporary DB instance
         $opt = array('UseLatestRestorableTime' => true, 'AvailabilityZone' => $this->_fixGet($instance, 'AvailabilityZone'));
         if (false !== $this->_options['dbinstanceclass']) {
             $opt['DBInstanceClass'] = $this->_options['dbinstanceclass'];
         }
         $rds->restore_db_instance_to_point_in_time($this->_options['dbinstance'], $tempName, $opt);
         $dbInstanceName = $this->_options['dbinstance'];
         $this->_out->logNotice("point in time restore of '{$dbInstanceName}' started and '{$tempName}' will be created");
     }
     // wait for readiness
     $job = $this->_out->jobStart("waiting for temporary RDS instance '{$tempName}' to become 'available'");
     do {
         $response = $rds->describe_db_instances(array('DBInstanceIdentifier' => $tempName));
         $tmpInstance = $response->body->DescribeDBInstancesResult->DBInstances[0]->DBInstance;
         $status = $this->_fixGet($tmpInstance, 'DBInstanceStatus');
         // TODO reverse this check, this way may loop forever if unpredicted status is returned
         if (in_array($status, array('available'))) {
             break;
         }
         if (in_array($status, array('failed', 'storage-full', 'incompatible-parameters', 'incompatible-restore'))) {
             throw new Core_StopException("RDS backup instance '{$tempName}' has stalled in status '{$status}'. Please fix the situation and restart backup.", "MysqlAmazonRdsInit");
         }
         sleep(3);
     } while (true);
     $this->_out->jobEnd($job, "ready");
     // configure and execute mysql backup
     $this->_mysql->setHost($this->_fixGet($tmpInstance->Endpoint, 'Address'), $this->_fixGet($tmpInstance->Endpoint, 'Port'));
     $drivers['local'] = $this->_mysql;
     $this->_mysql->init($myRole, $drivers);
     if ($this->_options['droptemp']) {
         // drop temporary instance
         $job = $this->_out->jobStart("droping temporary RDS instance '{$tempName}'");
         $response = $rds->delete_db_instance($tempName, array('SkipFinalSnapshot' => true));
         if (!$response->isOK()) {
             $this->_out->jobEnd($job, "failed");
         } else {
             $this->_out->jobEnd($job, "started, not waiting for finish");
         }
     }
 }