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)
Related Posts:
- Q3 Top Provider Poll Results - November 13, 2016
- BudgetNode – DDoS Protected OpenVZ VPS Starting @ $12/year – Ashburn, Virginia - June 29, 2016
- BuyVM – $7/m KVM 2GB RAM / 40GB SSD / 2TB BW – in Las Vegas - June 17, 2016
wonderful
and
usefull
:v
Bacula, I think it can do a better job
Please make a tutorial about redundancy with rsync
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.
+1 for this. No wasting time and uptime.
Please make tutorial about HA website, thanks
how about with using custom ssh port ? for example im using port 8888
thanks
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:
add the flag “-e ‘ssh -p 22′”. where “-p 22” is the port you want to connect to via ssh.
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.
Doesn’t work anymore. I tried exactly and follow all but not working.
It works, I am right now migrating xD over Debian Jessie
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.
i dizzy
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]
I also getting the same error message
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 —
need to do –exclude instead of -exclude
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/
Hi, I just performed this task and while everything seemed to work fine, I cannot login to the destination server anymore as any user?
What about users and file permissions??? Rsync is a great start for the file structure and contents, but this is hardly a migration.
Imho /etc/udev should be excluded
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.
replays – with – and it going work
wow cool this helped alot
Great post
keep sharing :)
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.
hi,mr Jarland Donnell:
i encountered an error:
https://pastebin.com/g44EBE6j
how to solve it?
may be interesting to not rsync /etc/lvm
too
maybe you should exclude /lib/modues too
to be consistent with /boot exclusion
Because Numeric IDs are used, you need to replace /etc/passwd which didn’t happen for me, presumably because root didn’t have write access. It worked once I added write access (I also did shadow and sudoers)
Hi, I have do that from Linode to GCP Compute Engine Instance with the followwing command:
sudo 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@172.104.50.88:/* /
Big thanks.
Hello An, i have run the same command as u, AND rsync copying finishes successfully, but when i reboot my destination server, i cannot access the server via ssh, error”Could not connect to ‘34.92.95.3’ (port 22): Connection failed.” and i reinstall the os and start over to rsync and also try to update grub using command”grub2-mkconfig -o /boot/grub2/grub.cfg” but still got the same error, can you please tell me what did u do after entering the following command? thanks so much.
sudo 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@IP ADDRESS:/* /
Hi everyone,
I’ve got the same problem that k k mentionned? When i’m doing rsync from one remote server to an other remote one, my server is not usable when it restarts, and my ssh is not working anymore.
Any suggestion ?
Regards,
Arnaud
You must exclude the folder with ssh keys
hi,
I ran into syntax errors while running this command in Debian 9. The command below worked for me.
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@REMOTE_IP: /* /
I can’t delete the previous comment, but please ignore it, wrong command.
This one worked for me on Debian 9 after getting a lot of syntax errors from the recommended command here.
It’s not working for me, I need to copy all the data from centos 4 5 or 6 to a new centos server. I don’t care about wasting space I just need to copy the data minus the OS. I have tried the command but get errors.
Super Simple. I was going nuts with clonezilla. Just install OS (mine is ubuntu) copy over the root certificates and its done
This worked fine on Ubuntu 18.04
Tried to post the command but cloudflare blocked me LOL
Hi, Is it going to work on SUSE sap servers with licensing ? We need to migrate the server from old hypervisor to new.
Only this worked on Ubuntu 20 :
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@Server-IP:/* /
I use Ubuntu 20.04 and show error:
rsync error: syntax or usage error (code 1) at main.c(1372) [sender=3.1.3]
rsync -aHAXSz –delete –info=progress2 –numeric-ids -e “ssh” –rsync-path=”rsync” –exclude={“/dev/*”,”/proc/*”,”/sys/*”,”/tmp/*”,”/run/*”,”/mnt/*”,”/media/*”,”/lost+found”,”/etc/fstab”,”/etc/udev/rules.d/*”,”/etc/network/*”,”/lib/modprobe.d/*”,”/boot/*”,”/root/.ssh/”,”/etc/ssh”} root@SOURCE_IP:/* /
This script succeed in Ubuntu 22.04 Jammy