public function OnMysqlBackupComplete(MysqlBackupCompleteEvent $event)
{
try {
$DBFarmRole = $event->DBServer->GetFarmRoleObject();
$farm_roleid = $DBFarmRole->ID;
} catch (Exception $e) {
return;
}
if ($event->Operation == MYSQL_BACKUP_TYPE::DUMP) {
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_LAST_BCP_TS, time(), Entity\FarmRoleSetting::TYPE_LCL);
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_IS_BCP_RUNNING, 0, Entity\FarmRoleSetting::TYPE_LCL);
switch ($event->DBServer->platform) {
case SERVER_PLATFORMS::EC2:
$provider = 's3';
break;
default:
$provider = 'unknown';
}
$backup = Scalr_Db_Backup::init();
$backup->service = 'mysql';
$backup->platform = $event->DBServer->platform;
$backup->provider = $provider;
$backup->envId = $event->DBServer->envId;
$backup->farmId = $event->DBServer->farmId;
$backup->cloudLocation = $event->DBServer->GetCloudLocation();
$backup->status = Scalr_Db_Backup::STATUS_AVAILABLE;
$total = 0;
foreach ($event->backupParts as $item) {
if (is_object($item) && $item->size) {
$backup->addPart(str_replace(array("s3://", "cf://"), array("", ""), $item->path), $item->size);
$total = $total + (int) $item->size;
} else {
$backup->addPart(str_replace(array("s3://", "cf://"), array("", ""), $item), 0);
}
}
$backup->size = $total;
$backup->save();
} elseif ($event->Operation == MYSQL_BACKUP_TYPE::BUNDLE) {
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_LAST_BUNDLE_TS, time(), Entity\FarmRoleSetting::TYPE_LCL);
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_IS_BUNDLE_RUNNING, 0, Entity\FarmRoleSetting::TYPE_LCL);
if (!is_array($event->SnapshotInfo)) {
$event->SnapshotInfo = array('snapshotId' => $event->SnapshotInfo);
}
if ($event->SnapshotInfo['snapshotId']) {
if ($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_DATA_STORAGE_ENGINE) == \MYSQL_STORAGE_ENGINE::EBS) {
$this->DB->Execute("\n INSERT INTO ebs_snaps_info\n SET snapid = ?,\n comment = ?,\n dtcreated = NOW(),\n region = ?,\n ebs_array_snapid = '0',\n is_autoebs_master_snap = '1',\n farm_roleid = ?\n ", [$event->SnapshotInfo['snapshotId'], _('MySQL Master volume snapshot'), $event->DBServer->GetProperty(\EC2_SERVER_PROPERTIES::REGION), $DBFarmRole->ID]);
// Scalarizr stuff
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_SNAPSHOT_ID, $event->SnapshotInfo['snapshotId'], Entity\FarmRoleSetting::TYPE_LCL);
$snapshotConfig = new \stdClass();
$snapshotConfig->type = 'ebs';
$snapshotConfig->id = $event->SnapshotInfo['snapshotId'];
$event->SnapshotInfo['snapshotConfig'] = $snapshotConfig;
}
}
if ($event->SnapshotInfo['logFile']) {
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_LOG_FILE, $event->SnapshotInfo['logFile'], Entity\FarmRoleSetting::TYPE_LCL);
}
if ($event->SnapshotInfo['logPos']) {
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_LOG_POS, $event->SnapshotInfo['logPos'], Entity\FarmRoleSetting::TYPE_LCL);
}
try {
$storageSnapshot = Scalr_Storage_Snapshot::init();
$storageSnapshot->loadBy(array('id' => $event->SnapshotInfo['snapshotConfig']->id, 'client_id' => $event->DBServer->clientId, 'farm_id' => $event->DBServer->farmId, 'farm_roleid' => $event->DBServer->farmRoleId, 'env_id' => $event->DBServer->envId, 'name' => sprintf(_("MySQL data bundle #%s"), $event->SnapshotInfo['snapshotConfig']->id), 'type' => $DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_DATA_STORAGE_ENGINE), 'platform' => $event->DBServer->platform, 'description' => sprintf(_("MySQL data bundle created on Farm '%s' -> Role '%s'"), $DBFarmRole->GetFarmObject()->Name, $DBFarmRole->GetRoleObject()->name), 'ismysql' => true, 'service' => ROLE_BEHAVIORS::MYSQL));
$storageSnapshot->setConfig($event->SnapshotInfo['snapshotConfig']);
$storageSnapshot->save(true);
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_SCALR_SNAPSHOT_ID, $storageSnapshot->id, Entity\FarmRoleSetting::TYPE_LCL);
} catch (Exception $e) {
$this->Logger->fatal("Cannot save storage snapshot: {$e->getMessage()}");
}
}
}