Scheduling Miniserver Snapshots

Our Miniserver Snapshots allow you to save a copy of your server's disk at a particular point in time to Memstore. That snapshot can be downloaded or used to re-image any of your Miniservers, and it's a really useful tool for any operations department. Think of the stress-free deployments when you can take a snapshot of your production server, use it to re-image a temporal Miniserver, perform the operations in the cloned box, and finally put it live when everything is done.

Taking a snapshot is simple using your control panel, but it can be automated thanks to the server.snapshot method provided by Memset's API.

Firstly you need to create an API key using the "API Keys Manager" link in your account overview page. In this example we're going to limit the API key scope to method:server.snapshot and method:job.status.

The RESTful Interface

The easiest way to access the API is with the RESTful interface. In the following example we're going to use  cURL to perform the request:

$ curl --user 4c2c8e0e3d784ad3af18aa25582e059a:x \

Note that I'm going to split the commands with backslashes to make them look better in the blog post, but you don't need to if you don't want to. In the crontab example, don't do it because it won't work.

In this case we're going to store the resulting snapshot in mymemstore1, using tar image type (it's a Linux server, other image formats are supported including ntfsclone images for Windows servers).

With that simple line we can take a snapshot whenever we want, and it's very easy to schedule a one time only snapshot using the at UNIX command. For example, to take the snapshot tomorrow at 3am:

$ at 3am + 1 days
warning: commands will be executed using /bin/sh
at>  curl --user 4c2c8e0e3d784ad3af18aa25582e059a:x \
"" \
| mail -s "Snapshot result"
at> <EOT>
job 1 at Thu May 10 03:00:00 2012

By default at expects you to enter the command to be run, and you can finish the input using CTRL + d (please refer to at manual page for further details). In this example we want the results to be sent to

Taking a snapshot is a time consuming operation that depends on the disk size, the image type and the network status, and it can use a large amount of space in our Memstore instance. Because of that we probably don't want to take snapshots too frequently, but we may still want to take periodic snapshots.

This is easy to accomplish using the cron UNIX command. For example, to take a snapshot every Monday at 3am we can add a cron job such as the following:

# DON'T SPLIT the following line in your crontab, it won't work!
0 3 * * 1 curl --user 4c2c8e0e3d784ad3af18aa25582e059a:x \
"" \
| mail -s "Snapshot result"

The XML-RPC Interface

Let's write a small script using Python (other languages are supported) that will take a snapshot and also tell us when it is done thanks to the job.status method (remember that we added it to the API key scope).

All you need installed is a recent version of  Python (2.7 is highly recommended). We are going to take the snapshot immediately, and using the job ID returned by server.snapshot, we will monitor the progress:

Take a snapshot and wait for it to be finished.

KEY = "4c2c8e0e3d784ad3af18aa25582e059a"
URI = "" % KEY

from xmlrpclib import ServerProxy
from time import sleep

def main():
    s = ServerProxy(URI)
    r = s.server.snapshot(dict(name='myserver1', storage_name='mymemstore1', image_type='tar'))

    print "Snapshot scheduled:", r

    while not r['finished']:
        r = s.job.status(dict(id=r['id']))
        print r
    print " snapshot DONE!"

if __name__ == "__main__":

As you can see, the script is very simple and easy to understand. You only need to use your API key in the KEY variable and set your server and Memstore details in the server.snapshot call.

The possibilities are endless. I recommend you have a look at Memset's API documentation.

Posted May 2012 by Juan J. Martínez , in Dev

blog comments powered by Disqus