LowEndBox

Hosting Websites on Bare Minimum VPS/Dedicated Servers

How to Migrate a Hosted Server in 5 Easy Steps with Rsync

Tags: , , , , , , Date/Time: January 25, 2016 @ 4:56 am, by Jarland Donnell

In order to migrate the files on a VPS box from one provider to another, one of the best solutions for the job is to use a tool called Rsync.

Let’s take a closer look at Rsync and see how it can help you rapidly move virtual private servers from one provider to another.

About Rsync

Rsync is one of the most common ways to copy/backup/restore files and folders between two locations, regardless if these endpoints are local or remote servers.

It supports compression, encryption and incremental transfer which makes the app an extremely versatile and useful tool for systems administrators.

Note: Running Rsync does not require you to be logged in as root.

Since Rsync supports incremental file transfer, the first time you will run it, it will require the same time to copy all the files as any other command However, when you subsequently execute Rsync, it will determines which changes have been made and only transfers those files.

This mechanism is designed to save time for the system admin, load and bandwidth.
Getting Acquainted with Rsync

Let’s get started and copy our live running server in a new location in 5 easy steps.

Step 1) Ensure that your OS is in Place

In order to migrate your server to a new location, the first step is to install your operating system onto new infrastructure. You can determine what architecture your current server is running on with the following command:

uname –a

You will want to use a Linux distro and kernel as close as possible to the one installed on server you are migrating.

Most VPS providers will set this up for you when you buy your new VPS server.

Tip: You may only need to do this step if you are building out a server locally. Hypervisors such as VMware help administrators streamline the process of installing operating systems onto hardware.

Step 2) Check the connection between the 2 servers

Once you have the two systems up and running you will need to check to see if it will be possible to make a connection between the two servers.

You can easily do that with the command “ssh”. Assuming you are running SSH from the new server and trying to connect to the old one, if the old server asks for the password, you passed the test!

ssh user@oldserver

Step 3) Check Rsync

At this point you will have to verify that Rsync is installed on both systems and if not, it’s time to install it. You can check if the command is present in the following way:

which rsync

In case the tool should not be present, you can easily install it using the following commands:

apt-get install rsync (on Ubunbu based distros)
yum install rsync (on CentOS based distros)

Step 4) Prepare the Exclude List

You will now only need to decide which directories to exclude. This may vary from system to system, but I would never suggest you to include the following unless differently needed:

/etc/fstab
/etc/sysconfig/network-scripts/* (CentOS distros)
/etc/network/* (Ubuntu distros)
/proc/*
/tmp/*
/sys/*
/dev/*
/mnt/*
/boot/*

Step 5) Run Rsync

Some VPS administrators may be worried about running Rsync while a MySQL instance running.

In most cases, this won’t present a problem. You might consider running it outside of heavy load periods if your server is hosting a live system, but other than that you should not have any problems.

Of course you will not have a consistent copy of the DB unless you stop the service before beginning Rsync, so please keep that in mind.

In this instance, Rsync will create a copy and it will allow you to test the system on the new server, which is usually always a big plus.

An Example of Rsync at Work

Assuming you are logged into the destination server, you’d implement a command that looks like this:

rsync -auHxv –numeric-ids –exclude=/etc/fstab –exclude=/etc/network/* –exclude=/proc/* –exclude=/tmp/* –exclude=/sys/* –exclude=/dev/* –exclude=/mnt/* –exclude=/boot/* –exclude=/root/* root@SRC-IP:/* /

Once it finishes, simply reboot your destination server and you will notice that you will have a precise copy of the files located on your source VPS.

(Tutorial written by a contributor)

29 Comments

  1. agentmishra:

    wonderful

    and

    usefull

    January 25, 2016 @ 5:37 pm | Reply
  2. Bacula, I think it can do a better job

    January 26, 2016 @ 8:10 pm | Reply
  3. Anton:

    Please make a tutorial about redundancy with rsync

    January 31, 2016 @ 4:42 pm | Reply
  4. Sven:

    Regarding database consistency – you can just run rsync twice. First you run it while the DB is up, then you stop the DB and run it again. It won’t take very long the second time and you will have a consistent copy of the data.

    February 4, 2016 @ 1:26 pm | Reply
  5. hawkjohn7:

    Please make tutorial about HA website, thanks

    February 8, 2016 @ 11:09 am | Reply
  6. goya:

    how about with using custom ssh port ? for example im using port 8888
    thanks

    February 10, 2016 @ 5:02 am | Reply
    • Pedro Frota:

      To use rsync over a different ssh port, use -e “ssh -p XXXX” where XXXX is the port number needed.
      The shell command listed in the post would then become:

      rsync -auHxv -e "ssh -p XXXX" –numeric-ids –exclude=/etc/fstab –exclude=/etc/network/* –exclude=/proc/* –exclude=/tmp/* –exclude=/sys/* –exclude=/dev/* –exclude=/mnt/* –exclude=/boot/* –exclude=/root/* root@SRC-IP:/* /
      February 16, 2016 @ 10:58 am | Reply
    • Sven W.:

      add the flag “-e ‘ssh -p 22′”. where “-p 22” is the port you want to connect to via ssh.

      February 16, 2016 @ 4:11 pm | Reply
  7. Good post and rsync is a great tool to migrate.
    I recommend using the -z switch for compression to save time and bandwidth.
    One caveat is that databases should be stopped before rsyncing to ensure consistency especially for MySQL. You could always rsync a final time or export all databases.

    February 15, 2016 @ 8:40 am | Reply
  8. John Smith:

    Doesn’t work anymore. I tried exactly and follow all but not working.

    June 25, 2016 @ 5:55 pm | Reply
    • Eric Hernandez:

      It works, I am right now migrating xD over Debian Jessie

      August 30, 2016 @ 2:58 am | Reply
    • Beware that this webpage converted the double hyphens in the parameters to long hyphens so you have to replace those with double hyphens and it will work.

      September 12, 2016 @ 11:59 pm | Reply
  9. i dizzy

    September 14, 2016 @ 8:09 pm | Reply
  10. Jaap:

    Unfortunately I end up with an error. Doesn’t matter if I use – or — hyphens.

    Unexpected remote arg: root@xx.xx.xxx.x:/*
    rsync error: syntax or usage error (code 1) at main.c(1348) [sender=3.1.1]

    November 16, 2016 @ 8:33 am | Reply
    • I also getting the same error message

      February 2, 2017 @ 9:14 am | Reply
    • I get a slightly different error msg:

      Unexpected remote arg: root@xx.xx.xxx.x:/*
      rsync error: syntax or usage error (code 1) at main.c(1201) [sender=3.0.6]

      Does anyone know how to fix this?

      BTW, I tried the following changes to no avail:

      ‘ to ”
      – to —

      March 4, 2017 @ 5:02 pm | Reply
  11. Marta J. Petersenq:

    This article clearly explains how to migrate a hosted server. I like to add some more points to this Don’t Throw Out Your VPS Package Yet, Resist the Temptation to Make ‘Quick’ Changes. Find some more points here http://blog.fdcservers.net/tips-to-ensure-smooth-vps-to-dedicated-server-migration/

    January 31, 2017 @ 7:35 pm | Reply
  12. kraziekris:

    Hi, I just performed this task and while everything seemed to work fine, I cannot login to the destination server anymore as any user?

    March 9, 2017 @ 6:05 pm | Reply
  13. Melchior:

    What about users and file permissions??? Rsync is a great start for the file structure and contents, but this is hardly a migration.

    April 23, 2017 @ 10:15 am | Reply
  14. Maurizio Marini:

    Imho /etc/udev should be excluded

    May 4, 2017 @ 6:37 am | Reply
  15. Bob:

    Proper rsync command that worked for me is:
    rsync -auHxv –exclude etc/fstab –exclude etc/udev/ –exclude etc/sysconfig/network-scripts/ –exclude proc/ –exclude tmp/ –exclude sys/ –exclude dev/ –exclude mnt/ –exclude boot/ –exclude root/ –exclude opt/ root@SOURCE-IP:/* /

    Your excludes may be different than mine.

    June 14, 2017 @ 1:59 pm | Reply
  16. replays – with – and it going work

    June 26, 2017 @ 1:57 pm | Reply
  17. wow cool this helped alot

    August 8, 2017 @ 10:59 am | Reply
  18. Great post
    keep sharing :)

    August 18, 2017 @ 3:29 am | Reply
  19. David Favor:

    Be sure to exclude /etc/netplan/* + /etc/sysconfig/network-scripts/* + /etc/systemd/network/* as all these directories may contain various networking related files, which may destroy your target system.

    May 31, 2018 @ 5:22 pm | Reply
  20. fish:

    hi,mr Jarland Donnell:
    i encountered an error:
    https://pastebin.com/g44EBE6j

    how to solve it?

    June 29, 2018 @ 10:54 pm | Reply
  21. Maurizio Marini:

    may be interesting to not rsync /etc/lvm
    too

    July 5, 2018 @ 6:58 am | Reply
  22. Maurizio Marini:

    maybe you should exclude /lib/modues too
    to be consistent with /boot exclusion

    July 9, 2018 @ 6:05 am | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Quoting webhostingtalk.com URL seems to get binned consistently here, but I do peek into the spam box frequently to publish those comments.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

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