After making several modifications to your test site, what’s the best way to transfer those changes to your live site? This post explains how to use Rsync over SSH to keep your test website synchronized to your live site.
Rsync saves time and is more efficient than using FTP to upload files one at a time because it can tell which files have changed based on their timestamp. It only copies the files that have changed since the last synchronization.
Update: I have created a bash script, which automates the syncing of web files, as well as MySQL databases. You can read the post about it here.
Build an Rsync Command
To build the arguments for the command, you will need to gather some information:
- Local directory – From which directory on your local computer will you be transferring the files? For example, /home/john/website
- Remote directory – The directory on the remote server where you will be copying the files. You may need to SSH to the server to find this out. On a shared hosting provider it may be something like: /home/username/public_html/mysite/
- Arguments – This is optional information you can pass to the command. For example, you may wish to preserve only the timestamp of your files, but not the owner, group, or permissions.
The format of the command, using SSH, is as follows:
rsync [arguments] -e ssh [local directory] [username]@[domain]:[remote directory]
Here is an example of the command, followed by an explanation of all the options:
rsync --dry-run --exclude-from=exclude.txt --delete -rltv -e ssh /home/santa/website/html/ email@example.com:/path/to/public_html/directory/
# –dry-run See what would happen, but don’t actually do anything (remove this argument when you want to actually transfer the files)
# –exclude-from Specify a file that contains a list of exclusions. See “File Exclusions” below.
# –delete Delete files from destination if they’ve been deleted from source
# -r Recursive (recurse into directories)
# -l Copy symlinks as symlinks
# -t Preserve modification times
# -v Verbose (more information)
# -e Specify remote shell to use (SSH in this case)
# Options NOT USED
# -p Preserve permissions
# -g Preserve group
# -o Preserve owner
# -D Preserve device/special files
For more detailed information about rsync’s arguments, have a look at its man pages:
Configure File Exclusions
There may be some local files or directories that you do not want to upload to your live site. This is where an exclusions file comes in handy.
Create a simple text file that will contain the exclusions (one line for each exclusion)
To exclude a file, precede it with a minus ‘-‘ sign followed by a space and then the file or directory name.
Here is an example of an Rsync excludes file:
# Files and directories excluded from rsync to hosting provider. Comments are preceded by hash '#' characters.
# Excludes wordpress blog files, plus some others