rsync is a simple way to copy files and folders between your local computer and server.
It not only makes file-transfer easy, but it allows you to build and maintain your website offline, then easily upload it to the proper directory on your server so you don't need to constantly be logged into your server to modify your site.
Run the following on your server and on your local machine.
apt install rsync
Uploading files with rsync
From your local machine you can upload files to your server like this:
rsync -rtvzP /path/to/file firstname.lastname@example.org:/path/on/the/server
You will be prompted for the root password and then uploading will commence.
If you omit root@, rsync will not attempt to log in as root, but whatever your local username is.
Options to rsync
In this command, we give several options to rsync:
-r– run recurssively (include directories)
-t– transfer modification times, which allows skipping files that have not been modified on future uploads
-v– visual, show files uploaded
-z– compress files for upload
-P– if uploading a large file and upload breaks, pick up where we left off rather than reuploading the entire file
Avoid using the commonly used
-a option when uploading. It can transfers your local machine's user and group permissions to your server, which might cause breakage.
It's a good idea to build your website offline, then make an rsync script or bash alias like the one above to upload the edited files when you have made updates.
To avoid having to manually input your password each upload, you can set up SSH keys to securely idenitify yourself and computer as a trusted.
Picky trailing slashes
rsync is very particular about trailing slashes. This is useful, but can be confusing to some new users. Suppose we run the following wanting to mirror our offline copy of our website in the directory we use on our server (
rsync -rtvzP ~/websitefiles/ email@example.com:/var/www/websitefiles/
This will not actually do quite what we want. It will take our local
websitefiles directory and put it inside
websitefiles on the remote machine, ending up with:
Instead, remove the trailing slash from the remote server location:
rsync -rtvzP ~/websitefiles/ firstname.lastname@example.org:/var/www/websitefiles
websitefiles/ has been replaced with
websitefiles, and this will do what we want.
Downloading files with rsync
You may just as easily download files and directories from your server with rsync:
rsync -rtvzP email@example.com:/path/to/file /path/to/file