Wednesday, May 27, 2015

AWS EC2 API tools: Create snapshot & Check Data in snapshot

After installed AWS EC2 API tools,  It's time for example create/delete snapshot. 
- Creating snapshot.
ubuntu@ip-x-x-x-x~$ ec2-describe-volumes
VOLUME  vol-41885f55    8       snap-d00ac9e4   ap-southeast-1a in-use  2015-05-26T09:07:04+0000        gp2     24
ATTACHMENT      vol-41885f55    i-d6cdb71a      /dev/sda1       attached        2015-05-26T09:07:04+0000        true
ubuntu@ip-x-x-x-x:~$ ec2-create-snapshot  -d vol-41885f55-$(date +%Y%m%d%H%M) vol-41885f55
SNAPSHOT        snap-b20a8c87   vol-41885f55    pending 2015-05-27T05:46:58+0000                843870022970    8       vol-41885f55-201505270546
ubuntu@ip-x-x-x-x:~$ ec2-describe-snapshots
SNAPSHOT        snap-b20a8c87   vol-41885f55    pending 2015-05-27T05:46:58+0000        0%      843870022970    8       vol-41885f55-201505270546
ubuntu@ip-x-x-x-x:~$ ec2-create-snapshot  -d vol-41885f55-$(date +%Y%m%d%H%M) vol-41885f55
SNAPSHOT        snap-bea0d28b   vol-41885f55    pending 2015-05-27T05:50:11+0000                843870022970    8       vol-41885f55-201505270550
ubuntu@ip-x-x-x-x:~$ ec2-describe-snapshots
SNAPSHOT        snap-b20a8c87   vol-41885f55    completed       2015-05-27T05:46:58+0000        100%    843870022970    8       vol-41885f55-201505270546
SNAPSHOT        snap-bea0d28b   vol-41885f55    completed       2015-05-27T05:50:11+0000        100%    843870022970    8       vol-41885f55-201505270550
- Deleting snapshot (delete snap-b20a8c87). 
ubuntu@ip-x-x-x-x:~$ ec2-describe-snapshots  |head -1| awk '{print $2}'|xargs ec2-delete-snapshot
SNAPSHOT        snap-b20a8c87
ubuntu@ip-x-x-x-x:~$ ec2-describe-snapshots
SNAPSHOT        snap-bea0d28b   vol-41885f55    completed       2015-05-27T05:50:11+0000        100%    843870022970    8       vol-41885f55-201505270550
How to check data in "snap-bea0d28b"? Checking idea on AWS, look like we must create Volume from snapshot and attach it to Instance.
- Creating Volume > Attach to Instance and Mount.
ubuntu@ip-x-x-x-x:~$ ec2-describe-volumes
VOLUME  vol-41885f55    8       snap-d00ac9e4   ap-southeast-1a in-use  2015-05-26T09:07:04+0000        gp2     24
ATTACHMENT      vol-41885f55    i-d6cdb71a      /dev/sda1       attached        2015-05-26T09:07:04+0000        true
ubuntu@ip-x-x-x-x:~$ ec2-describe-availability-zones
AVAILABILITYZONE        ap-southeast-1a available       ap-southeast-1
AVAILABILITYZONE        ap-southeast-1b available       ap-southeast-1
ubuntu@ip-x-x-x-x:~$ ec2-create-volume -s 8 --snapshot snap-bea0d28b -z ap-southeast-1a
VOLUME  vol-d15087c5    8       snap-bea0d28b   ap-southeast-1a creating        2015-05-27T06:24:00+0000        standard
ubuntu@ip-x-x-x-x:~$ ec2-describe-volumes
VOLUME  vol-41885f55    8       snap-d00ac9e4   ap-southeast-1a in-use  2015-05-26T09:07:04+0000        gp2     24
ATTACHMENT      vol-41885f55    i-d6cdb71a      /dev/sda1       attached        2015-05-26T09:07:04+0000        true
VOLUME  vol-d15087c5    8       snap-bea0d28b   ap-southeast-1a available       2015-05-27T06:24:00+0000        standard
ubuntu@ip-x-x-x-x:~$ sudo fdisk -l
Disk /dev/xvda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders, total 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *       16065    16771859     8377897+  83  Linux
ubuntu@ip-x-x-x-x:~$ ec2-attach-volume vol-d15087c5 -i  i-d6cdb71a  -d sdf
ATTACHMENT      vol-d15087c5    i-d6cdb71a      sdf     attaching       2015-05-27T06:31:16+0000
ubuntu@ip-x-x-x-x:~$ sudo fdisk -l
Disk /dev/xvda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders, total 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *       16065    16771859     8377897+  83  Linux
Disk /dev/xvdf: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders, total 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
    Device Boot      Start         End      Blocks   Id  System
/dev/xvdf1   *       16065    16771859     8377897+  83  Linux
ubuntu@ip-x-x-x-x:~$

ubuntu@ip-x-x-x-x:~$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/xvda1       8115168 1212140   6467752  16% /
none                   4       0         4   0% /sys/fs/cgroup
udev              503188      12    503176   1% /dev
tmpfs             101632     332    101300   1% /run
none                5120       0      5120   0% /run/lock
none              508144       0    508144   0% /run/shm
none              102400       0    102400   0% /run/user
ubuntu@ip-x-x-x-x:~$ sudo mount /dev/xvdf1 /mnt/
ubuntu@ip-x-x-x-x:~$ ls -l /mnt/
total 92
drwxr-xr-x   2 root root  4096 May 26 09:35 bin
drwxr-xr-x   3 root root  4096 Mar 25 11:52 boot
drwxr-xr-x   5 root root  4096 Mar 25 11:53 dev
drwxr-xr-x 105 root root  4096 May 26 09:35 etc
drwxr-xr-x   3 root root  4096 May 26 09:07 home
lrwxrwxrwx   1 root root    33 Mar 25 11:51 initrd.img -> boot/initrd.img-3.13.0-48-generic
drwxr-xr-x  21 root root  4096 May 26 09:35 lib
drwxr-xr-x   2 root root  4096 Mar 25 11:50 lib64
drwx------   2 root root 16384 Mar 25 11:53 lost+found
drwxr-xr-x   2 root root  4096 Mar 25 11:50 media
drwxr-xr-x   2 root root  4096 Apr 10  2014 mnt
drwxr-xr-x   2 root root  4096 Mar 25 11:50 opt
drwxr-xr-x   2 root root  4096 Apr 10  2014 proc
drwx------   3 root root  4096 May 26 09:07 root
drwxr-xr-x   3 root root  4096 Mar 25 11:53 run
drwxr-xr-x   2 root root  4096 May 26 09:35 sbin
drwxr-xr-x   2 root root  4096 Mar 25 11:50 srv
drwxr-xr-x   2 root root  4096 Mar 13  2014 sys
drwxrwxrwt   6 root root  4096 May 27 05:38 tmp
drwxr-xr-x  10 root root  4096 Mar 25 11:50 usr
drwxr-xr-x  12 root root  4096 Mar 25 11:52 var
lrwxrwxrwx   1 root root    30 Mar 25 11:51 vmlinuz -> boot/vmlinuz-3.13.0-48-generic
ubuntu@ip-x-x-x-x:~$ ls /mnt/
bin  boot  dev  etc  home  initrd.img  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  vmlinuz
ubuntu@ip-x-x-x-x:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      7.8G  1.2G  6.2G  16% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            492M   12K  492M   1% /dev
tmpfs           100M  332K   99M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            497M     0  497M   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/xvdf1      7.8G  1.2G  6.2G  16% /mnt
- After checking data, We can unmount and remove it.
ubuntu@ip-x-x-x-x:~$ sudo umount /mnt
ubuntu@ip-x-x-x-x:~$
ubuntu@ip-x-x-x-x:~$
ubuntu@ip-x-x-x-x:~$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/xvda1       8115168 1212140   6467752  16% /
none                   4       0         4   0% /sys/fs/cgroup
udev              503188      12    503176   1% /dev
tmpfs             101632     332    101300   1% /run
none                5120       0      5120   0% /run/lock
none              508144       0    508144   0% /run/shm
none              102400       0    102400   0% /run/user

ubuntu@ip-x-x-x-x:~$ sudo fdisk -l
Disk /dev/xvda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders, total 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *       16065    16771859     8377897+  83  Linux
Disk /dev/xvdf: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders, total 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
    Device Boot      Start         End      Blocks   Id  System
/dev/xvdf1   *       16065    16771859     8377897+  83  Linux
ubuntu@ip-x-x-x-x:~$
ubuntu@ip-x-x-x-x:~$ ec2-describe-volumes
VOLUME  vol-41885f55    8       snap-d00ac9e4   ap-southeast-1a in-use  2015-05-26T09:07:04+0000        gp2     24
ATTACHMENT      vol-41885f55    i-d6cdb71a      /dev/sda1       attached        2015-05-26T09:07:04+0000        true
VOLUME  vol-d15087c5    8       snap-bea0d28b   ap-southeast-1a in-use  2015-05-27T06:24:00+0000        standard
ATTACHMENT      vol-d15087c5    i-d6cdb71a      sdf     attached        2015-05-27T06:31:16+0000        false

ubuntu@ip-x-x-x-x:~$ ec2-detach-volume vol-d15087c5 -i  i-d6cdb71a
ATTACHMENT      vol-d15087c5    i-d6cdb71a      sdf     detaching       2015-05-27T06:31:16+0000
ubuntu@ip-x-x-x-x:~$ ec2-describe-volumes
VOLUME  vol-41885f55    8       snap-d00ac9e4   ap-southeast-1a in-use  2015-05-26T09:07:04+0000        gp2     24
ATTACHMENT      vol-41885f55    i-d6cdb71a      /dev/sda1       attached        2015-05-26T09:07:04+0000        true
VOLUME  vol-d15087c5    8       snap-bea0d28b   ap-southeast-1a in-use  2015-05-27T06:24:00+0000        standard
ATTACHMENT      vol-d15087c5    i-d6cdb71a      sdf     detaching       2015-05-27T06:31:16+0000        false
ubuntu@ip-x-x-x-x:~$ ec2-describe-volumes
VOLUME  vol-41885f55    8       snap-d00ac9e4   ap-southeast-1a in-use  2015-05-26T09:07:04+0000        gp2     24
ATTACHMENT      vol-41885f55    i-d6cdb71a      /dev/sda1       attached        2015-05-26T09:07:04+0000        true
VOLUME  vol-d15087c5    8       snap-bea0d28b   ap-southeast-1a available       2015-05-27T06:24:00+0000        standard
ubuntu@ip-x-x-x-x:~$ ec2-delete-volume vol-d15087c5
VOLUME  vol-d15087c5
ubuntu@ip-x-x-x-x:~$ ec2-describe-volumes
VOLUME  vol-41885f55    8       snap-d00ac9e4   ap-southeast-1a in-use  2015-05-26T09:07:04+0000        gp2     24
ATTACHMENT      vol-41885f55    i-d6cdb71a      /dev/sda1       attached        2015-05-26T09:07:04+0000        true
Look like it's easy to use and adapt with script.

2 comments:

Anonymous said...

Hey yo! I'm sunmi..
I know AWS oh.. Your post is great!

Unknown said...

Awesome !!!!