AWS EC2スナップショットの作成と復元
- 2019.11.01
- AWS

はじめに
EC2インスタンスのスナップショットを作成したり復元したりしてみます。
テスト用EC2インスタンスの作成
毎回同じようなEC2インスタンスを作成するのが面倒なのでJSONファイルから作成するようにします。起動テンプレートを使った方がもっと簡単なのかな。
{
"ImageId": "ami-0064e711cbc7a825e",
"InstanceType": "t2.micro",
"KeyName": "myawskey-tokyo",
"MaxCount": 1,
"SecurityGroupIds": [
"sg-04b4e7642a8fc6efe"
],
"SubnetId": "subnet-0b99e52d17314979d"
}
次のコマンドでEC2インスタンスを作成します。
$ aws ec2 run-instances --cli-input-json file://ec2-amazonlinux.json
$ vol_id=vol-0000f3210040fe16b
テスト用ファイルを作成しておきます。
$ aws desc-ins-vpc $vpc_id
[
{
"InstanceId": "i-0878c4589b0c46ec5",
"State": "running",
"AvailabirityZone": "ap-northeast-1a",
"PrivateDnsName": "ip-10-1-0-173.ap-northeast-1.compute.internal",
"PrivateIpAddress": "10.1.0.173",
"PublicDnsName": "ec2-hoge.ap-northeast-1.compute.amazonaws.com",
"PublicIpAddress": "hoge",
"Name": null
}
]
$ ins_pubdns=ec2-hoge.ap-northeast-1.compute.amazonaws.com
$ ssh -i $key ec2-user@$ins_pubdns
[ec2-user@ip-hoge ~]$ echo "test_data" > test.txt
この状態で一旦スナップショットを取っておきます。
スナップショットの作成
$ aws ec2 create-snapshot --description "snapshot test" --volume-id $vol_id
{
"Description": "snapshot test",
"Encrypted": false,
"OwnerId": "281230433728",
"Progress": "",
"SnapshotId": "snap-0fedc3af65c52001a",
"StartTime": "2019-11-01T07:05:14.000Z",
"State": "pending",
"VolumeId": "vol-0000f3210040fe16b",
"VolumeSize": 8,
"Tags": []
}
これで作成できます。作成の状況を確認してみます。
$ aws ec2 describe-snapshots --owner-ids self
{
"Snapshots": [
{
"Description": "snapshot test",
"Encrypted": false,
"OwnerId": "281230433728",
"Progress": "1%",
"SnapshotId": "snap-0fedc3af65c52001a",
"StartTime": "2019-11-01T07:05:14.847Z",
"State": "pending",
"VolumeId": "vol-0000f3210040fe16b",
"VolumeSize": 8
}
]
}
まだ1%なので待ちましょう。
$ aws ec2 describe-snapshots --owner-ids self
{
"Snapshots": [
{
"Description": "snapshot test",
"Encrypted": false,
"OwnerId": "281230433728",
"Progress": "100%",
"SnapshotId": "snap-0fedc3af65c52001a",
"StartTime": "2019-11-01T07:05:14.847Z",
"State": "completed",
"VolumeId": "vol-0000f3210040fe16b",
"VolumeSize": 8
}
]
}
$snap_id=snap-0fedc3af65c52001a
これで作成完了です。
テスト用ファイルを削除
それでは復元のテストをするためにテスト用ファイルを削除してみます。
[ec2-user@ip-hoge ~]$ rm test.txt
[ec2-user@ip-hoge ~]$ ls
[ec2-user@ip-hoge ~]$ exit
この状態でスナップショットを復元してみます。
スナップショットの復元
スナップショットを復元するには最初にスナップショットからボリュームを作成する必要があるようです。
$ aws ec2 create-volume --availability-zone ap-northeast-1a --snapshot-id $snap_id
{
"AvailabilityZone": "ap-northeast-1a",
"CreateTime": "2019-11-01T07:19:25.000Z",
"Encrypted": false,
"Size": 8,
"SnapshotId": "snap-0fedc3af65c52001a",
"State": "creating",
"VolumeId": "vol-0cf2fb7995f669ccc",
"Iops": 100,
"Tags": [],
"VolumeType": "gp2"
}
作成したボリュームを確認してみます。
$ aws ec2 describe-volumes
{
"Volumes": [
{
"Attachments": [
{
"AttachTime": "2019-11-01T06:50:08.000Z",
"Device": "/dev/xvda",
"InstanceId": "i-0878c4589b0c46ec5",
"State": "attached",
"VolumeId": "vol-0000f3210040fe16b",
"DeleteOnTermination": true
}
],
"AvailabilityZone": "ap-northeast-1a",
"CreateTime": "2019-11-01T06:50:08.555Z",
"Encrypted": false,
"Size": 8,
"SnapshotId": "snap-0bdffaa2da723d01c",
"State": "in-use",
"VolumeId": "vol-0000f3210040fe16b",
"Iops": 100,
"VolumeType": "gp2"
},
{
"Attachments": [],
"AvailabilityZone": "ap-northeast-1a",
"CreateTime": "2019-11-01T07:19:25.901Z",
"Encrypted": false,
"Size": 8,
"SnapshotId": "snap-0fedc3af65c52001a",
"State": "available",
"VolumeId": "vol-0cf2fb7995f669ccc",
"Iops": 100,
"VolumeType": "gp2"
}
]
}
$ vol_id2=vol-0cf2fb7995f669ccc
availableとなっていて、何もアタッチされていないボリュームが今回作成したボリュームです。それでは起動しているEC2インスタンスを停止して、ボリュームを切り替えていきます。
$ aws ec2 stop-instances --instance-ids $ins_id
{
"StoppingInstances": [
{
"CurrentState": {
"Code": 64,
"Name": "stopping"
},
"InstanceId": "i-0878c4589b0c46ec5",
"PreviousState": {
"Code": 16,
"Name": "running"
}
}
]
}
$ aws ec2 detach-volume --volume-id $vol_id
{
"AttachTime": "2019-11-01T06:50:08.000Z",
"Device": "/dev/xvda",
"InstanceId": "i-0878c4589b0c46ec5",
"State": "detaching",
"VolumeId": "vol-0000f3210040fe16b"
}
$ aws ec2 attach-volume --device /dev/xvda --instance-id $ins_id --volume-id $vol_id2
{
"AttachTime": "2019-11-01T07:27:25.732Z",
"Device": "/dev/xvda",
"InstanceId": "i-0878c4589b0c46ec5",
"State": "attaching",
"VolumeId": "vol-0cf2fb7995f669ccc"
}
$ aws ec2 start-instances --instance-ids $ins_id
{
"StartingInstances": [
{
"CurrentState": {
"Code": 0,
"Name": "pending"
},
"InstanceId": "i-0878c4589b0c46ec5",
"PreviousState": {
"Code": 80,
"Name": "stopped"
}
}
]
}
復元の確認
それでは実際にsshで入ってみて先ほど削除したtest.txtが復元されているか確認してみます。
$ aws desc-ins-vpc $vpc_id
[
{
"InstanceId": "i-0878c4589b0c46ec5",
"State": "running",
"AvailabirityZone": "ap-northeast-1a",
"PrivateDnsName": "ip-10-1-0-173.ap-northeast-1.compute.internal",
"PrivateIpAddress": "10.1.0.173",
"PublicDnsName": "ec2-hoge.ap-northeast-1.compute.amazonaws.com",
"PublicIpAddress": "hoge",
"Name": null
}
]
$ ssh -i $key ec2-user@hoge.ap-northeast-1.compute.amazonaws.com
[ec2-user@ip-hoge ~]$ ls
test.txt
[ec2-user@ip-hoge ~]$ cat test.txt
test_data
きちんと復元できていました!よくありそうなオペレーションなので、スムーズにできるようになっておきたいです。
-
前の記事
AWS Auto Scalingの構築 2019.11.01
-
次の記事
AWS VM Import を試してみる 2019.11.05