Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to take a snapshot

I've asked this question before, but I am still confused. What's the correct and quickest way to take a snapshot (I only use EBS-backed Unix and Windows machines, so that's all my interest right now). Some ideas:

  • Just take the snapshot... This seems to sometimes cause system corruption.
  • Stop the machine, take the snapshot and then start the machine. I guess this also means I need to wait for each individual task to complete, making scirpting a bit of a challenge?
  • Take a snapshot with the 'reboot machine' flag set. There is very little in the documentation to specify why a reboot is needed...

Hope you EC2 experts can help me.

like image 415
Snapshot Avatar asked Oct 18 '10 21:10

Snapshot


2 Answers

If a bit of data loss is acceptable, just take the snapshot while the instance is running. If it's not acceptable, write some script magic to ensure everything your application is working on is saved to disk, take the snapshot, then let your app resume work.

For what I do, I find it best to keep a separate EBS volume with my application and its data on it, and when I need a snapshot, I just stop the app for a moment, snapshot, and fire it back up. That way you don't have to worry about what the OS is doing during the snapshot, and it also comes with the added bonuses of being able to quickly move your app and its data to more powerful hardware, and having much smaller snapshots.

like image 105
James Avatar answered Oct 06 '22 15:10

James


Quick answer - both operating systems have features for safely unmounting the drive. An unmounted drive can be snapshotted without fear of corruption.

Long answer An EBS snapshot is point-in-time and differential (it does not "copy" your data per-se), so as long as your drive is in a consistent and restorable state when it begins, you'll avoid the corruption (since the snapshot is atomic).

As you have implied, whatever state the whole drive is in when it starts, that's what your snapshot image will be when it is restored (if you snapshot while you are half-way done writing a file, then, by-golly, that file will be half-written when you restore).

For both Linux and Windows, a consistent state can be this can be acheived by unmounting the drive. This will guarantee that your buffers are flushed to disk and no writes can occur. In both linux and windows there are commands to list which processes are using the drive; once you have stopped those processes or otherwise gotten them to stop marking the drive for use (different for each program/service), you can unmount. In windows, this is very easy by setting your drive as a "removable drive" and then using the "safely remove hardware" feature to unmount. In linux, you can unmount with the "umount" command.

There are other sneakier ways, but the above is pretty universal.

So assuming you get to a restorable state before you begin, you can resume using your drive as soon as the snapshot starts (you do not need to wait for the snapshot completes before you unlock (or remount) and resume use). At that point you can remount the volume.

How AWS snapshot works:

Your volume and the snapshot is just a set of pointers, when you take a snapshot, you just diverge any blocks that you write to from that point forward; they are effectively new blocks associated with the volume, and the old blocks at that logical place in the volume are left alone so that the snapshot stays the same logically.

This is also why subsequent snapshots will tend to go faster (they are differential).

http://harish11g.blogspot.com/2013/04/understanding-Amazon-Elastic-block-store-EBS-snapshots.html

like image 2
Christopher McGowan Avatar answered Oct 06 '22 15:10

Christopher McGowan