Rsync is also known as remote sync is a command-line tool used to copy and sync files and directories remotely as well as locally in Linux/Unix systems. With this rcopy and synchronize data:
- Remotely and locally across directories, disks, and networks.
- Perform data backups.
- Mirror between two Linux machines.
We have created a list with various Rsync command examples that include using Rsync backup examples and other tips that will help you make the most out of this fabulous tool.
Let’s get in!
Table of Contents
- 1 Why use Rsync Commands in the first place?
- 2 17 Different Rsync Commands To Use (Man, Backup, SSH and Directories)
- 2.1 Rsync Syntax
- 2.2 Common Rsync Arguments
- 2.3 Installing Rsync (Remote Sync) on Linux
- 2.4 Copy and syncing files or a directory (Local to Remote Site)
- 2.5 Copy or Sync a remote directory to local desktop
- 2.6 Display Rsync Transfer Progress
- 2.7 Rsync over SSH
- 2.8 Backup files from a cloud server to a locally hosted server via SSH
- 2.9 Copy file from local server to a local remote server with SSH
- 2.10 Rsync –delete
- 2.11 Set the max size of files to be first transferred
- 2.12 Do a dry run with Sync
- 3 Conclusion: Which Rsync command amazes you?
Why use Rsync Commands in the first place?
Thought you would ask, so here is why:
- Copies and syncs file efficiently to or from a remote system.
- It supports copying links, devices, owners, groups, and permissions.
- It is much faster than Secure Copy (scp) as it uses a remote-update protocol that allows transferring between two sets of files. For the first time, it copies the entire content of a file or directory from the source towards a destination but from next time, it only copies the changed blocks and bytes to the destination.
- It consumes less bandwidth as it uses compression and decompression method while sending and receiving data on both ends.
17 Different Rsync Commands To Use (Man, Backup, SSH and Directories)
sync options source destination
Common Rsync Arguments
- -r: copies data recursively.
- -v: This is for verbose output.
- -a: archive mode which allows copying files recursively. Moreover, it also preserves symbolic links, file permissions, user, and group.
- -z: Compress file data.
- -h: human-readable, it output numbers in a format which is read by humans.
Installing Rsync (Remote Sync) on Linux
Install this by using the following command
yum install rsync (For RedHat OS) apt-get install rsync (For Debian Distros)
Copy and syncing files or a directory (Local to Remote Site)
This shall sync directory from local machine to remote machine, like there is a folder in local computer “rpmpkgs” which carries some RPM packages and you want local directories content sent towards the remote server, use the following command:
[[email protected]] rsync -avz rpmpkgs/ [email protected]:/home/ sending incremental file list
READ ALSO: Top Xfce Dark Themes for Manjaro and Mint.
Copy or Sync a remote directory to local desktop
This shall help to sync the remote directory to the local directory. In the example below, a directory /home/shaheer/rpmpkgs, a remote server is brought copies in local systems in /tmp/myrpms.
[[email protected]] rsync -avzh [email protected]:/home/shaheer/rpmpkgs /tmp/myrpms receiving incremental file list created directory /tmp/myrpms
This shall sync a single file on a local machine from one location to another. In the example, a file name backup.tar needs to be copied or synced towards /tmp/backups/ folder.
[[email protected]] rsync -zvh yourfiles.tar /tmp/yourfiles/ created directory /tmp/yourfiles yourfiles.tar
The following command shall transfer or sync all files from one directory to a different directory in the same machine. In the examples, /root/rpmpkgs carries some rpm package files and you want that directory to be copied inside /tmp/backups/ folder.
[[email protected]] rsync -avzh /root/rpmpkgs /tmp/yourdekifiles/ sending incremental file list rpmpkgs/
Display Rsync Transfer Progress
We use –progress option to show progress while transferring data from one machine to another. It also displays files and time remaining to complete the transfer.
[[email protected]] rsync -avzhe ssh --progress /home/rpmpkgs
Rsync over SSH
With this, we can use SSH (Secure shell) for the transfer of data, using this protocol while transferring data we can be assured that data is being transferred in a secured connection with encryption so that no one can read data while it is transferred over wire on the internet.
Also when we use this, we need to provide user/root password to achieve a certain task, so using this option shall send logins in an encrypted manner so that passcode shall remain safe.
Backup files from a cloud server to a locally hosted server via SSH
To specify protocol with this you need to give –e option with protocol name you wish to use. In the example, we shall use ssh with –e option and perform data transfer.
[[email protected]]rsync -avzhe ssh [email protected]:/root/install.log /tmp/ [email protected]'s password:
Copy file from local server to a local remote server with SSH
[[email protected]] rsync -avzhe ssh backup.tar [email protected]:/dekibackup/ [email protected]'s password: sending incremental file list
If any file or directory does not exist at the source, but exits at the destination then you may want to delete that existing file or directory at a target while syncing.
-delete option can be used to delete a file that is not in the source directory.
Source and target are in sync, now creating new files test.txt at the target.
[[email protected]] touch dekisoftryncpost.txt [[email protected]] rsync -avz --delete [email protected]:/var/lib/rpm/
Target carries a new file known as test.txt when synched with the source with this option, it removed file dekisoftrysncpost.txt.
Set the max size of files to be first transferred
Users can specify max file size to be transferred or synced. This can be done with –max-size option. In the example, the maximum size is 200k so this command shall transfer only those files which are equal to or small than this.
[[email protected]] rsync -avzhe ssh --max-size='400k' /var/lib/rpm/ [email protected]:/root/tmprpm [email protected]'s password: sending incremental file list created directory /root/tmprpm ./
Do a dry run with Sync
If you are a new user and using this and do not know what your command is going to do. This can mess up things in your destination folder and then doing undo can be quite a tedious job.
Using this option shall not make any changes but only do a dry run of command and show the output of the command, if the output shows the same result you want to do then you can remove –dry-run option from the command and run on the terminal.
[email protected]$ rsync --dry-run --remove-source-files -zvh mybackup.tar /tmp/mybackup/ mybackup.tar
Conclusion: Which Rsync command amazes you?
A remote sync is a great tool that has made many system administrators’ life easier for creating backups and moving data between servers. This is why I have shared my expertise on the practical Rsync command examples that you can utilize for backups, use on SSH, moving and synchronizing directories. Fur further information you can refer to the official Man page.