Originally published on 17 September 2019
Note: This was an blog post that, for some reason, was stuck in my Drafts folder since December 2018. As part of my effort to restart my blog, I found this and thought it would be wise to publish it now, especially since I depend on rsync
to sync my HTML files between my local machine and the server.
rsync
is a powerful, fast and versatile command line utility that allows systems administrators to quickly make copies of files and directories, sync them locally or sync them across machines. It has a large number of options which are documented in the man page as well as in the project's home page. This post will focus on describing how I use rsync
to transfer my blog posts from my local machine over to my FreeBSD web server. Please bear in mind that I am only using a fraction of what rsync
is capable of doing. Resources documenting additional functionality are included below in case you want to learn more.
The main use case I have for rsync
is to simply sync a folder on my local machine, which contains my blog posts (html
and css
files) with a folder on my FreeBSD server. Generally speaking, the command I use is:
$ rsync -avP --delete -e "ssh -p <port>" /path/to/directory/on/local/machine/ username@ipAddress:/path/to/directory/on/server
The -a
flag stands for "archive" and ensures that things like permissions, symbolic links, groups, owners and modification times are preserved while syncing. It also incorporates the -r
feature for recursively syncing directories.
The -v
switch, or verbose option, provides additional details on what rsync
is doing while it is performing its operations. There are options to increase verbosity and to toggle how much information is displayed.
The -P
option combines the --partial
and --progress
options to not only show the command's progress during the operation, but to also preserve any partially transferred files in case the operation is interrupted.
The --delete
option deletes any files on the target system that are not present in the source system. Do not use this option for scenarios where you are using rsync
to perform backups as this will delete files in your backup that you may have accidentally deleted.
The portion of the command made up of -e "ssh -p <port>"
allows you to use rsync
over ssh
when you are using a port other than 22 for your ssh
connections. See my post Harden SSH Configuration for more information on how to use a non-standard ssh
port.
The last parts of the command are made up of the source directory followed by the destination directory, which must come after the username and IP address of the blog server. Note the trailing slash (/
) after the source directory which is required if you want to sync the contents of that directory into the target directory. Without it, you will copy the source directory into the target folder. See the first additional resource linked to below from Digital Ocean for more details on this.
rsync
also provides options to exclude certain files and folders from syncing. It also has a facility, -n
, to first execute a dry run so that you can see what will actually happen when you run it for real. It's a good idea to use this option the first time you are using the command.
As promised above, here are some additional resources to learn about the many features of rsync
in case you still need something above and beyond the man page.