How to Upload and Synchronise Files with SCP / RSYNC?
SCP and RSYNC are services used for copying or synchronizing files between separate locations, respectively.
SCP (secure copy) is based on ssh and works by reading the source file and copying it to the selected destination - locally or through a network.
RSYNC can also be used to copy files locally or over the network as scp above, but in addition it synchronizes the data. It does not copy and paste the entire directory, but is able to detect changes and replicate them to the destination.
SCP
General syntax used for scp:
scp [options] [source] [destination]
A basic example will look something like this:
scp home/test/file1 [email protected]:/home/eouser/
Explanation:
home/test/file1 is the source file
eouser is a username of the server
185.178.13.12 is the ip address of the remote server
home/eouser/ is the destination folder
Output:
file1 100% 230 43.6KB/s 00:00
Options:
-r This is mainly used for copying folders, and is used to recursively copy entire directories.
-P Specifies the port to connect to on the remote host.
-c Selects the cipher to use for encrypting the data transfer.
-C Enables compression
-i identity_file This option is necessary if the default files for authenticating the ssh connection (id_rsa) are not used.
scp example
scp -i ~/.ssh/<private_key> <file/directory> [email protected]:/home/eouser/
scp output:
<file> 100% 1680 49.6KB/s 00:00
RSYNC
General syntax used for rsync:
*rsync [options] [source] [destination]*
A basic example will look something like this:
rsync -avz –progress -e "ssh -i /home/account/.ssh/id_rsa" file1 [email protected]:/home/eouser/
Output:
sending incremental file listfile1sent 135 bytes received 35 bytes 113.33 bytes/sectotal size is 57 speedup is 0.3
Options:
v : verbose
-r : copies data recursively (be cautious, it does not copy file/folder permissions while transferring data)
-a : archive mode; this allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
-z : compress data
*-h : human-readable, output numbers in a human-readable format
You have to add your private ssh key for authentication with the -e parameter.
rsync example
rsync -avz -e "ssh -i ~/.ssh/<private_key>" <file/directory> [email protected]:/home/eouser/
rsync output:
sending incremental file list
<file>
sent 1,404 bytes received 35 bytes 959.33 bytes/sec
total size is 1,680 speedup is 1.17