Monday, April 14, 2014

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

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 rsynccommand.
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

No comments:

Post a Comment