Rsync, old but still powerful tools for moving files between hosts. The main use is to keep file trees synchronized. Rsync have been used for long time since its inception. And it seems Rsync is still used this day.
Rsync is available in many platform, especially UNIX-like system. As the title said, we will discuss about Rsync and not nailed to specific Operating System. In this article we are not discussing about how to install rsync so I assume we already have rsync installed on local machine.
We also not covering about how to prepare rsync server. It will be discussed in another article.
Rsync syntax in general would be:
rsync [OPTION] SRC DST
Where SRC is the source address and DST is the destination.
In general, we can use rsync for some purposes:
- Copying local files.
- Copying from local machine to remote machine.
- Copying from remote machine to local machine.
Synchronize local directory and remote rsync server.
On next sections, we will discuss each way described above. But our topic will be focused to point (2) to (4). Well, why would we use rsync to copying local files instead of using cp command?
It is also recommended to used rsync over ssh. Rsync does not provide any security while transferring data therefore we need to tunnel it with secure remote connection.
Now, some arguments or command options we will used in this article:
- –delete: delete files that don’t exists on sender (system)
- -v: verbose, use -vv for more detailed information
- -e “ssh options”: specify ssh as remote shell. The “options” will then passed to ssh when creating a connection
- -a: archive mode
- -r: recurse into directories
- -z: compress file data. Might consume memory and cpu resource but helpful for low bandwidth.
Copying from Local Machine to Remote Machine
Also known as push / upload file(s) into remote machine. Suppose we want to copy /var/www/backup.tgz to a remote server called backup.celestial-links.net on home directory, we could use:
rsync -v -e ssh /var/www/backup.tgz [email protected]:~
You will then be prompted by a password authentication message.
Copying from Remote Machine to Local Machine
Also known as pull / download file(s) from remote machine. Suppose we want to copy /usr/bin/secretbackdoor from remote server machine.celestial-being.net to local storage /tmp:
rsync -v -e ssh [email protected]:/usr/bin/secretbackdoor /tmp/
Synchronizing Local Directory and Remote RSync Server
Well, this is the main purpose of rsync (guess where rsync name comes from). Here the methods are two way connection, it means both machine will synchronizing their content.
rsync -r -a -v --delete rsync://rsync.celestial-links.net/somedir/ /local/somedir/
Or you can swap the src and destination, its same
rsync -r -a -v /local/somedir rsync://rsync.celestial-links.net/somedir
Note that this method use pure rsync which means we are not using it on top of ssh protoocl.
If you want to only synchronize a local directory according to remote directory:
rsync -r -a -v -e "ssh -l xathrya" --delete /local/somedir backup.celestial-links.net:/somedir
Or you want to only synchronize a remote directory according to our local directory:
rsync -r -a -v -e "ssh -l xathrya" --delete backup.celestial-links.net:/somedir /local/somedir
Transfer from One Remote Machine to Other Remote Machine
Suppose we have two remote servers: A and B. In this section we will use rsync to move a file from A to B without transit to our machine. It means file on A is directly copied to B.
rsync -zavrR --delete --links usernameA:[email protected]:/path/to/resource \ usernameB:[email protected]chine-B:/path/to/resource