Rsync Command Tutorial With Examples
Rsync is popular tool used for copying, backup and synchrnizing. Rsync can work locally or remotely over network. Rsync can also be used to clone some site to multiple site. Popular backup tools like LuckyBackup also uses rsync in the background. We will look various examples of rsync in this tutorial. Here is some features of rsync
- Copying directories
- Backup options
- Rich feature options
- Dry run capability
- Run as Daemon or Server
- Works over ssh
- Permission retention
Syntax of rsync is like below.
rsync OPTIONS SOURCE DESTINATION
- OPTIONS used to change behavior of the rsync
- SOURCE is source of the data path
- DATA is destination of data path
Help With Man Page
There are a lot of options rsync command to get details about these following command can be used.
$ man rsync
Copy/Sync File Locally
We will copy files and directories locally. This is generally happen if we mounted a remote file system locally or mounted an external storage.
In this example we will sync file named
$ rsync -zvh tmux.tar.gz /home/ismail/backup/
Copy/Sync Directories Locally
Copying or synchronizing directories is a bit different from files. We will provide extra option -a like below. This copy operation will run recursively.
$ rsync -azvh test1 /home/ismail/backup/
Start Rsync Daemon, Service
In our examples we are using Ubuntu distribution but this will work other distributions as well. Rsync can be run as a daemon or service. Rsync daemon is called rsyncd and needs some configuration to start. Following simple configuration can be used as rsync configuration. Put following configuration to the /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
pid file = /var/run/rsyncd.pid
And run systemctl to start service.
$ sudo systemctl start rsync.service
And check the status of daemon
$ sudo systemctl status rsync.service
Copy/Sync File To A Server
Single file named tmux.tar.gz can be copied like below.
$ rsync -zvh tmux.tar.gz ubu1:/home/ismail/backup
Or remote destination host name is ubu1 we can also specify user named like below.
$ rsync -zvh tmux.tar.gz ismail@ubu1:/home/ismail/backup
Copy/Sync Directories To A Server
Directories can be copied to the remote server like below. But keep in mind that rsynd daemon user rights can prevent copying operation.
$ rsync -azvh test1 ismail@ubu1:/home/ismail/backup/
Copy/Sync File From A Server
We can use remote servers to get files to the local server. Generally used for restoring from backup.
$ rsync -zvh ubu1:/home/ismail/backup/tmux.tar.gz .
Copy/Sync Directories From A Server
We can also sync directories and files from remote servers. This operation is similar to the file operations. In this example we will sync remote server named
ubu1 with our local directory
test_1 . Remote directory is
$ rsync -azvh ubu1:/home/ismail/backup/test1 test_1
Transmission Over Ssh
Rsync daemon provides remote transmission of files but there is a problem. It is not so much secure because I transfer data as clear text. What is alternative? As we always use ssh is the solution. As we know ssh have tunneling ability is can transmit data over secured encrypted tunnels. We will provide -e ssh parameter to the rsync command. Other part of the rsync command is the same.
$ rsync -azvh -e ssh ubu1:/home/ismail/backup/test1 test_1
While transferring a lot of files knowing the status of transfer is very important. We can show progress of sync with –progress option like below.
$ rsync -azvh --progress ubu1:/home/ismail/backup/ tmux
Delete Nonexistent Files
On the sync operations the aim is to make source and destination directory the same. In some situations a file or folder do not exist in source but all ready exists in the destination. In this situation to sync both sides destination copy should be deleted. –delete options provides these mechanism to work.
$ rsync -avz --delete test1 test_1/
Set Max File Size To Transfer
Another important filter option is limiting maximum file size for transfer. We can set this filter by using –max-size option like below.
$ rsync -avz --max-size='1K' test_1 test_1.bak
Delete Source After Transfer
We have already looked to delete option which will delete destination files if they do not exists in the source. There is similar option to delete source files if they do not needed. For example we want to delete backup files and folders after sync operation because we do not need them. Here we can use –remove-source-files option t accomplish this.
$ rsync -avz --remove-source-files test_1 test_1.bak
Up to now after issuing command every change was real. In some situation we may want to only see what will happen with the command but do not want to start action we can use –dry-run option.
$ rsync -avz --dry-run test_1 test_1_back
Another useful option for rsync is limiting bandwidth usage. This is very important option because some networks have limited bandwidth. Enabling rsync can hurt other network traffic because of bulk transfer sizes. We can easily limit network traffic with –bwlimit option like below.
$ rsync -avz --bwlimit=500k test_1 ubu1:/home/ismail/web_backup
Create Log File
Rsync is generally used for non-interactive batch operations. During these operations there will be a lot of different actions or errors like, copying, deleting, permission error etc. To get information about these events logs should be saved and reviewed. Log file can be saved with –log-file option like below.
$ rsync -avz --log-file=back.log test_1 ubu1:/home/ismail/web_backup