Last active
August 29, 2015 14:06
-
-
Save Twinuma/1cb4ce7642df5db36a2f to your computer and use it in GitHub Desktop.
AMIとSnapshotを自動取得。tagに"Backup-Generation/世代設定"をすることで可能。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/php | |
| <?php | |
| // 初期設定 | |
| require_once("/opt/aws/AWSSDKforPHP/sdk.class.php"); | |
| date_default_timezone_set("Asia/Tokyo"); | |
| $ec2 = new AmazonEC2(array( | |
| "key" => "YOUR ACCESS KEY", | |
| "secret" => "YOUR SECRET KEY" | |
| )); | |
| $ec2->set_region(AmazonEC2::REGION_APAC_NE1); | |
| error_log(date("Y/m/d H:i:s") . " [Info] Begin create images."); | |
| // 対象EC2(runnning)の取得 | |
| $response = $ec2->describe_instances(array( | |
| "Filter" => array( | |
| array("Name" => "instance-state-name", "Value" => "running") | |
| ) | |
| )); | |
| if(!$response->isOK()) { | |
| error_log(date("Y/m/d H:i:s") . " [" . $response->body->Errors->Error->Code . "] " . $response->body->Errors->Error->Message); | |
| } | |
| // 対象EC2に対するバックアップ処理 | |
| if(isset($response->body->reservationSet->item)) { | |
| foreach($response->body->reservationSet->item as $reservation) { | |
| foreach($reservation->instancesSet->item as $instance) { | |
| error_log(date("Y/m/d H:i:s") . " [Info] Begin execute instance(" . $instance->instanceId . ")."); | |
| $is_backup = false; | |
| $instance_id = $instance->instanceId; | |
| $image_tag = $instance_id; | |
| // バックアップ条件の確認 | |
| if(isset($instance->tagSet->item)) { | |
| foreach($instance->tagSet->item as $tag) { | |
| if($tag->key == "Name" && $tag->value != null && trim($tag->value) != "") { | |
| $image_tag = $tag->value; | |
| } | |
| if($tag->key == "Backup-Generation" && is_numeric($tag->value->to_string()) && intval($tag->value) > 0) { | |
| $is_backup = true; | |
| $generation = intval($tag->value); | |
| } | |
| } | |
| } | |
| // バックアップと世代管理の実施 | |
| if($is_backup) { | |
| // AMI名の作成 | |
| $image_name = $image_tag . "-" . date("YmdHis"); | |
| // AMIの作成 | |
| $image_id = create_image($ec2, $image_name, $instance_id); | |
| // 削除対象AMIの取得 | |
| $images = find_delete_images($ec2, $image_tag, $generation); | |
| // AMIとスナップショットの削除 | |
| delete_images($ec2, $images); | |
| // AMIにタグ付け | |
| tag_image($ec2, $image_id, $image_tag); | |
| // スナップショットにタグ付け | |
| tag_snapshots($ec2, $image_id); | |
| } else { | |
| error_log(date("Y/m/d H:i:s") . " [Info] Skip create image from " . $instance->instanceId . "."); | |
| } | |
| error_log(date("Y/m/d H:i:s") . " [Info] End execute instance(" . $instance->instanceId . ")."); | |
| } | |
| } | |
| } | |
| error_log(date("Y/m/d H:i:s") . " [Info] End create images."); | |
| exit(0); | |
| // AMIの作成 | |
| function create_image($ec2, $image_name, $instance_id) { | |
| error_log(date("Y/m/d H:i:s") . " [Info] Begin create image(" . $image_name . ") from " . $instance_id . "."); | |
| $response = $ec2->create_image( | |
| $instance_id, | |
| $image_name, | |
| array( | |
| "Description" => "Create from " . $instance_id . ".", | |
| "NoReboot" => true | |
| ) | |
| ); | |
| if(!$response->isOK()) { | |
| error_log(date("Y/m/d H:i:s") . " [" . $response->body->Errors->Error->Code . "] " . $response->body->Errors->Error->Message); | |
| } | |
| error_log(date("Y/m/d H:i:s") . " [Info] End create image(" . $image_name . ") from " . $instance_id . "."); | |
| return $response->body->imageId; | |
| } | |
| // 削除対象AMIの取得 | |
| function find_delete_images($ec2, $image_tag, $generation) { | |
| $response = $ec2->describe_images(array("Filter" => array( | |
| array("Name" => "tag:Name" , "Value" => $image_tag), | |
| array("Name" => "tag:Backup-Type", "Value" => "auto") | |
| ))); | |
| if(!$response->isOK()) { | |
| error_log(date("Y/m/d H:i:s") . " [" . $response->body->Errors->Error->Code . "] " . $response->body->Errors->Error->Message); | |
| } | |
| $images = array(); | |
| foreach($response->body->imagesSet->item as $image) { | |
| $images["$image->name"] = array( | |
| "id" => $image->imageId, | |
| "snapshots" => $image->blockDeviceMapping | |
| ); | |
| } | |
| krsort($images); | |
| return array_slice($images, $generation - 1); | |
| } | |
| // AMIとスナップショットの削除 | |
| function delete_images($ec2, $images) { | |
| foreach($images as $image_name => $image) { | |
| error_log(date("Y/m/d H:i:s") . " [Info] Begin delete image(" . $image_name . ")."); | |
| $image_id = $image["id"]; | |
| $response = $ec2->deregister_image($image_id); | |
| if(!$response->isOK()) { | |
| error_log(date("Y/m/d H:i:s") . " [" . $response->body->Errors->Error->Code . "] " . $response->body->Errors->Error->Message); | |
| } | |
| error_log(date("Y/m/d H:i:s") . " [Info] End delete image(" . $image_name . ")."); | |
| foreach($image["snapshots"]->item as $snapshot) { | |
| $snapshot_id = $snapshot->ebs->snapshotId; | |
| error_log(date("Y/m/d H:i:s") . " [Info] Begin delete snapshot(" . $snapshot_id . ")."); | |
| $response = $ec2->delete_snapshot($snapshot_id); | |
| if(!$response->isOK()) { | |
| error_log(date("Y/m/d H:i:s") . " [" . $response->body->Errors->Error->Code . "] " . $response->body->Errors->Error->Message); | |
| } | |
| error_log(date("Y/m/d H:i:s") . " [Info] End delete snapshot(" . $snapshot_id . ")."); | |
| } | |
| } | |
| } | |
| // AMIにタグ付け | |
| function tag_image($ec2, $image_id, $image_tag) { | |
| error_log(date("Y/m/d H:i:s") . " [Info] Begin tag(" . $image_tag . ") image to " . $image_id . "."); | |
| $response = $ec2->create_tags($image_id, array( | |
| array("Key" => "Name" , "Value" => $image_tag), | |
| array("Key" => "Backup-Type", "Value" => "auto"), | |
| )); | |
| if(!$response->isOK()) { | |
| error_log(date("Y/m/d H:i:s") . " [" . $response->body->Errors->Error->Code . "] " . $response->body->Errors->Error->Message); | |
| } | |
| error_log(date("Y/m/d H:i:s") . " [Info] End tag(" . $image_tag . ") image to " . $image_id . "."); | |
| } | |
| // スナップショットにタグ付け | |
| function tag_snapshots($ec2, $image_id) { | |
| $response = $ec2->describe_images(array("ImageId" => $image_id)); | |
| foreach($response->body->imagesSet->item as $image) { | |
| foreach($image->blockDeviceMapping->item as $snapshot) { | |
| $snapshot_id = $snapshot->ebs->snapshotId; | |
| $snapshot_tag = $image->name . "-" . basename($snapshot->deviceName); | |
| error_log(date("Y/m/d H:i:s") . " [Info] Begin tag(" . $snapshot_tag . ") snapshot to " . $snapshot_id . "."); | |
| $response = $ec2->create_tags($snapshot_id, array( | |
| array("Key" => "Name" , "Value" => $snapshot_tag), | |
| array("Key" => "Backup-Type", "Value" => "auto") | |
| )); | |
| if(!$response->isOK()) { | |
| error_log(date("Y/m/d H:i:s") . " [" . $response->body->Errors->Error->Code . "] " . $response->body->Errors->Error->Message); | |
| } | |
| error_log(date("Y/m/d H:i:s") . " [Info] End tag(" . $snapshot_tag . ") snapshot to " . $snapshot_id . "."); | |
| } | |
| } | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 0 0 * * * /usr/bin/php /opt/aws/php/backup/ami-ebs-backup.php &> /opt/aws/php/backup/log/aws-backup-`date +\%Y\%m\%d\%H\%M\%S`.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment