Categories
Drupal Development Resources WordPress

Tutorial: Use rsync on Linux based servers to move/launch your websites

Project files move around several times during the development process. A standard cycle includes:

  1. Development environment – usually someone’s laptop/desktop or a private area of a web server
  2. Staging environment – usually on the web but password protected or not indexed by search engines
  3. Production/Live environment – this is ultimately where the website resides, usually accessible by a domain name

Sometimes one or more of the environments are on the same computer, but they’re often on different computers.

We like to use source control like git to roll out sites when possible, but whenever we need a quick migration or the clients’ server does not support git but has SSH access, we prefer the trusty rsync command.

rsync is synchronizes files across local or remote directories. It works on any Unix based operating systems such as Linux and Mac OS.

Why rsync?

Rsync addresses many common problems:

  1. Speed – network to network transfers are much faster than downloading a site to your own computer, then uploading it again to the destination.
  2. File and folder permissions – with the right option enabled, rsync keeps your folder permissions after the transfer, helping you avoid “permission denied”
  3. Hidden files – rsync will grab hidden files like .htaccess. This often gets missed with other methods.
  4. Convenience – you can migrate a site with a single command

What you’ll need:

  1. SSH Access
  2. rsync installed on both computers (if doing a remote transfer)
  3. Good understand of how to navigate Linux file systems

How to use rsync:

The general format is:

rsync [options] [source] [destination]

From my own experience, the following options are best for migrating a site:

  • a : archive mode, keeps timestamps and permissions
  • z : compresses data during transfer, optional but recommended
  • v : verbose mode, will show you progress and tell you where a transfer fails
  • r : recursive, grabs all files and folders within a folder

You can combine all of this into a single stringe of “-azvr”

Local transfers:

Let’s say you have site at “dev.domain.com” and you’re ready to copy it over to “domain.com”. Both folders are inside of “/var/www/”, a common location in Linux web servers with multiples domain names. You could use the following:

rsync -azvr /var/www/dev.domain.com /var/www/domain.com

This example uses absolute paths. You can also use relative paths. Assuming we’re in “/var/www/dev.domain.com”:

rsync -azvr . ../domain.com

Remote transfers:

Remote transfers work the same way, but you have to log into the remote host. You could use the following:

rsync -azvr /var/www/dev.domain.com user@host:/var/www/domain.com

Substitute “user@host” with your ssh credentials on the remote server.

Sync remote files to local host:

You can also reverse the direction and grab a copy of remote files to your local server. An example might be copying your production environment back to your development environment on your desktop. Our destination folder will be whatever our current folder is in terminal.

rsync -azvr user@host:/var/www/domain.com .

Exclusions:

rsync has good exclusion handling. Let’s say you want to copy everything except a certain folder:

rsync -azvr --exclude "/folder" . /var/www/domain.com

You can also use multiple exclude options for multiple folders/files:

rsync -azvr --exclude "/folder" --exclude ".htaccess" --exclude "robots.txt". /var/www/domain.com

6 replies on “Tutorial: Use rsync on Linux based servers to move/launch your websites”

You actually make it seem so easy together with your presentation however I in finding this matter to be actually one thing which I feel I would by no means understand. It sort of feels too complicated and extremely wide for me. I am having a look forward to your subsequent post, I’ll attempt to get the dangle of it!

I have enjoyable together with, bring on I uncovered what precisely I was taking a look intended for. You’ve ended my personal several morning prolonged seek out! Lord Thank you man. Have a fantastic day. Cya

Many shared server environments use a non-standard SSH port for transfers. To handle this use the following command (where the port number is 2234). Here I’m setting the SSH port on when connecting to the source file location. You could use the same syntax for the destination file location as well.

rsync -azvr -e ‘ssh -p 2234’ sshusername@sshdomainorip:sourcefilelocation [destination]

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.