Rsync (Remote Sync) is a command-line tool for synchronizing files between two Unix-based systems. Rsync can also be used on the same system to synchronize files between two directories. Rsync uses a delta-transfer algorithm to send only the differences between source files and existing files to the destination. Rsync uses less data on the network to conserve bandwidth.
Rsync is typically employed for backing up large amounts of data or transferring data between two computers. Rsync supports local-to-local, local-to-remote, and remote-to-local file syncing. However, remote-to-remote file syncing is not supported.
In this post, we will look at some common uses for the rsync command-line tool.
rsync [OPTION...] SOURCE... [DESTINATION]
- Here SOURCE can be a local directory like “/opt/backup” or a remote location like “[email protected]:/opt/remotedir/”
- Also the DESTINATION can be refer to a local directory or remote system directory
- Both SOURCE and DESTINATION can refer to a local directory.
- Only one SOURCE or DESTINATION can refer to the remote location. You can’t use both as a remote locations.
Rsync Command Options
Rsync offers a large number of useful options that control each and every aspect of its behavior and make it more flexible for synchronizing files. Below is the list of some frequently used Rsync command options:
-v, -vv -vvv, --verbose– This option is used for verbose logs on standard output. The default rsync runs silently. Higher the number of “v” increases the logging level.
-a, --archive– Is the most common used option with rsync command. The archive mode is equivalent to the options
-rlptgoD. Basically, it includes all necessary options like, recursively, preserving file permissions, symbolic links, file ownership, and timestamps.
-z, -–compress– Use this option to transfer data in compressed format. Which is useful to save bandwidth.
-h, –-human-readable– Use this option to print all the outputs in a human readable format.
--delete– Use this option only if you need to remove files on destination, which does not exists on source.
--exclude=PATTERN– Exclude files from rsync matching the given pattern.
rsync --exclude '*.jpg' SRC/ DEST/
--include=PATTERN– Include files in rsync matching the given pattern.
rsync --include '*.jpg' --include '*.txt' --exclude '*' SRC/ DEST/
Here are 12 practical examples of the Rsync command in Unix/Linux systems.
- Copy/Sync files and directories local-to-local
Rsync allows synchronizing files between directories on the local system. For example, I have a few backup files on my local machine under the /mnt/backup directory. I want to copy/sync all the files to the USB drive that is mounted on /mnt/disk1. Use the following command to synchronize all files and directories from /opt/backup to /mnt/disk1.
rsync -avhz /opt/backup /mnt/disk1/sending incremental file list backup/ backup/tecadmin.22Mar2022.sql backup/tecadmin.22Mar2022.tar.gz backup/tecadmin.23Mar2022.sql backup/tecadmin.23Mar2022.tar.gz sent 1.09G bytes received 96 bytes 23.39M bytes/sec total size is 1.47G speedup is 1.35
- Copy/Sync files and directories local-to-remote
Using rsync, we can quickly copy/synchronize files from local to remote systems. For example, I need to transfer a website to a new server. This required website content to be a copy of the remote systems. Use the below command to copy all files from the local /var/www/html directory to remote (192.168.1.100) systems /var/www/html directory.
rsync -avhz /var/www/html [email protected]:/var/www/html/
The above command is helpful for system administrator, who copies files between two systems on regular basis.
- Copy/Sync files and directories remote-to-local
The Rsync command also allows the source as a remote directory, but then the destination must be the local. Use the source directory from the remote host and the destination must be the local directory. For example, I need to copy all backup files stored under /backups directory on remote to the local /opt/backups directory.
rsync -avhz [email protected]:/backups /opt/backups
- Using Rsync over SSH
The Rsync connects to remote systems using the Rsync daemon directly via TCP. We can instruct rsync to use Secure shell (SSH) to connect remote systems rather than creating a TCP connection.
-eoption to specify the remote shell. The below command will use ssh as a remote shell:
rsync -avhz -e ssh /src [email protected]:/dest/
If the SSH server on destination is running on non-standard port use this tutorial to connect rsync over non-standard port.
- Ignore files and directories already exists on destination
Use the Rsync command with
--ignore-existing option to ignore all files, that already exist on the destination. For example, you want to schedule a backup script to copy all files to the backup disk daily. In that case, you don't want to copy all files again and again. This option will copy those files, that are not available at the destination.
rsync -avhz --ignore-existing /opt/backups /mnt/disk1
- Update file only if the source is newer
--updateoption to update file on remote only if there is a newer version is local. The Rsync will create a file on the destination if not exists. Also, the update file on the destination of a local timestamp is newer than the remote system file.
rsync --update -avhz /opt/backups /mnt/disk1/
- Remove file from source after sync
--remove-source-filesoption to remove file from source after successful transfer to the destination. For example, you need to schedule a backup of log files to the remote system. Also, wanted to remote log the file from the local system after successfully copied to the remote. This will help to claim disk space.
rsync --remove-source-files -avhz /var/log [email protected]:/backup/logs/
- Exclude specific file or directory with Rsync
You can specify a file or pattern with
--exclude=PATTERNoption to exclude from rsync.
rsync -avh --exclude '*.zip' --exclude '*.log' /src [email protected]:/dest
- Include Specific files with Rsync
The default Rsync includes all files under the source directory tree. Now, you need to include only specific files to synchronize to the destination.
Here you can specify
--include=PATTERNto include specific files and exclude all files with
rsync -avhz -include '*.jpg' --include '*.txt' --exclude '*' /src [email protected]:/dest
- Display Progress with Rsync
--progressoption to display progress during the file transfer between source and destination with Rsync command.
rsync -avh --progress /src [email protected]:/dest
- Chnage file permissions with Rsync
You can instruct the Rsync to change files owner and group owners on destination. Use option
--chown=USER:GROUPwith Rsync to change file permission.
rsync -avh --chown=USER:GROUP /src [email protected]:/dest
Similarly, you can use the --chmod=CHMOD option to change file or directory permissions on the destination.
rsync -avh --chmod=755 /src [email protected]:/dest
- Rsync dry run only
--dry-runoption to execute dry run only. It will show you similar results as the original command but nothing will update on the destination. Basically, we can use this to check, what the Rsync will update before actually running it.
rsync -avh --dry-run /src [email protected]:/dest
Rsync is a great utility for transferring large amounts of data between two machines. You may also use Rsync to back up the entire system at a remote backup location in this article. You've learned some practical Rsync command examples in this article that are great for daily tasks.