Skip to main content
Elliot posted in: Developer

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 \
"https://api.memset.com/v1/json/server.snapshot/myserver1/?storage_name=mymemstore1&image_type=tar"

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 \
"https://api.memset.com/v1/json/server.snapshot/myserver1/?storage_name=msmemseag1&image_type=tar" \
| mail sysadm@mydomain.com -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 sysadm@mydomain.com.

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 \
"https://api.memset.com/v1/json/server.snapshot/myserver1/?storage_name=msmemseag1&image_type=tar" \
| mail sysadm@mydomain.com -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:

#!/usr/bin/python
"""
Take a snapshot and wait for it to be finished.
"""

KEY = "4c2c8e0e3d784ad3af18aa25582e059a"
URI = "https://%s:@api.memset.com/v1/xmlrpc" % 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']:
        sleep(60)
        r = s.job.status(dict(id=r['id']))
        print r
    print " snapshot DONE!"

if __name__ == "__main__":
    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.