In this blog, we will discuss the way to transfer files among systems using SCP & Rsync with some examples. For remote copying, there are various things that needs to be taken into consideration. Like speed, security, Bandwidth, data replication etc.
Rsync
Rsync is a command used for moving and synchronizing files. With this command we can manage files or directories effectively while backing up data on a regular basis. Basically rsync compares if there is any differences. And only transfer those differences. Therefore it is a very flexible network-enabled syncing tool. Also while copying if there is a connection interruption, it can pick up quickly by reissuing the same command.
Rsync command
Syntax:-
rsync OPTION SOURCE DESTINATION
- If we don’t pass the option then it will sync between two directories. But only files in the main directory, not include files in the subdirectories.
Sync all files in original directory to duplicate directory
rsync SOURCE/* DESTINATION/
-r
or-recursive
Sync all files in all the main directory and subdirectories. Sync all files in original directory and its subdirectories
rsync -r SOURCE/ DESTINATION/
-a
or-archive
This option allow us to copy almost everything, including file permissions, user & group ownership and timestamps.
rsync -a SOURCE/ DESTINATION/
-v
or-verbose
It gives us a summary about the data transferred.
rsync -v SOURCE/ DESTINATION/
-h
or--human-readable format
We will get the result in human readable format.
rsync -h SOURCE/ DESTINATION/
-z
or-compress
Compress file data during the transfer
rsync -z SOURCE/ DESTINATION/
- -e
To specify the type of protocol we use. We can choose using ssh or rsh.
For instance, sync using ssh protocol
rsync -avhze ssh SOURCE/ DESTINATION/
- –progress
To show the progress while transferring the data
rsync -avhze ssh --progress SOURCE/ DESTINATION/
- –update
To updated more recently on the local filesystem. Files that don’t exist are copied. Moreover files that already exist, but have a newer timestamp are also copied. Basically update only if there is a new version.
rsync --update SOURCE/ DESTINATION/
- –remove-source-files
To remove files that successfully transferred.
rsync --remove-source-files SOURCE/ DESTINATION/
- –delete
Delete files that have been deleted in the original directory
Scp
To copy files and directories in secure way we use for scp. Basically, It is a plain linear copy. scp reads the source file and writes it to the destination. Secure Copy (SCP) uses SSH to copy only the files or directories that you select. It uses ssh for data transfer, and uses the same authentication and provides the same security as ssh. For authentication it can use ssh key. We use SCP for it’s simplicity, security and pre-installed availability.
Scp command
Syntax:-
scp OPTION SOURCE DESTINATION
- Copy a single file
scp SOURCE_FILE.txt DESTINATION/
- Copy multiple files
scp FILE1.txt FILE2.txt FILE3.txt DESTINATION/
- Copy .pem file
scp -i file.pem file.pem DESTINATION/
-r
to copy a directory recursively
scp -r SOURCE/ DESTINATION/
-P
to specify the port
scp -P 2249 SOURCE/ DESTINATION/
-v
for verbose
scp -v SOURCE/ DESTINATION/
-l
limit the bandwidth usage
scp -l 400 SOURCE/ DESTINATION/
-q
for quite mode
scp -q SOURCE/ DESTINATION/
-p
for preserves modification times, access times, and modes from the original file
scp -p SOURCE/ DESTINATION/
-u
to delete source files after the transfer is successful
scp -u SOURCE/ DESTINATION/
Conclusion
Conclusion is, rsync is good for incremental transfers and for taking the backup while scp is good while securely pushing or pulling the small file from or to the remote nodes.
Reference
https://www.ssh.com/academy/ssh/scp