Project files move around several times during the development process. A standard cycle includes:
- Development environment – usually someone’s laptop/desktop or a private area of a web server
- Staging environment – usually on the web but password protected or not indexed by search engines
- 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.
Rsync addresses many common problems:
- Speed – network to network transfers are much faster than downloading a site to your own computer, then uploading it again to the destination.
- File and folder permissions – with the right option enabled, rsync keeps your folder permissions after the transfer, helping you avoid “permission denied”
- Hidden files – rsync will grab hidden files like .htaccess. This often gets missed with other methods.
- Convenience – you can migrate a site with a single command
What you’ll need:
- SSH Access
- rsync installed on both computers (if doing a remote transfer)
- 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”
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 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 .
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