Both SCP and RSYNC use SSH to transfer files and directories.

While SCP simply uploads or downloads files and folders, RSYNC can synchronize the content of two folders by copying only what is new.

The basic syntax of both looks like this:

[scp/rsync] [OPTION] … [SRC] … [USER@]HOST:DEST
[scp/rsync] [OPTION] … [USER@]HOST:SRC [DEST]

SCP

scp /home/user/file.zip [email protected]:/home/user/file.zip
scp file.zip [email protected]:/home/user
scp file.zip 200.100.50.3:/home/user
scp file.zip [email protected]:
scp file.zip 200.100.50.3:

scp [email protected]:/home/user/file.zip /home/user/file.zip
scp [email protected]:/home/user/file.zip /home/user
scp [email protected]:/home/user/file.zip .
scp 200.100.50.3:file.zip /home/user
scp 200.100.50.3:file.zip .

The examples above show multiple ways to upload (1 to 5) and download (6 to 10) files.

The first example in each block (in bold) shows the complete syntax, unless you need to apply additional options.

By omitting the filename at the destination, it will be copied with the same name.

By omitting the full path on the source, it refers to the current directory.

By omitting the full path at the destination, it saves to the home directory of the user.

By omitting the username, it will use the same username as the one you are logged in with locally.

Use the option -r to copy directories recursively, -v (verbose) to print debugging messages, and -P (same as –progress) to see real-time progress.

RSYNC

The main feature of RSYNC is the ability to compare file dates and directory contents to determine what is new or missing on either side.

rsync -r /directory [email protected]:/directory
rsync -r [email protected]:/directory /directory

The option -r (recursive) copies all content, while option -a (archive) compares both directories (local and remote) and only copies what is missing or has been updated.

Option -b (backup) will not overwrite the old file with the new version but instead makes a backup copy of the old one first (file.zip~). The option –delete removes files at the destination that were deleted from the source.

Using option -z (compress), traffic will be compressed, saving time on slow connections but increasing CPU usage on both ends. The option –dry-run simulates the operation and shows what would be done without actually performing any changes.

The option –exclude ‘dir1’ prevents RSYNC from comparing or copying a specific file or directory.

You can also try the option –backup-dir=$(date +%F) to see what it does.


BONUS

LSYNCD is a tool that runs as a service and periodically compares and syncs files (using RSYNC) based on a provided configuration.

sudo apt install lsyncd -y
sudo mkdir /etc/lsyncd
sudo nano /etc/lsyncd/lsyncd.conf.lua

Add the following content, customizing the parameters in bold:

settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
statusInterval = 20,
nodaemon   = false
}

sync {
default.rsync,
source = "/path/",
target = "/path"
}

Note: the interval (20) is in seconds.

systemctl start lsyncd
systemctl enable lsyncd
systemctl status lsyncd

For troubleshooting:

tail -f /var/log/lsyncd/lsyncd.log
tail -f /var/log/lsyncd/lsyncd.status

Backing up additional or newer files from source to destination.

rsync -rlptgo /source /destination