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 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 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/
Sync all files in all the main directory and subdirectories. Sync all files in original directory and its subdirectories
rsync -r SOURCE/ DESTINATION/
This option allow us to copy almost everything, including file permissions, user & group ownership and timestamps.
rsync -a SOURCE/ DESTINATION/
It gives us a summary about the data transferred.
rsync -v SOURCE/ DESTINATION/
We will get the result in human readable format.
rsync -h SOURCE/ DESTINATION/
Compress file data during the transfer
rsync -z SOURCE/ DESTINATION/
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/
To show the progress while transferring the data
rsync -avhze ssh --progress SOURCE/ DESTINATION/
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/
To remove files that successfully transferred.
rsync --remove-source-files SOURCE/ DESTINATION/
Delete files that have been deleted in the original directory
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 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/
-rto copy a directory recursively
scp -r SOURCE/ DESTINATION/
-Pto specify the port
scp -P 2249 SOURCE/ DESTINATION/
scp -v SOURCE/ DESTINATION/
-llimit the bandwidth usage
scp -l 400 SOURCE/ DESTINATION/
-qfor quite mode
scp -q SOURCE/ DESTINATION/
-pfor preserves modification times, access times, and modes from the original file
scp -p SOURCE/ DESTINATION/
-uto delete source files after the transfer is successful
scp -u SOURCE/ DESTINATION/
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.