rsync command examples

rsync stands for remote sync. rsync is typically used to synchronize files and directories between two different systems. rsync is used for fast and incremental file transfer. The rsync protocol is mainly used for backups, only transferring changed files to destination which has changed since the last backup.

rsync-command-examples

1) Install rsync

Use the below command to install rsync utility into Linux box.

# How to install rsync in CentOS/RHEL/Fedora #
[email protected] [~]# yum install rsync

# How to install rsync in Debian/ubuntu/linuxmint #
[email protected] [~]# apt-get install rsync

# rsync syntax #
[email protected] [~]# rsync [options] [source] [destination]

# Common option's for rsync #
-a : archive mode
-v : Verbose
-z : compress file data
-e : specify the ssh as remote shell
-r : recurse into directories
--delete : delete files that don't exist on sender (system)

2) copy/sync a file in local

The below example is sync a file locally from one location to another location. The cpmove-mageshco.tar.gz file has been copied from /home/2daygeek/ directory to /home/mageshm/ directory.

# sync file in local #
[email protected] [~]# rsync -zvr /home/2daygeek/cpmove-mageshco.tar.gz /home/mageshm/
sending incremental file list
cpmove-mageshco.tar.gz

sent 39554341 bytes  received 31 bytes  26369581.33 bytes/sec
total size is 39540978  speedup is 1.00

3) copy/sync a directory in local

The below example is sync a directory locally from one location to another location. The /home/2daygeek/ entire directory will be copied to /home/mageshm/ directory.

# sync folders in local #
[email protected] [~]# rsync -zvr /home/2daygeek/ /home/mageshm/
or
[email protected] [~]# rsync -avz /home/2daygeek/ /home/mageshm/
sending incremental file list
2daygeek.txt
cpmove-mageshco.tar.gz
test/
test/2daygeek-insdie.txt

sent 37912510 bytes  received 73 bytes  10832166.57 bytes/sec
total size is 39541296  speedup is 1.04

4) copy/sync a file & directory from local to remote

The below example is sync a file & directory from local server (Local server folder : /home/mageshco/public_html/rsync-magesh/) to remote server (Remote server folder : /home/mageshm/).

# sync folders in local #
[email protected] [~]# rsync -zvr /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
[email protected] [~]# rsync -avz /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
magesh.txt
test/magesh-inside.txt

sent 171 bytes  received 51 bytes  9.87 bytes/sec
total size is 0  speedup is 0.00

5) copy/sync a file & directory from remote to local

The below example is sync a file & directory from remote server (Remote server folder : /home/mageshm/) to local server (Local server folder : /home/mageshco/public_html/rsync-magesh/).

# sync folders in local #
[email protected] [~]# rsync -zvr [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
or
[email protected] [~]# rsync -avz [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
[email protected]'s password:
receiving incremental file list
.bash_logout
.bash_profile
.bashrc
magesh.txt
test/
test/magesh-inside.txt

sent 110 bytes  received 610 bytes  53.33 bytes/sec
total size is 318  speedup is 0.44

6) copy/sync a file & directory from local to remote over SSH

The below example is sync a file & directory from local server (Local server folder : /home/mageshco/public_html/rsync-magesh/) to remote server (Remote server folder : /home/mageshm/) with securly using SSH protocol. e option denote the SSH protocol.

# sync folders in local #
[email protected] [~]# rsync -zvre ssh /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
[email protected] [~]# rsync -avze ssh /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
test.txt
test/
test/test-inside.txt

sent 164 bytes  received 54 bytes  14.06 bytes/sec
total size is 0  speedup is 0.00

7) copy/sync a file & directory from remote to local over ssh

The below example is sync a file & directory from remote server (Remote server folder : /home/mageshm/) to local server (Local server folder : /home/mageshco/public_html/rsync-magesh/) with securly using SSH protocol. e option denote the SSH protocol.

# sync folders in local #
[email protected] [~]# rsync -zvre ssh [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
or
[email protected] [~]# rsync -avze ssh [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
[email protected]'s password:
receiving incremental file list
./
.bash_logout
.bash_profile
.bashrc
test.txt
test/
test/test-inside.txt

sent 113 bytes  received 663 bytes  31.67 bytes/sec
total size is 318  speedup is 0.41

8) copy/sync a file & directory from local to remote with Non-standard port

The below example is sync a file & directory from local server (Local server folder : /home/mageshm/) to remote server (Remote server folder : /home/mageshco/public_html/rsync-magesh/) over ssh with Non-standard port.

# sync folders in local #
[email protected] [~]# rsync -zvre "ssh -p 2200" /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
[email protected] [~]# rsync -avze "ssh -p 2200" /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
stdin: is not a tty
sending incremental file list
.bash_logout
.bash_profile
.bashrc
test.txt
test/
test/test-inside.txt

sent 613 bytes  received 111 bytes  96.53 bytes/sec
total size is 318  speedup is 0.44

9) copy/sync a file & directory from remote to local with Non-standard port

The below example is sync a file & directory from remote server (Remote server folder :
/home/mageshco/public_html/rsync-magesh/)
to local server (Local server folder : /home/mageshm/) over SSH with Non-standard port.

# sync folders in local #
[email protected] [~]# rsync -zvre "ssh -p 2200" [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
or
[email protected] [~]# rsync -avze "ssh -p 2200" [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
[email protected]'s password:
stdin: is not a tty
receiving incremental file list
.bash_logout
.bash_profile
.bashrc
test.txt
test/
test/test-inside.txt

sent 128 bytes  received 309 bytes  17.84 bytes/sec
total size is 318  speedup is 0.73

10) copy/sync a file & directory from remote to local with progress

The below example is sync a file & directory from remote server to local server with file transfer speed and times.

# sync folders in local #
[email protected] [~]# rsync -zvre ssh --progress [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
or
[email protected] [~]# rsync -avze ssh --progress [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
[email protected]'s password:
receiving incremental file list
.bash_logout
          18 100%   17.58kB/s    0:00:00 (xfer#1, to-check=5/7)
.bash_profile
         176 100%  171.88kB/s    0:00:00 (xfer#2, to-check=4/7)
.bashrc
         124 100%  121.09kB/s    0:00:00 (xfer#3, to-check=3/7)
test.txt
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=2/7)
test/
test/test-inside.txt
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=0/7)

sent 110 bytes  received 594 bytes  56.32 bytes/sec
total size is 318  speedup is 0.45

11) copy/sync a file & directory from local to remote with –delete option

The below example is sync a file & directory from local server (Local server folder :
/home/mageshco/public_html/rsync-magesh/)
to remote server (Remote server folder : /home/mageshm/)
and delete files that don’t exist on sender (system).

# sync folders in local #
[email protected] [~]# rsync -zvre ssh --delete /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
[email protected] [~]# rsync -avze ssh --delete /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
deleting test.txt

sent 141 bytes  received 13 bytes  20.53 bytes/sec
total size is 318  speedup is 2.06

12) copy/sync a file & directory from local to remote with –include option

The below example is sync a file & directory from local server (Local server folder :
/home/mageshco/public_html/rsync-magesh/)
to remote server (Remote server folder : /home/mageshm/)
only certain types of files. In this example i’m going to sync/copy only .php files and exclude everything.

# sync folders in local #
[email protected] [~]# rsync -zvre ssh --include '*.php' --exclude '*' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
[email protected] [~]# rsync -avze ssh --include '*.php' --exclude '*' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
test.php
test1.php

sent 132 bytes  received 50 bytes  17.33 bytes/sec
total size is 0  speedup is 0.00

13) copy/sync a file & directory from local to remote with –exclude option

The below example is sync a file & directory from local server (Local server folder :
/home/mageshco/public_html/rsync-magesh/)
to remote server (Remote server folder : /home/mageshm/)
only certain types of files. In this example i’m going to sync/copy except .txt files and and include everything.

# sync folders in local #
[email protected] [~]# rsync -zvre ssh --exclude '*.txt' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
[email protected] [~]# rsync -avze ssh --exclude '*.txt' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
test.html
test.php
test1.php

sent 255 bytes  received 70 bytes  34.21 bytes/sec
total size is 318  speedup is 0.98

14) Do not modify the destination file

-u or –update to skip any files which exist on the destination and have a modified time that is newer than the source file. This option will help you when you are sync files from production servers to development servers. In this example i have added some contents into test.txt file at destination end and run the below command. I found that, that file didn’t modified after ran the below command.

# sync folders in local #
[email protected] [~]# rsync -zvre ssh --update /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
[email protected] [~]# rsync -avze ssh --update /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list

sent 190 bytes  received 13 bytes  23.88 bytes/sec

15) Set file size limit

You can specify the MAX file size limit while sync. So that, it will leave big (Like, zip and tar file) size files. It can be done by adding –max-size= into rsync command. Here i’m going to sync Max 100K files and it will sync 100K files also less then 100k files.

# sync folders in local #
[email protected] [~]# rsync -zvre ssh --max-size='100k' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
[email protected] [~]# rsync -avze ssh --max-size='100k' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
test.html
test.php
test.txt
test1.php

sent 334 bytes  received 95 bytes  24.51 bytes/sec
total size is 318  speedup is 0.74

16) rsync automation

We can automate the process by creating samll shell script with cronjob. Create a file called offsite_backup.sh wherever you want and add the below code. Make sure the the fill permission should be 755 so that it will be executable file.

# creating file #
[email protected] [~]# touch offsite_backup.sh

# Assign executable permission #
[email protected] [~]# chmod +x offsite_backup.sh

# shell script #
[email protected] [~]# nano offsite_backup.sh
#/bin/bash
rsync -zvre ssh /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/

# setting cronjob #
[email protected] [~]# crontab -e
0 18 * * * /home/mageshco/public_html/offsite_backup.sh

# Running the shell file for testing purpose #
[email protected] [~]# ./offsite_backup.sh
sending incremental file list
offsite_backup.sh
test.html
test.php
test.txt
test1.php

sent 498 bytes  received 108 bytes  71.29 bytes/sec
total size is 421  speedup is 0.69

For rsync, it asks for password while doing rsync from local to remote server, if you want to run rsync automatically, you should generate the public key to do it without entering password. For more details about public/private key generation & crontab.

Hope this article was useful for you. Kindly provide your valuable feedback/comments in the commenting section.

Stay tuned with us !!

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...