This is one of those things that we all kinda know but that it’s good to write down – how to deploy static files using rsync. For purposes of illustration, this article will describe how to deploy a statically generated website.
In the following discussion, you can assume that the static site resides at a directory local the script. Using rsync, it’s a simple command to turn your server into an exact copy of your local file system.
The script uses the
-n option, represented as the
NFLAG bash variable,
to do a dry run of your rsync command. This shows the list of files that
would be affected by the command, yet does not transfer any data.
The script is general purpose and takes advantage of a
.env file that
you would write. The
.env file declares the source directory you wish to
copy files from (
$DEPLOY_SOURCE_DIR), the account name to deploy with
$DEPLOY_ACCOUNT), the server to deploy to (
$DEPLOY_SERVER), and the
destination directory to copy files to on the server (
The rsync command uses the
--delete option to remove any files from the
server that no longer exist on the local filesystem and the
--exclude-from option to ignore any files listed in the
file that you would declare. The command is also recursive (
-r), compressed (
-z), and preserves file permissions (