đCheatsheet
Sync one directory into another with compression:
rsync -az source/directory destination/directory
Sync over SSH:
rsync -az user@source-server.com:path/to/file destination
or
rsync -az source/directory user@destination-server.com:destination/path
Sync does not delete existing files at destination. To make sure the source and destination folders look identical, use the --delete
flag:
rsync -az --delete source/directory destination/directory
đ What is Rsync?
Rsync is this neat utility that commonly exists on Linux systems (and other Unix-likes). Itâs like a copy command but the destination (or source) can be remote.
It syncs files rather than copies them so if the destination file is already present, it doesnât get overwritten. This is great because you can sync large folders several times but only the missing files will get copied.
So while you might run something like cp -r ~/projects/dotfiles/.bashrc ~
to copy a .bashrc
file from a folder to your home directory, rsync lets you do it in a remote fashion like so: rsync me@my-server.com:path/to/file ~
. Or the other way around rsync path/to/file me@my-server.com:path/to/destination
.
To me, what makes rsync complicated are the flags. Itâs always the flags that trip me up and push me to feel uncertain about the command.
đłCopying with no flags
You lose a lot by not providing flags. Without flags, you can also sync one file at a time.
rsync source/file destination
You can also do remote copy:
rsync serverUser@serverAddressOrIP.com:path/to/file destination
Rsync uses ssh
for remote connections so make sure you have access via ssh!
Copying the other way is the same deal:
rsync path/to/file serverUser@serverAddressOrIP.com:destination
đŠ Supercharging rsync with flags
rsync -flags source destination
There are several helpful flags and if you follow any guides online, youâll most likely come across these same exact ones.
Flags:
a
- stands forarchive
. It is necessary for recursive copies (ie. copying whole directories)z
- thez
flag enables compression for faster copying across networksv
- for verbose output
The archive
option has always been confusing to me. Luckily, thereâs a great server fault post detailing the archive flag. Itâs a compilation of other flags that allows recursive syncing - so directory copy/sync support. And if youâre copying locally, it preserves permissions and a bunch of other stuff.
The z
flag enables compression via Zlib (at least thatâs what Wikipedia tells me!). In case you wanted to know, zlib uses the same compression technique as gzip
which you can read about in my Demystifying TAR article.
The verbose output should display files copied file-by-file. A typical rsync command might look like this:
rsync -azv ./files-to-upload me@myserver.com:~/uploads
đłď¸âđ ď¸âMore flags? (OPTIONAL)
Donât read this section of rsync is already giving you a headspin. But if you ever want to refer back to this article, you can check these flags out. I wouldnât use any of these by default you might come across them in tutorials/articles or you might want to use them yourself:
n
- stands fordry run
. This will check that you actually can transfer those files.delete
- delete files from destination that donât exist in sourceP
- TheP
flag is a combination of the--partial
and--progress
.
The n
flag is handy because you can use it to make sure youâre running your command correctly. Combined with a verbose output, you can see exactly whatâs going to happen without making it happen. I found this useful when preparing to upload a lot of files and not wanting to screw up where they upload or which files upload.
The delete
flag is also super useful in that it doesnât just copy files to the destination, it ensures that files in the source directory donât exist in the destination. Super useful when syncing from a source that you consider the âsource of truthâ and want to have identical copies elsewhereâŚwithout having to delete everything and copy everything from scratch.
I donât often use the P
flag but for many, itâs the default. The P
flag will do 2 very important things:
- allow partial file upload + resume
- display a progress bar
If you check the screenshot above, I ran that copy with a P
flag. At the end, itâll let you know neat stuff about how much data youâve transferred if you combine it with the v
flag.
The wonderful part about partial file uploads is that if your connection fails, you can run the command again and itâll pick up where it left off with the partially uploaded files. Without the P
flag, rsync would not be able to finish uploading the interrupted file.