How to use rsync on local and remote machine in Linux 8 tips and tricks


 
     "rsync"
is a robust command tool that can be use to copy and synchronize folder either locally or remotely.It offer a large number of  options that we can use to control the behavior on how our machine handle file copying process. "rsync" use  its famous  delta transferring algorithm that make it flexible and extraordinary fast copying tool . For more information, please refer here .

    In this post, I'm going to share how you can use "rsync" command line on your everyday job task. "rsync"  can be  use in either in local mode only or between local and remote machine .If you use in local mode,it will act like an improved copy tools. If you use "rsync" between local and remote machine, It must be note here that "rsync" does not use any kind of secure file transmission .If you run it on unsecure network It is advisable to run in SSH mode  .



Guideline on using "rsync" in this tutorial
Local:
rsync -[OPTIONS] [SOURCE ] [DESTINATION]

Access via Remote ssh :
push : rsync -[OPTIONS] [SOURCE] [USER]@[HOST]:[DESTINATION]

pull : rsync -[OPTIONS] [USER]@[HOST]:[DESTINATION] [SOURCE]


[OPTIONS] that I use in this tutorial

-e                  = specify remote shell to use
-z                  =compress file data during the transfer
-v                  =give information on screen what is transferred and brief summary on whats going on
-a                  =It is a way of saying you want recursion and want to preserve everything form sourcefile
-u , --update  = skip any files which exist on the  destination  and  have  a  modified  time that is newer than the sourcefile
--delete         =delete extraneous files on the destination side (ones that aren’t on the sending side), but only  for  the directories  that  are  being synchronized.



8 Tip and tricks

1. Copy single file in local machine
2. Copy directory in local machine.
3. Synchronizing directory in local machine
4. Synchronizing file between folder without over writting file on the destination
5. Copy file from local machine to remote machine
6. Copy folder from local to remote machine.
7. Copy folder from remote to local machine.(execute on local)
8. Synchronizing from remote to local machine folder (execute on local)

(Note : I've  setup my SSH connection login without using any password .Please click here  to see how do I do it . I will share how you can run the command if you don't configure the SSH . Bare in mind if you don't configure the SSH, you will be prompted with login  password every time you execute rsync command.)


1. Copy single file in local machine

rsync -avz /home/shark_attack/Documents/Technical/linux/emacs.pdf /home/shark_attack/Desktop/backup

2. Copy directory in local machine.

rsync -avz /home/shark_attack/Documents/Technical/linux /home/shark_attack/Desktop/backup

3. Synchronizing directory in local machine

rsync -avz  --delete /home/shark_attack/Documents/Technical/linux /home/shark_attack/Desktop/backup


4. Synchronizing file between folder without over writing file on the destination

rsync -avzu /home/shark_attack/Documents/Technical/linux /home/shark_attack/Desktop/backup

5. Copy file from local machine to remote machine ( "pi" is my username "10.42.0.68" is remote  ip) .
Make sure you have the folder that you want to use as backup folder in the remote machine. You can do this by executing "mkdir" command .I set my backup folder  under "/home/pi/backup" 

rsync -avz /home/shark_attack/Documents/Technical/linux/emacs.pdf pi@10.42.0.68:/home/pi/backup

or

rsync -avz -e ssh /home/shark_attack/Documents/Technical/linux/emacs.pdf pi@10.42.0.68:/home/pi/backup


6. Copy folder from local to remote machine.

rsync -avz /home/shark_attack/Documents/Technical/linux pi@10.42.0.68:/home/pi/backup

or

rsync -avz -e ssh /home/shark_attack/Documents/Technical/linux pi@10.42.0.68:/home/pi/backup


7. Copy folder from remote to local machine.(execute on local)

rsync -avz pi@10.42.0.68 pi@10.42.0.68:/home/pi/backup /home/shark_attack/Desktop

or

rsync -avz -e ssh pi@10.42.0.68:/home/pi/backup /home/shark_attack/Desktop

8. Synchronizing from remote to local machine folder (execute on local)

rsync -avz --delete pi@10.42.0.68 pi@10.42.0.68:/home/pi/backup /home/shark_attack/Desktop

or

rsync -avz --delete -e ssh  pi@10.42.0.68:/home/pi/backup /home/shark_attack/Desktop