rsync(remote sync) commonly found on Unix-like systems which is a remote/local file synchronisation utility where It uses an algorithm that minimises the amount of data copied by only moving the portions of files that have changed. Without beating around the bush let’s get our hands dirty. I found this link really interesting. Also check out this link for the difference between scp and rsync.
Basic syntax and options of rsync
rsync options source destination
- -v , –verbose : verbose
- -r , –recursive : copies data recursively (but don’t preserve timestamps and permission while transferring data)
- -a , –archive : archive mode( copy files recursively and preserve symbolic links, file permissions, user & group ownerships and timestamps)
- -z , –compress : compress file data
- -P , –progress : show progress during transfer
Note : Any of the below commands with -P option will show the progress of the transfer. I will be using below list of files.
1. Dry Run with rsync
If you are not sure about rsync command output do a dry run and see the output. It will not actually do the transfer but give the exact output as the transfer occured.
rsync -az --dry-run test1/ test2/
2. Copy/Sync Files and Directory Locally
Let’s start with the basics. Copying files locally.
rsync -az test1/ test2/
3. Copy/Sync Files and Directory to or From a Remote Server
Now we are going to copy file to the remote server which is 192.168.1.10 from test1 folder.
rsync -az test1/ firstname.lastname@example.org:/home/myuser/
4. Rsync Over SSH
You can also specify any remote shell you like, either by using the -e command line option.
rsync -aze ssh email@example.com:/myuser/myfile1 test1/
Interesting : Is it possible to specify a different ssh port when using rsync? Yes.
5. Set Bandwidth Limit
If you are doing a huge file transfer make sure you apply a bandwidth limit before it eat out the available bandwidth. use –bwlimit=<kb/second>.
rsync -az --bwlimit=100 test1/ firstname.lastname@example.org:/home/myuser/
6. Automatically Delete source Files after successful Transfer
It tells rsync to remove from the sending side the files that are a part of the transfer and have been successfully duplicated on the receiving side.
rsync -az --remove-source-files test1/ email@example.com:/home/myuser/
7. Sync with –delete
You can delete files on the receiving side that don’t exist on the sending side with –delete option.
rsync -az --delete test1/ firstname.lastname@example.org:/home/myuser/
Please see the link for a more complex scenario in this link1 and link2. Also you might want to exclude and include files when transferring over the network as mentioned in this link. If you have any questions let me know in the comments below. Your feedback is highly appreciated(happy-face).