COPY FILES BETWEEN SERVERS WITH RSYNC

In all my servers (Linux) I have rs ync installed.

Rsync allows to copy files, or rather synchronize them, between different servers.

What makes it stand out is that it does it incrementally, fast and in a very simple way. This means that it only sends the files that have been modified, very useful to use in backups or deploys.

I use it for two different things:

  • Making a quick backup of all the web files.
  • Move a web from one server to another (and also to move the web from my local to the client's production server).

By command line, it is very easy to use, but as I always leave something out ... well I made this blog post to have it documented.

Basically: 

To copy a directory recursively keeping permissions on the files you can do the following.

From external server to your local machine

rsync -avzP -e 'ssh -p222' [email protected]:/var/www/ /directorio/web/

With this we are copying everything in the path /var/www from the server with IP 123.123.123.123.133, using an ssh connection on port 222, to a directory "/directory/web" on the current machine.

From local machine to external server

We may also want to do it the other way around, from our current machine to send the data to another server using an ssh connection:

rsync -avzP /directorio/web/ -e 'ssh -p22' [email protected]:/var/www/

Note that we are now using a different port, port 22 instead of 222. Using rsnyc is very flexible, and you can use ssh keys to do this without having to remember passwords.

Tags

Have Any Project in Mind?

If you want to do something in Drupal maybe you can hire me.

Either for consulting, development or maintenance of Drupal websites.