We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How to Migrate DigitalOcean Droplets Using Snapshots

UpdatedOctober 6, 2017 52.1k views DigitalOcean

Introduction

DigitalOcean snapshots copy a full image of a Droplet on demand at a specific point in time and store the image in your DigitalOcean account. You can use a snapshot as the base image for new Droplets.

Snapshots are useful as manual backups and can be especially helpful when migrating Droplets to new regions. They cost $0.05 per gigabyte per month. The size is based on the actual amount of space used within the file system, not the size of the Droplet.

In this tutorial, we will create a snapshot and use it to create a new Droplet. We'll finish by removing both the original Droplet and the snapshot.

Prerequisites

To follow along with this tutorial, you will need:

  • One DigitalOcean Droplet of any size or configuration.

Step 1 — Creating a Snapshot

From the DigitalOcean Control Panel's Droplets tab, locate the Droplet that you plan to snapshot. In this example, we'll take a snapshot of a Droplet called source-droplet. To start, we'll click its name in the Droplets list.

Screenshot of Droplet page

This leads us to the Droplet's detail page. From there, we'll select Snapshots from the left-hand navigation to open the Take snapshot page.

Screenshot of the Snapshots page with the menu item highlighted

Before we create the snapshot, we'll need to give it a name and choose what type of snapshot we want.

The name for the snapshot is prefilled with the original Droplet's name followed by the Unix timestamp at the time we clicked the button. This scheme identifies the Droplet and indicates approximately when the snapshot was taken. The name can be customized now or at any time after the snapshot is created. In this example, we'll follow the existing naming convention and leave it as is.

In addition to setting the name, we have a choice between taking a live snapshot or powering down the Droplet first to ensure data consistency. The correct choice depends on the kinds of the services running on the Droplet as well as the individual use case. If the Droplet is hosting a read-only website, a live snapshot can work well. If it's hosting a database or recording important logs, cleanly powering it down ensures all data is saved consistently.

In this example, we'll power down our Droplet before taking the Snapshot, but instructions for both methods follow.

Option 1 — Power Down First (Preferred)

To ensure data integrity, it's preferable to power down the Droplet before taking a snapshot.

Warning: It's possible to power down a Droplet toggling the On button to Off in the top right corner or by clicking the Power down link. However, both of these choices are equivalent to a hard reset, which means we might lose data stored in RAM or cause database corruption. Shutting down from the command line is the best way to ensure a quality snapshot.

To shut down the Droplet from the command line, use SSH to connect to the Droplet as an administrative user (either root or a user with sudo privileges).

Open a terminal on your local computer and use the command below to connect. Be sure to substitute the appropriate username and the IP address or domain name for your Droplet.

sudo ssh admin@your_server_ip_or_domain

Once we're logged in and at the command prompt, we'll issue the shutdown command, adding the -h flag to power down cleanly.

  • shutdown -h now

Next, we'll return to the web browser and reload the page to verify the Droplet is off. If it is, the text of the button next to the snapshot name field will be Take Snapshot. We'll click that button to take the snapshot.

Screenshot of the Take Snapshot button which displays when the Droplet is powered off

The progress bar will track the snapshot's creation. Once the snapshot is ready, it will appear in the Droplet snapshots table.

Screenshot of the Droplet snapshots table

Option 2 — Take a Live Snapshot

To take a live snapshot, click the Take Live Snapshot button, which will be visible when the Droplet is running. Depending on the Droplet's size and the amount of data on it, creating a Snapshot may take several minutes. The progress bar will track the snapshot's creation.

Warning: Live snapshots can capture certain datasets while in an inconsistent state. Databases are especially prone to this and should be stopped or synced to disk before taking a snapshot.

Regardless of how we create our snapshot, once the snapshot has been created, it will appear in the Droplet snapshots table on the Take snapshot page. It will also be available under Images in the main navigation on the Snapshots tab.

Step 2 — Adding the Snapshot to New Region (Optional)

If you plan to use the new snapshot to create a Droplet in the same region, you can skip ahead to Step 3 and create the new Droplet now. However, if you want to create the new Droplet in a different region, you'll need to add a copy of the snapshot to that region.

To add the snapshot to a new region so it can be used to create new Droplets there, open the snapshot's More menu, select Add to region, then select the region.

Screenshot of the Snapshot availability choices from the More menu

A spinner will display as the snapshot image is added, which may take several minutes.

When the move is complete, the spinner will stop and the region will be greyed out. This indicates that the snapshot is now available in that region (i.e. it doesn't need to be transferred there). You can return to the More menu options by clicking the left arrow.

Note: There is no additional charge for making a snapshot available in multiple regions.

Once the snapshot is added to the new region, you can create a new Droplet based on it.

Step 3 — Creating a New Droplet from a Snapshot

To create a new Droplet based on our snapshot, we'll select Create Droplet on the Snapshot's More menu.

Screenshot of the Snapshot's More menu

This will take us to the Droplet creation page.

Note: We could also reach this page from the main Create menu by choosing Droplet, then choosing the Snapshots tab under the Choose an image header.

The choices when creating a Droplet from a snapshot are slightly different than creating a Droplet from one of the provided Linux base images on the default Distributions tab. These differences include:

  • Choose a size: The size defaults to the size of the original Droplet. You can increase this size but can't decrease it, as shrinking the file system layout can lead to data loss.

  • Add block storage: If the region where you're creating the new Droplet has block storage available, you can create a new volume now or attach an existing one.

Warning: If you're moving a Droplet to ensure block storage compatibility in NYC3, be sure to add block storage now.

  • Choose a datacenter region: The choice of regions is limited to those where you've added the snapshot. To use the snapshot in a region not currently available, follow Step Two to add the Droplet to a new region.

  • Select additional options: With the exception of Monitoring, these additional options are independent of the options that were enabled on the source Droplet, so you should select the ones you need.

If the monitoring agent was installed on the original Droplet, the new Droplet will start collecting data and showing extended graphs automatically after a few minutes, regardless of whether or not Monitoring is selected now. Alert policies may or may not continue to apply depending on how they were configured.

  • Add your SSH keys: SSH keys will be transferred along with all the user accounts from the original Droplet at the time the snapshot was created.

Note that if you used SSH keys for the root user on the original Droplet and allowed root logins, you may want to add the same key or a new key now. Otherwise, you'll be e-mailed a root password. You won't be able to log in without a valid authorized key, but when you connect as root, you'll need to reset root's password in order to continue logging in as before.

Once you've selected the options you want, including quantity of Droplets and their names, click the green Create button at the bottom of the page.

Once the new Droplet is created, we will make sure it works.

Step 4 — Testing the New Droplet

The data and organization of the file system on the new Droplet should be identical to the original. However, it's a good idea to test the services on the new instance because, for example, it's possible that services you expect to be running weren't set to start at boot. You may also want to ask stakeholders to verify that the services on the new Droplet are working as expected.

After any migration, it can be worth the cost to retain the original Droplet and snapshots, run automated and manual tests, and seek stakeholder verification. This way, if something goes wrong in the new location, these backups can be used to determine if the problems existed before the move.

Once you've verified that the system works as expected, you can take steps to move it into production.

For example, if you had a domain name directed to the original Droplet, you'll need to update the DNS record to point to the new IP address. Exactly how to do this depends on where you manage your DNS. For help making the update on DigitalOcean, see How to Set Up a Host Name on Digitalocean.

Step 5 — Deleting the Original Droplet and Snapshot

When you're ready, you can delete the interim snapshot and the original Droplet.

Delete the Snapshot

We'll locate the snapshot by visiting the original Droplet's Snapshots page. From there, we'll open the snapshot's More menu and select Delete.

Screenshot of the open More menu

We'll be prompted to confirm the deletion. To do so, we'll click the red Delete Snapshot button.

Snapshots can also be deleted from the Images tab, where all of the snapshots for your account are displayed. This is helpful when the Droplet that the snapshots were created from has already been deleted.

Destroy the Original Droplet

To destroy the original Droplet, we'll open the Destroy page from the side menu, then click the red Destroy button.

Screenshot with Destroy button highlighted

We'll be prompted to confirm the deletion. To do so, we'll click the red Confirm button.

Note: Snapshots are independent from the Droplets used to create them. If you delete a Droplet without removing a snapshot based on it, that snapshot will still be accessible on the Images page in the Snapshots tab, and you will still be billed for it.

Once the Droplet is deleted, we've completed our migration.

Conclusion

In this tutorial, we created a snapshot from an existing Droplet, transferred it to a new region, used the snapshot to create a new Droplet, and finally deleted both the original Droplet and the snapshot.

44 Comments

Creative Commons License