How to Use rsync to Transfer and Synchronize Local and Remote Directories
Rsync (Remote Sync) is the most commonly used command for copying and synchronizing files and directories remotely as well as locally in Linux/Unix systems. With the help of rsync command, you can copy and synchronize your data remotely and locally across directories, across disks, and networks, perform data backups, and mirroring between two Linux machines.
Using the Rsync for Linux is somewhat tricky and you have to be very careful about commands. Otherwise, you will end up losing data and time.
Symbols are very important in rsync commands in Linux.
Some advantages and features of the Rsync command
- It efficiently copies and sync files to or from a remote system.
- Supports copying links, devices, owners, groups, and permissions.
- It’s faster than scp (Secure Copy) because rsync uses a remote-update protocol that allows transferring of just the differences between two sets of files. The first time, it copies the whole content of a file or a directory from source to destination but from next time, it copies only the changed blocks and bytes to the destination.
- Rsync consumes less bandwidth as it uses compression and decompression method while sending and receiving data at both ends.
How to Use Rsync to sync with Remote system
Here you need only two things to make your job easier. They are
- SSH connection
- Rsync installed on the local and remote system
Then your task will be much easier as you can easily sync files between remote and local systems.
There are two types of operations used in file transfer between the local and remote systems.
This function is used to sync the local file to the remote system, and the format for that command is
rsync -a ~/dir1 username@remote_host:destination_directory
The above command syncs dir1 from the local system to the remote system.
The PULL function syncs the remote directory to the local systems. The command for this operation is
rsync -a username@remote_host:/home/username/dir1 place_to_sync_on_local_machine
The above command copies the dir1 from the remote system to the local system.
Here we should always mention the source as the first argument and the destination as the second argument.
Some common options used with rsync commands
- -v : verbose
- -r : copies data recursively (but don’t preserve timestamps and permission while transferring data
- -a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships, and timestamps
- -z : compress file data
- -h : human-readable, output numbers in a human-readable format
- –P : It is the combination of –progress and –partial flags.
Let us see some flags which we can use here. They will be very helpful to you while you transfer the file.
### If you have large files that are not compressed, you can compress them while transferring using -z flag.
Here is the command for that.
rsync -az source destination
### Here is another useful flag -P. It is the combination of –progress and –partial flags.
The progress flags show you the status of the transfer and the partial flag will resume the interrupted file.
You can use the P flag with the above command like this
rsync -azP source destination
### Sometimes you want to stop syncing specific directories and files.
You can do that using the following rsync ignore folder command which excludes the list you mention while syncing.
rsync -a --exclude=pattern_to_exclude source destination
Here, you might be using different patterns to specify the exclusion.
You can override the options using “–include” flag. Here is the syntax for overriding.
rsync -a --exclude=pattern_to_exclude --include=pattern_to_include source destination
Now you have learned how to use the rsync to transfer the files and come to know some rsync options.
Rsync is very effective only if you know how to use that with its flags. You can also use that for the large backup process.
As I mentioned again, you have to care about using slash(/) to avoid confusion.
Based on: https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/