Skip to content

Migrating Ubuntu server from Amazon EC2 to DigitalOcean

Recently I migrated my blogs from AWS EC2 micro instance to a DigitalOcean 512MB VPS. Migration was surprisingly easy. All I had to do was sync everything from EC2 instance to DigitalOcean VPS using rsync provided I use Ubuntu 12.04 LTS server on both locations. This is what I did step-by-step.

  • Create a DititalOcean droplet with similar OS and version as the EC2 instance (in my case Ubuntu 12.04 LTS 32bit with LAMP stack pre-installed)
  • Generate ssh keypairs in the newly created DigitalOcean droplet and copy the public key to” ~/.ssh/authorized_keys” of the root user of the EC2 instance. (we should be able to login to EC2 intance from DigitalOcean droplet for rsync migration to work. It’s easier to use the root account in this case because we need to migrate config files which only root can access. Using rsync with default ‘ubuntu’ user having sudo privileges is difficult )
  
###########################################################
#
# Login to DigitalOcean droplet, and generate ssh keypair

ssh-keygen -t rsa #Complete the keygeneration process

#Now back from your laptop/local-control-pc
# Copy generated public-key to local machine
scp root@digitalceansvr:~/.ssh/id_rsa.pub  ~/

#Because by default EC2 instances use 'ubuntu' user with sudo we have to 
#copy the public key to 'ubuntu' user first and then copy it again to 'root'

scp ~/id_rsa.pub    ubuntu@ec2server:~

# now login to EC2 instance and issue these commands 

ssh ubuntu@ec2server
#following commands are given from EC2 instance
sudo mkdir  /root/.ssh
sudo cat ~/id_rsa.pub > /root/.ssh/authorized_keys
sudo chmod 600 /root/.ssh/authorized_keys


  • Now you should be able to login to Amazon EC2 instance’s root account from DigitalOcean droplet.
  • Before proceeding to next step, do an ‘apt-get update/upgrade’ on both EC2 instance and DitgitalOcean droplet. Also install any missing apache/php/mysql modules compared to EC2 in DigitalOcean droplet.
  • The next step is to synchronize everything with rsync-via-ssh.

#############################################################
## These commands should be given from the DigitalOcean droplet
## First stop LAMP stack

service apache2 stop
service mysql stop

## Then copy the config files. 
## Here --delete option is used to make an exact copy from EC2 to
##  DigitalOcean deleting any additional files in DigitalOcean droplet
## Copy all required config files as necessary

rsync -avzhe ssh --progress --delete root@ec2server:/etc/phpmyadmin   /etc/
rsync -avzhe ssh --progress --delete root@ec2server:/etc/php5   /etc/
rsync -avzhe ssh --progress --delete root@ec2server:/etc/apache2   /etc/
rsync -avzhe ssh --progress --delete root@ec2server:/etc/ssl   /etc/
rsync -avzhe ssh --progress --delete root@ec2server:/etc/mysql   /etc/

## Finally copy www directory and mysql databases
## Here --delete option is used to make an exact copy from EC2 to
##  DigitalOcean deleting any additional files in DigitalOcean droplet

rsync -avzhe ssh --progress --delete  root@ec2server:/var/lib/mysql   /var/lib/
rsync -avzhe ssh --progress --delete  root@ec2server:/var/www /var/

## Now start LAMP stack
service apache2 start
service mysql start


  • Voila! Everything should be up-and-running now!

This is my referral code for DigitalOcean if you are interested in testing out DigitalOcean cloud servers:

https://www.digitalocean.com/?refcode=8415bee5bb8a

One thought on “Migrating Ubuntu server from Amazon EC2 to DigitalOcean

  1. Justin Samuel says:

    Hi, in case it’s useful to you next time, you might want to try using ServerPilot (https://serverpilot.io) to set up the LAMP stack on a DigitalOcean server. That way, you can use ServerPilot to do ongoing configuration like adding new sites and databases. It also keeps the server’s packages updated.

    I’m one of the founders. If you have any questions or feedback, please shoot me an email.

    Thanks,
    Justin

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.