質問編集履歴

17

文章追加

2019/01/10 13:45

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -6,8 +6,6 @@
6
6
 
7
7
  aws cliでは、コマンドを作れるのですが、スクリプトで書けず、止まっています。
8
8
 
9
-
10
-
11
9
  ~やりたい内容~
12
10
 
13
11
  1.
@@ -54,8 +52,6 @@
54
52
 
55
53
  新インスタンスに接続する。
56
54
 
57
-
58
-
59
55
  AWS cli のコマンドはありますが、.shのファイルにそのまま記載しても、当たり前ではありますが、実行はされず、
60
56
 
61
57
  困っています。
@@ -64,22 +60,16 @@
64
60
 
65
61
  cronなどは使わず。
66
62
 
67
-
68
-
69
- ```ここに言語を入力
63
+ ```
70
64
 
71
65
  #!/usr/bin/env bash
72
66
 
73
-
74
-
75
67
  _INSTANCE_ID="${i-xxxxxxxxxxx}"
76
68
 
77
69
  _ROOT_DEVICE="/dev/sda1"
78
70
 
79
71
  _CHECK_INTERVAL=60
80
72
 
81
-
82
-
83
73
  echo "処理を続行しますか? [Y/n]"
84
74
 
85
75
  read ANSWER
@@ -100,16 +90,12 @@
100
90
 
101
91
  esac
102
92
 
103
-
104
-
105
93
  if [ -f tmp/snapshot_ids ];then
106
94
 
107
95
  rm tmp/snapshot_ids
108
96
 
109
97
  fi
110
98
 
111
-
112
-
113
99
  for ebs in $(aws ec2 describe-instances --filters Name=instance-id,Values=${_INSTANCE_ID} --query 'Reservations[].Instances[].BlockDeviceMappings[].Ebs.VolumeId' --output text);
114
100
 
115
101
  do
@@ -132,14 +118,8 @@
132
118
 
133
119
  done
134
120
 
135
-
136
-
137
121
  if [ `-f tmp/snapshot_ids -a $(wc -l tmp/snapshot_ids | awk '{print $1}') == 2` ];then
138
122
 
139
-
140
-
141
-
142
-
143
123
  ```
144
124
 
145
125
  ↑ここの行で
@@ -150,9 +130,7 @@
150
130
 
151
131
  スナップショットの作成に失敗しました.処理を中断します.
152
132
 
153
- このように表示されてしまいます。
133
+ このように表示されてしまいます。(2-1.解決済)
154
-
155
- (2-1.解決済)
156
134
 
157
135
  どうかご教示頂きたいです。
158
136
 
@@ -172,8 +150,6 @@
172
150
 
173
151
  #!/usr/bin/env bash
174
152
 
175
-
176
-
177
153
  _INSTANCE_ID="i-xxxxxxxxxxxx"
178
154
 
179
155
  _ROOT_DEVICE="/dev/sda1"
@@ -206,16 +182,12 @@
206
182
 
207
183
  esac
208
184
 
209
-
210
-
211
185
  if [ -f tmp/snapshot_ids ];then
212
186
 
213
187
  rm tmp/snapshot_ids
214
188
 
215
189
  fi
216
190
 
217
-
218
-
219
191
  for ebs in $(aws ec2 describe-instances --filters Name=instance-id,Values=${_INSTANCE_ID} --query 'Reservations[].Instances[].BlockDeviceMappings[].Ebs.VolumeId' --output text);
220
192
 
221
193
  do
@@ -238,8 +210,6 @@
238
210
 
239
211
  done
240
212
 
241
-
242
-
243
213
  #if [ -f tmp/snapshot_ids -a $(cat tmp/snapshot_ids | wc -l) = "2" ];then
244
214
 
245
215
  # echo ${_SLACK_CHANNEL} "スナップショットの作成を開始しました..."
@@ -252,8 +222,6 @@
252
222
 
253
223
  #fi
254
224
 
255
-
256
-
257
225
  aws ec2 wait snapshot-completed --snapshot-ids $(cat tmp/snapshot_ids)
258
226
 
259
227
  if [ $? == 0 ]; then
@@ -262,8 +230,6 @@
262
230
 
263
231
  fi
264
232
 
265
-
266
-
267
233
  # tmp/snapshot_ids のチェック
268
234
 
269
235
  if [ ! -f tmp/snapshot_ids ];then
@@ -274,8 +240,6 @@
274
240
 
275
241
  fi
276
242
 
277
-
278
-
279
243
  # ボリュームの作成
280
244
 
281
245
  echo "スナップショットからボリュームを作成します..."
@@ -284,8 +248,6 @@
284
248
 
285
249
  do
286
250
 
287
-
288
-
289
251
  _DEVICE=$(aws ec2 describe-tags --filters "Name=resource-id,Values=${snapshot_id}" "Name=key,Values=device" --query Tags[].Value --output text)
290
252
 
291
253
  _VOLUME_ID=$(aws ec2 create-volume --availability-zone ap-northeast-1a --snapshot-id ${snapshot_id} --volume-type gp2 --query VolumeId --output text)
@@ -322,20 +284,6 @@
322
284
 
323
285
  ```
324
286
 
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
287
  ### *2
340
288
 
341
289
  ```aws ec2 wait snapshot-completed --snapshot-ids $(cat tmp/snapshot_ids)
@@ -358,8 +306,6 @@
358
306
 
359
307
  fi
360
308
 
361
-
362
-
363
309
  # ボリュームの作成
364
310
 
365
311
  echo "スナップショットからボリュームを作成します..."
@@ -368,8 +314,6 @@
368
314
 
369
315
  do
370
316
 
371
-
372
-
373
317
  _DEVICE=$(aws ec2 describe-tags --filters "Name=resource-id,Values=${snapshot_id}" "Name=key,Values=device" --query Tags[].Value --output text)
374
318
 
375
319
  _VOLUME_ID=$(aws ec2 create-volume --availability-zone ap-northeast-1a --snapshot-id ${snapshot_id} --volume-type gp2 --query VolumeId --output text)
@@ -404,10 +348,6 @@
404
348
 
405
349
  ```
406
350
 
407
-
408
-
409
-
410
-
411
351
  ### *3
412
352
 
413
353
  ```
@@ -438,14 +378,8 @@
438
378
 
439
379
  if [ _RESULT_1=0,_RESULT_2=0 ];then
440
380
 
441
-
442
-
443
-
444
-
445
381
  #~set により解読~
446
382
 
447
-
448
-
449
383
  ++ aws ec2 describe-tags --filters Name=resource-id,Values=snap-xxx Name=key,Values=device --query 'Tags[].Value' --output text
450
384
 
451
385
  + _DEVICE=/dev/sda1
@@ -467,3 +401,141 @@
467
401
  + '[' _RESULT_1=0,_RESULT_2=0 ']'
468
402
 
469
403
  ```
404
+
405
+ ### *4
406
+
407
+ ```
408
+
409
+ #条件変更(インスタンス停止したいIDを_INSTANCE1_IDとしました)
410
+
411
+ #!/usr/bin/env bash
412
+
413
+ #original#########################
414
+
415
+ _INSTANCE_ID="i-xxx"
416
+
417
+ #create instance##################
418
+
419
+ _INSTANCE1_ID="i-yyy"
420
+
421
+ #tag##############################
422
+
423
+ _OS="OS1"
424
+
425
+ _OS_restore="OS1_restore"
426
+
427
+ ######################################
428
+
429
+ _ROOT_DEVICE="/dev/sda1"
430
+
431
+ _CHECK_INTERVAL="60"
432
+
433
+ #~~~~~前回のresultの続きボリューム作成後になります。
434
+
435
+ # インスタンスの停止
436
+
437
+ echo "インスタンスを停止します..."
438
+
439
+ aws ec2 stop-instances --instance-ids ${_INSTANCE1_ID}
440
+
441
+ echo "インスタンスの停止まで待機します..."
442
+
443
+ aws ec2 wait instance-stopped --instance-ids ${_INSTANCE1_ID}
444
+
445
+ if [ $? -eq 0 ];then
446
+
447
+ echo "インスタンスの停止が完了しました. 引続き処理を続行します."
448
+
449
+ else
450
+
451
+ echo "インスタンスの停止に失敗しました."
452
+
453
+ exit 1
454
+
455
+ fi
456
+
457
+ # インスタンスからボリュームをデタッチ
458
+
459
+ echo "インスタンスからボリュームをデタッチします..."
460
+
461
+ for ebs in $(aws ec2 describe-instances --filters Name=instance-id,Values=${_INSTANCE1_ID} --query 'Reservations[].Instances[].BlockDeviceMappings[].Ebs.VolumeId' --output text);
462
+
463
+ do
464
+
465
+ aws ec2 detach-volume --volume-id ${ebs}
466
+
467
+ done
468
+
469
+ echo "インスタンスからボリュームをデタッチしました. 引続き処理を続行します."
470
+
471
+ # インスタンスに付与するボリュームの情報を取得
472
+
473
+ echo "インスタンスに付与するボリュームの情報を取得します..."
474
+
475
+ if [ -f tmp/volume_info ];then
476
+
477
+ rm tmp/volume_info
478
+
479
+ fi
480
+
481
+ for _NAME in _OS_restore DATA_restore
482
+
483
+ do
484
+
485
+ aws ec2 describe-volumes \
486
+
487
+ --filters Name=tag-key,Values="Name" Name=tag-value,Values="${_NAME}" \
488
+
489
+ | jq -r '[.Volumes[]|.VolumeId,(.Tags[] | select(.Key=="device").Value)]|@csv' >> tmp/volume_info
490
+
491
+ done
492
+
493
+ echo "インスタンスに付与するボリュームの情報を取得しました. 引続き処理を続行します."
494
+
495
+ echo "インスタンスにボリュームをアタッチします..."
496
+
497
+ _VOLUME_IDS=()
498
+
499
+ OLDIFS=${IFS}
500
+
501
+ while read volume_info
502
+
503
+ do
504
+
505
+ IFS=','
506
+
507
+ set -- $volume_info
508
+
509
+ _VOLUME_ID=$(echo $1 | tr -d '"')
510
+
511
+ _DEVICE=$(echo $2 | tr -d '"')
512
+
513
+ echo ${_VOLUME_ID}
514
+
515
+ _VOLUME_IDS=("${_VOLUME_IDS[@]}" ${_VOLUME_ID})
516
+
517
+ aws ec2 attach-volume --volume-id ${_VOLUME_ID} --instance-id ${_INSTANCE1_ID} --device ${_DEVICE}
518
+
519
+ done < tmp/volume_info
520
+
521
+ IFS=${OLDIFS}
522
+
523
+ # インスタンスにボリュームがアタッチされるまで待機する
524
+
525
+ aws ec2 wait volume-in-use --volume-ids "${_VOLUME_IDS[@]}"
526
+
527
+ if [ $? -eq 0 ];then
528
+
529
+ echo "ボリュームのアタッチが完了しました. 引続き処理を続行します."
530
+
531
+ else
532
+
533
+ echo "ボリュームのアタッチに失敗しました. 処理を中断します."
534
+
535
+ exit 1
536
+
537
+ fi
538
+
539
+ ```
540
+
541
+ 上記の続きをしました所、アタッチする部分で躓きました。

16

文章の追加

2019/01/10 13:45

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -403,3 +403,67 @@
403
403
  fi
404
404
 
405
405
  ```
406
+
407
+
408
+
409
+
410
+
411
+ ### *3
412
+
413
+ ```
414
+
415
+ _DEVICE=$(aws ec2 describe-tags --filters "Name=resource-id,Values=${snapshot_id}" "Name=key,Values=device" --query Tags[].Value --output text)
416
+
417
+ _VOLUME_ID=$(aws ec2 create-volume --availability-zone ap-northeast-1a --snapshot-id ${snapshot_id} --volume-type gp2 --query VolumeId --output text)
418
+
419
+ if [ "${_DEVICE}" = "/dev/sda1" ];then
420
+
421
+ aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value=${_OS1_restore}
422
+
423
+ [ $? -eq 0 ] && _RESULT_1=0 || _RESULT_1=1
424
+
425
+ else
426
+
427
+ aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="DATA_restore"
428
+
429
+ [ $? -eq 0 ] && _RESULT_2=0 || _RESULT_2=1
430
+
431
+ fi
432
+
433
+ done < tmp/snapshot_ids
434
+
435
+ #_RESULT=${_RESULT_1}${_RESULT_2}
436
+
437
+ #if [ "${_RESULT}" = "00" ];then
438
+
439
+ if [ _RESULT_1=0,_RESULT_2=0 ];then
440
+
441
+
442
+
443
+
444
+
445
+ #~set により解読~
446
+
447
+
448
+
449
+ ++ aws ec2 describe-tags --filters Name=resource-id,Values=snap-xxx Name=key,Values=device --query 'Tags[].Value' --output text
450
+
451
+ + _DEVICE=/dev/sda1
452
+
453
+ ++ aws ec2 create-volume --availability-zone ap-northeast-1a --snapshot-id snap-xxx --volume-type gp2 --query VolumeId --output text
454
+
455
+ + _VOLUME_ID=vol-xxx
456
+
457
+ + '[' /dev/sda1 = /dev/sda1 ']'
458
+
459
+ + aws ec2 create-tags --resources vol-xxx --tags Key=device,Value=/dev/sda1 Key=Name,Value=OS1_restore
460
+
461
+ + '[' 0 -eq 0 ']'
462
+
463
+ + _RESULT_1=0
464
+
465
+ + read snapshot_id
466
+
467
+ + '[' _RESULT_1=0,_RESULT_2=0 ']'
468
+
469
+ ```

15

文章更新

2019/01/10 13:18

投稿

momo2
momo2

スコア21

test CHANGED
@@ -1 +1 @@
1
- AWSにおいてEBSスナップショットを作成してEC2を復元させるシェルスクリプトについて(1/2)
1
+ AWSにおいてEBSスナップショットを作成してEC2を復元させるシェルスクリプトについて
test CHANGED
File without changes

14

文章見直し

2019/01/10 12:58

投稿

momo2
momo2

スコア21

test CHANGED
@@ -1 +1 @@
1
- AWSにおいてEBSスナップショットを作成してEC2を復元させるシェルスクリプトについて
1
+ AWSにおいてEBSスナップショットを作成してEC2を復元させるシェルスクリプトについて(1/2)
test CHANGED
File without changes

13

文章見直し

2019/01/10 07:23

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -152,7 +152,7 @@
152
152
 
153
153
  このように表示されてしまいます。
154
154
 
155
- (解決済)
155
+ 2-1.解決済)
156
156
 
157
157
  どうかご教示頂きたいです。
158
158
 

12

文章見直し

2019/01/10 07:06

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
  4.
46
46
 
47
- 作ったボリュームを新しい3.で作ったインスタンスにアタッチする。
47
+ 2.で作ったボリュームを新しい3.で作ったインスタンスにアタッチする。
48
48
 
49
49
  5.
50
50
 

11

文章見直し

2019/01/10 07:05

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,8 @@
36
36
 
37
37
  (必要分だけ、インスタンスを作成する)
38
38
 
39
+ (*これに関しては、事前に手動での作成も考えている)
40
+
39
41
  3-2.
40
42
 
41
43
  全てボリュームをデタッチする。

10

文章見直し

2019/01/10 07:04

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -14,52 +14,46 @@
14
14
 
15
15
  インスタンス作成(original)
16
16
 
17
- (大本インスタンスをる)
17
+ (大本となるインスタンスをさせる)
18
18
 
19
19
  2.
20
20
 
21
+ 2-1
22
+
21
- 1.で作ったインスタンスo停止させる。
23
+ 大本のインスタンスを作った、ボリュームからスナップショットを作成する。
24
+
22
-
25
+ 2-2
26
+
27
+ そのスナップショットから複製に使うボリュームを作成する。
28
+
23
- その後、ボリュームをデタッチする
29
+ (必要分だけ、ボリュームを作成する
24
30
 
25
31
  3.
26
32
 
27
- 3-1
33
+ 3-1.
28
-
34
+
29
- 大本のインスタンスを作った、ボリュームからスナップショットを作成する
35
+ 新しくインスタンスを作成する(これは、複製するインスタンスになる)
36
+
30
-
37
+ (必要分だけ、インスタンスを作成する)
38
+
31
- 3-2
39
+ 3-2.
32
-
33
- そのスナップショットから複製に使うボリュームを作成する。
40
+
34
-
35
- (必要分だけ、作成する
41
+ 全てボリュームをデタッチする
36
42
 
37
43
  4.
38
44
 
39
- 新しくインスタンスを作成する(これは、複製するインスタンスになる)
40
-
41
- (必要分だけ、作成する)
42
-
43
- 全てボリュームをタッチする。
45
+ 作ったボリュームを新しい3.で作ったインスンスにアタッチする。
44
46
 
45
47
  5.
46
48
 
47
- 作ったボリュームを新しい4.で作ったインスタンスにアタッチする。
49
+ 4.でアタッチし後、インスタンスを起動する。
48
50
 
49
51
  6.
50
52
 
51
- 5.でアタッチした後、インスタンスを起動する。
52
-
53
- 7.
54
-
55
53
  新インスタンスに接続する。
56
54
 
57
55
 
58
56
 
59
-
60
-
61
- <追加.1>
62
-
63
57
  AWS cli のコマンドはありますが、.shのファイルにそのまま記載しても、当たり前ではありますが、実行はされず、
64
58
 
65
59
  困っています。
@@ -156,7 +150,7 @@
156
150
 
157
151
  このように表示されてしまいます。
158
152
 
159
- 3-1.解決済)
153
+ (解決済)
160
154
 
161
155
  どうかご教示頂きたいです。
162
156
 
@@ -168,18 +162,8 @@
168
162
 
169
163
 
170
164
 
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
165
  ### *1
180
166
 
181
- (3-2以降)
182
-
183
167
  <new ver>
184
168
 
185
169
  ```

9

文章更新

2019/01/10 06:54

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -390,7 +390,7 @@
390
390
 
391
391
  if [ "${_DEVICE}" = "/dev/sda1" ];then
392
392
 
393
- aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value=${OS1_restore}
393
+ aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value=${_OS1_restore}
394
394
 
395
395
  [ $? -eq 0 ] && _RESULT_1=0 || _RESULT_1=1
396
396
 

8

文章更新

2019/01/09 15:13

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -306,15 +306,99 @@
306
306
 
307
307
  if [ "${_DEVICE}" = "/dev/sda1" ];then
308
308
 
309
+ aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value=${_OS1_restore}
310
+
311
+ [ $? == 0 ] && _RESULT_1=0 || _RESULT_1=1
312
+
313
+ else
314
+
315
+ aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="DATA_restore"
316
+
317
+ [ $? == 0 ] && _RESULT_2=0 || _RESULT_2=1
318
+
319
+ fi
320
+
321
+ done < tmp/snapshot_ids
322
+
323
+ _RESULT=${_RESULT_1}${_RESULT_2}
324
+
325
+ if [ "${_RESULT}" == "00" ];then
326
+
327
+ echo "ボリュームを作成しました. 引続き処理を続行します."
328
+
329
+ else
330
+
331
+ echo "ボリュームの作成に失敗しました."
332
+
333
+ fi
334
+
335
+
336
+
337
+ ```
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+ ### *2
354
+
355
+ ```aws ec2 wait snapshot-completed --snapshot-ids $(cat tmp/snapshot_ids)
356
+
357
+ if [ $? -eq 0 ]; then
358
+
359
+ echo "スナップショットの作成が完了しました."
360
+
361
+ fi
362
+
363
+
364
+
365
+ # tmp/snapshot_ids のチェック
366
+
367
+ if [ ! -f tmp/snapshot_ids ];then
368
+
369
+ echo "tmp/snapshot_ids が存在しません.マネジメントコンソールにて処理を継続して下さい."
370
+
371
+ exit 1
372
+
373
+ fi
374
+
375
+
376
+
377
+ # ボリュームの作成
378
+
379
+ echo "スナップショットからボリュームを作成します..."
380
+
381
+ while read snapshot_id
382
+
383
+ do
384
+
385
+
386
+
387
+ _DEVICE=$(aws ec2 describe-tags --filters "Name=resource-id,Values=${snapshot_id}" "Name=key,Values=device" --query Tags[].Value --output text)
388
+
389
+ _VOLUME_ID=$(aws ec2 create-volume --availability-zone ap-northeast-1a --snapshot-id ${snapshot_id} --volume-type gp2 --query VolumeId --output text)
390
+
391
+ if [ "${_DEVICE}" = "/dev/sda1" ];then
392
+
309
393
  aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value=${OS1_restore}
310
394
 
311
- [ $? == 0 ] && _RESULT_1=0 || _RESULT_1=1
395
+ [ $? -eq 0 ] && _RESULT_1=0 || _RESULT_1=1
312
396
 
313
397
  else
314
398
 
315
399
  aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="DATA_restore"
316
400
 
317
- [ $? == 0 ] && _RESULT_2=0 || _RESULT_2=1
401
+ [ $? -eq 0 ] && _RESULT_2=0 || _RESULT_2=1
318
402
 
319
403
  fi
320
404
 
@@ -322,7 +406,7 @@
322
406
 
323
407
  _RESULT=${_RESULT_1}${_RESULT_2}
324
408
 
325
- if [ "${_RESULT}" == "00" ];then
409
+ if [ "${_RESULT}" = "00" ];then
326
410
 
327
411
  echo "ボリュームを作成しました. 引続き処理を続行します."
328
412
 
@@ -332,88 +416,4 @@
332
416
 
333
417
  fi
334
418
 
335
-
336
-
337
419
  ```
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
- ### *2
354
-
355
- ```aws ec2 wait snapshot-completed --snapshot-ids $(cat tmp/snapshot_ids)
356
-
357
- if [ $? -eq 0 ]; then
358
-
359
- echo "スナップショットの作成が完了しました."
360
-
361
- fi
362
-
363
-
364
-
365
- # tmp/snapshot_ids のチェック
366
-
367
- if [ ! -f tmp/snapshot_ids ];then
368
-
369
- echo "tmp/snapshot_ids が存在しません.マネジメントコンソールにて処理を継続して下さい."
370
-
371
- exit 1
372
-
373
- fi
374
-
375
-
376
-
377
- # ボリュームの作成
378
-
379
- echo "スナップショットからボリュームを作成します..."
380
-
381
- while read snapshot_id
382
-
383
- do
384
-
385
-
386
-
387
- _DEVICE=$(aws ec2 describe-tags --filters "Name=resource-id,Values=${snapshot_id}" "Name=key,Values=device" --query Tags[].Value --output text)
388
-
389
- _VOLUME_ID=$(aws ec2 create-volume --availability-zone ap-northeast-1a --snapshot-id ${snapshot_id} --volume-type gp2 --query VolumeId --output text)
390
-
391
- if [ "${_DEVICE}" = "/dev/sda1" ];then
392
-
393
- aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value=${OS1_restore}
394
-
395
- [ $? -eq 0 ] && _RESULT_1=0 || _RESULT_1=1
396
-
397
- else
398
-
399
- aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="DATA_restore"
400
-
401
- [ $? -eq 0 ] && _RESULT_2=0 || _RESULT_2=1
402
-
403
- fi
404
-
405
- done < tmp/snapshot_ids
406
-
407
- _RESULT=${_RESULT_1}${_RESULT_2}
408
-
409
- if [ "${_RESULT}" = "00" ];then
410
-
411
- echo "ボリュームを作成しました. 引続き処理を続行します."
412
-
413
- else
414
-
415
- echo "ボリュームの作成に失敗しました."
416
-
417
- fi
418
-
419
- ```

7

文章更新

2019/01/09 15:11

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -335,3 +335,85 @@
335
335
 
336
336
 
337
337
  ```
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+ ### *2
354
+
355
+ ```aws ec2 wait snapshot-completed --snapshot-ids $(cat tmp/snapshot_ids)
356
+
357
+ if [ $? -eq 0 ]; then
358
+
359
+ echo "スナップショットの作成が完了しました."
360
+
361
+ fi
362
+
363
+
364
+
365
+ # tmp/snapshot_ids のチェック
366
+
367
+ if [ ! -f tmp/snapshot_ids ];then
368
+
369
+ echo "tmp/snapshot_ids が存在しません.マネジメントコンソールにて処理を継続して下さい."
370
+
371
+ exit 1
372
+
373
+ fi
374
+
375
+
376
+
377
+ # ボリュームの作成
378
+
379
+ echo "スナップショットからボリュームを作成します..."
380
+
381
+ while read snapshot_id
382
+
383
+ do
384
+
385
+
386
+
387
+ _DEVICE=$(aws ec2 describe-tags --filters "Name=resource-id,Values=${snapshot_id}" "Name=key,Values=device" --query Tags[].Value --output text)
388
+
389
+ _VOLUME_ID=$(aws ec2 create-volume --availability-zone ap-northeast-1a --snapshot-id ${snapshot_id} --volume-type gp2 --query VolumeId --output text)
390
+
391
+ if [ "${_DEVICE}" = "/dev/sda1" ];then
392
+
393
+ aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value=${OS1_restore}
394
+
395
+ [ $? -eq 0 ] && _RESULT_1=0 || _RESULT_1=1
396
+
397
+ else
398
+
399
+ aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="DATA_restore"
400
+
401
+ [ $? -eq 0 ] && _RESULT_2=0 || _RESULT_2=1
402
+
403
+ fi
404
+
405
+ done < tmp/snapshot_ids
406
+
407
+ _RESULT=${_RESULT_1}${_RESULT_2}
408
+
409
+ if [ "${_RESULT}" = "00" ];then
410
+
411
+ echo "ボリュームを作成しました. 引続き処理を続行します."
412
+
413
+ else
414
+
415
+ echo "ボリュームの作成に失敗しました."
416
+
417
+ fi
418
+
419
+ ```

6

文章の追加

2019/01/09 14:59

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -1,19 +1,15 @@
1
1
  宜しくお願いします。
2
2
 
3
-
4
-
5
3
  現在、AWSのEC2でwindows server 2016を作成しております。
6
4
 
7
-
8
-
9
5
  複数台作成はできるのですが、そのscriptについて教えていただきたいです。
10
6
 
11
-
12
-
13
7
  aws cliでは、コマンドを作れるのですが、スクリプトで書けず、止まっています。
14
8
 
15
9
 
16
10
 
11
+ ~やりたい内容~
12
+
17
13
  1.
18
14
 
19
15
  インスタンス作成(original)
@@ -60,6 +56,8 @@
60
56
 
61
57
 
62
58
 
59
+
60
+
63
61
  <追加.1>
64
62
 
65
63
  AWS cli のコマンドはありますが、.shのファイルにそのまま記載しても、当たり前ではありますが、実行はされず、

5

文章追加

2019/01/09 13:16

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -158,16 +158,182 @@
158
158
 
159
159
  このように表示されてしまいます。
160
160
 
161
-
161
+ (3-1.解決済)
162
162
 
163
163
  どうかご教示頂きたいです。
164
164
 
165
165
  宜しくお願いします。
166
166
 
167
-
168
-
169
-
170
-
171
167
  以下、参考にしているURLになります。
172
168
 
173
169
  [リンク内容](https://cloudpack.media/28537)
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+ ### *1
182
+
183
+ (3-2以降)
184
+
185
+ <new ver>
186
+
187
+ ```
188
+
189
+ #!/usr/bin/env bash
190
+
191
+
192
+
193
+ _INSTANCE_ID="i-xxxxxxxxxxxx"
194
+
195
+ _ROOT_DEVICE="/dev/sda1"
196
+
197
+ _CHECK_INTERVAL="60"
198
+
199
+ _OS1="OS1"
200
+
201
+ _OS1_restore="OS1_restore"
202
+
203
+
204
+
205
+ echo "処理を続行しますか? [Y/n]"
206
+
207
+ read ANSWER
208
+
209
+ case ${ANSWER} in
210
+
211
+ [yY]) echo "処理を続行します..."
212
+
213
+ ;;
214
+
215
+ [nN]) echo "$(date '+%Y-%m-%d %H:%M:%S') 処理を終了します..."
216
+
217
+ exit 0 ;;
218
+
219
+ *) echo "$(date '+%Y-%m-%d %H:%M:%S') 処理を終了します..."
220
+
221
+ exit 0 ;;
222
+
223
+ esac
224
+
225
+
226
+
227
+ if [ -f tmp/snapshot_ids ];then
228
+
229
+ rm tmp/snapshot_ids
230
+
231
+ fi
232
+
233
+
234
+
235
+ for ebs in $(aws ec2 describe-instances --filters Name=instance-id,Values=${_INSTANCE_ID} --query 'Reservations[].Instances[].BlockDeviceMappings[].Ebs.VolumeId' --output text);
236
+
237
+ do
238
+
239
+ _SNAPSHOT_ID=$(aws ec2 create-snapshot --volume-id ${ebs} | jq -r .SnapshotId)
240
+
241
+ echo ${_SNAPSHOT_ID} >> tmp/snapshot_ids
242
+
243
+ _DEVICE=$(aws ec2 describe-volumes --volume-ids ${ebs} --query Volumes[].Attachments[].Device --output text)
244
+
245
+ if [ "${_DEVICE}" = "${_ROOT_DEVICE}" ];then
246
+
247
+ aws ec2 create-tags --resources ${_SNAPSHOT_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value=${_OS1}
248
+
249
+ else
250
+
251
+ aws ec2 create-tags --resources ${_SNAPSHOT_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="DATA"
252
+
253
+ fi
254
+
255
+ done
256
+
257
+
258
+
259
+ #if [ -f tmp/snapshot_ids -a $(cat tmp/snapshot_ids | wc -l) = "2" ];then
260
+
261
+ # echo ${_SLACK_CHANNEL} "スナップショットの作成を開始しました..."
262
+
263
+ #else
264
+
265
+ # echo ${_SLACK_CHANNEL} "スナップショットの作成に失敗しました.処理を中断します."
266
+
267
+ # exit 1
268
+
269
+ #fi
270
+
271
+
272
+
273
+ aws ec2 wait snapshot-completed --snapshot-ids $(cat tmp/snapshot_ids)
274
+
275
+ if [ $? == 0 ]; then
276
+
277
+ echo "スナップショットの作成が完了しました."
278
+
279
+ fi
280
+
281
+
282
+
283
+ # tmp/snapshot_ids のチェック
284
+
285
+ if [ ! -f tmp/snapshot_ids ];then
286
+
287
+ echo "tmp/snapshot_ids が存在しません.マネジメントコンソールにて処理を継続して下さい."
288
+
289
+ exit 1
290
+
291
+ fi
292
+
293
+
294
+
295
+ # ボリュームの作成
296
+
297
+ echo "スナップショットからボリュームを作成します..."
298
+
299
+ while read snapshot_id
300
+
301
+ do
302
+
303
+
304
+
305
+ _DEVICE=$(aws ec2 describe-tags --filters "Name=resource-id,Values=${snapshot_id}" "Name=key,Values=device" --query Tags[].Value --output text)
306
+
307
+ _VOLUME_ID=$(aws ec2 create-volume --availability-zone ap-northeast-1a --snapshot-id ${snapshot_id} --volume-type gp2 --query VolumeId --output text)
308
+
309
+ if [ "${_DEVICE}" = "/dev/sda1" ];then
310
+
311
+ aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value=${OS1_restore}
312
+
313
+ [ $? == 0 ] && _RESULT_1=0 || _RESULT_1=1
314
+
315
+ else
316
+
317
+ aws ec2 create-tags --resources ${_VOLUME_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="DATA_restore"
318
+
319
+ [ $? == 0 ] && _RESULT_2=0 || _RESULT_2=1
320
+
321
+ fi
322
+
323
+ done < tmp/snapshot_ids
324
+
325
+ _RESULT=${_RESULT_1}${_RESULT_2}
326
+
327
+ if [ "${_RESULT}" == "00" ];then
328
+
329
+ echo "ボリュームを作成しました. 引続き処理を続行します."
330
+
331
+ else
332
+
333
+ echo "ボリュームの作成に失敗しました."
334
+
335
+ fi
336
+
337
+
338
+
339
+ ```

4

文章の追加

2019/01/09 13:10

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -72,7 +72,91 @@
72
72
 
73
73
 
74
74
 
75
+ ```ここに言語を入力
75
76
 
77
+ #!/usr/bin/env bash
78
+
79
+
80
+
81
+ _INSTANCE_ID="${i-xxxxxxxxxxx}"
82
+
83
+ _ROOT_DEVICE="/dev/sda1"
84
+
85
+ _CHECK_INTERVAL=60
86
+
87
+
88
+
89
+ echo "処理を続行しますか? [Y/n]"
90
+
91
+ read ANSWER
92
+
93
+ case ${ANSWER} in
94
+
95
+ [yY]) echo "処理を続行します..."
96
+
97
+ ;;
98
+
99
+ [nN]) echo "$(date '+%Y-%m-%d %H:%M:%S') 処理を終了します..."
100
+
101
+ exit 0 ;;
102
+
103
+ *) echo "$(date '+%Y-%m-%d %H:%M:%S') 処理を終了します..."
104
+
105
+ exit 0 ;;
106
+
107
+ esac
108
+
109
+
110
+
111
+ if [ -f tmp/snapshot_ids ];then
112
+
113
+ rm tmp/snapshot_ids
114
+
115
+ fi
116
+
117
+
118
+
119
+ for ebs in $(aws ec2 describe-instances --filters Name=instance-id,Values=${_INSTANCE_ID} --query 'Reservations[].Instances[].BlockDeviceMappings[].Ebs.VolumeId' --output text);
120
+
121
+ do
122
+
123
+ _SNAPSHOT_ID=$(aws ec2 create-snapshot --volume-id ${ebs} | jq -r .SnapshotId)
124
+
125
+ echo ${_SNAPSHOT_ID} >> tmp/snapshot_ids
126
+
127
+ _DEVICE=$(aws ec2 describe-volumes --volume-ids ${ebs} --query Volumes[].Attachments[].Device --output text)
128
+
129
+ if [ "${_DEVICE}" == "${_ROOT_DEVICE}" ];then
130
+
131
+ aws ec2 create-tags --resources ${_SNAPSHOT_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="OS"
132
+
133
+ else
134
+
135
+ aws ec2 create-tags --resources ${_SNAPSHOT_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="DATA"
136
+
137
+ fi
138
+
139
+ done
140
+
141
+
142
+
143
+ if [ `-f tmp/snapshot_ids -a $(wc -l tmp/snapshot_ids | awk '{print $1}') == 2` ];then
144
+
145
+
146
+
147
+
148
+
149
+ ```
150
+
151
+ ↑ここの行で
152
+
153
+ wc: tmp/snapshot_ids: そのようなファイルやディレクトリはありません
154
+
155
+ ./snapshot.sh: 行 34: -f: コマンドが見つかりません
156
+
157
+ スナップショットの作成に失敗しました.処理を中断します.
158
+
159
+ このように表示されてしまいます。
76
160
 
77
161
 
78
162
 
@@ -84,8 +168,6 @@
84
168
 
85
169
 
86
170
 
87
-
88
-
89
171
  以下、参考にしているURLになります。
90
172
 
91
173
  [リンク内容](https://cloudpack.media/28537)

3

文章の追加

2019/01/08 17:39

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -60,6 +60,20 @@
60
60
 
61
61
 
62
62
 
63
+ <追加.1>
64
+
65
+ AWS cli のコマンドはありますが、.shのファイルにそのまま記載しても、当たり前ではありますが、実行はされず、
66
+
67
+ 困っています。
68
+
69
+ 纏めて、1つのファイル実行で、複数(~10数台)を起動させたい。
70
+
71
+ cronなどは使わず。
72
+
73
+
74
+
75
+
76
+
63
77
 
64
78
 
65
79
  どうかご教示頂きたいです。

2

文章変更

2019/01/08 11:17

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
File without changes

1

文章変更

2019/01/08 11:12

投稿

momo2
momo2

スコア21

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- 現在、AWSのEC2でwin serverを作成しております。
5
+ 現在、AWSのEC2でwindows server 2016を作成しております。
6
6
 
7
7
 
8
8