Last updated on February 24th, 2022 at 07:58 pm

rsync is a software application in Linux which synchronizes files and directories from one location to another while minimizing data transfer.
I am going to provide examples based on two folders random (source folder) and random_dest (destination folder) ,

How to recursively copy files ?

rsync -r -t -v /source_folder /destination_folder

Let us take an example I have 2 folders, and there are 10 files inside source folder (random). Destination folder (random_dest) is empty.

$ ls -rlt
total 8
drwxr-xr-x 2 root root 4096 Feb 24 23:26 random_dest
drwxr-xr-x 2 root root 4096 Feb 24 23:27 random
$ pwd
/root
[email protected]~/random$  ls -rlt
total 40
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile3
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile2
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile1
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile7
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile6
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile5
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile4
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile9
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile8
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile10
[email protected]~/random_dest# ls -rlt
total 0

Let us run the below command and see what happens

[email protected]$ rsync -r -t -v random random_dest
sending incremental file list
random/
random/myfile1
random/myfile10
random/myfile2
random/myfile3
random/myfile4
random/myfile5
random/myfile6
random/myfile7
random/myfile8
random/myfile9

sent 11,359 bytes  received 210 bytes  23,138.00 bytes/sec
total size is 10,730  speedup is 0.93
[email protected]~$  cd random_dest/
[email protected]~/random_dest$   ls -rlt
total 40
-rw-r--r-- 1 root root 1073 Feb 24 23:30 myfile9
-rw-r--r-- 1 root root 1073 Feb 24 23:30 myfile8
-rw-r--r-- 1 root root 1073 Feb 24 23:30 myfile7
-rw-r--r-- 1 root root 1073 Feb 24 23:30 myfile6
-rw-r--r-- 1 root root 1073 Feb 24 23:30 myfile5
-rw-r--r-- 1 root root 1073 Feb 24 23:30 myfile4
-rw-r--r-- 1 root root 1073 Feb 24 23:30 myfile3
-rw-r--r-- 1 root root 1073 Feb 24 23:30 myfile2
-rw-r--r-- 1 root root 1073 Feb 24 23:30 myfile10
-rw-r--r-- 1 root root 1073 Feb 24 23:30 myfile1
[email protected]~/random_dest$  

Note that the above command will also overwrites existing files(if any) on the destination_folder (random_dest) having the same name as files in source_folder (random).

The -r option allows for recursion into subfolders.

If you want to preserve newer existing files from overwriting continue reading..

How to copy files without overwriting?

rsync -r -t -v -u /source_folder /destination_folder

[email protected]~/$  ls -l random/myfile1
-rw-r--r-- 1 root root 1073 Feb 24 23:27 random/myfile1
[email protected]~/$ ls -l random_dest/myfile1
-rw-r--r-- 1 root root 1073 Feb 24 23:30 random_dest/myfile1
[email protected]~/$ rsync -r -t -v -u random/ random_dest/
sending incremental file list
./

sent 184 bytes  received 19 bytes  406.00 bytes/sec
total size is 10,730  speedup is 52.86
[email protected]~/$  ls -l random/myfile1
-rw-r--r-- 1 root root 1073 Feb 24 23:27 random/myfile1
[email protected]~/$  ls -l random_dest/myfile1
-rw-r--r-- 1 root root 1073 Feb 24 23:30 random_dest/myfile1
[email protected]~/$

Please note the timestamp of file myfile1 in both directories. When I ran the rsync command with -u option it didn’t overwrite any file

How to take backup of files (add suffix) before overwriting them?

If you would like to automatically backup existing files before overwriting them, use the command below

rsync -r -t -v -b /source_folder /destination_folder

You can also add any suffix you like to append along with the backup file name. Let us take an example in which I perform a regular rsync command and files got copied. Now I am modified the content of one of the file named myfile9 inside directory random and also added an option –suffix _bkp to the command.

As you can see below rsync created a backup with the suffix _bkp which we provided in the command and then updated the file myfile9

[email protected]~/$ cat random/myfile9
Testt
[email protected]~/$ rsync -r -t -v -b --suffix _bkp random/ random_dest/
sending incremental file list
myfile9

sent 246 bytes  received 35 bytes  562.00 bytes/sec
total size is 8,600  speedup is 30.60
[email protected]~/$ cd random_dest/
[email protected]~/random_dest# ls -rlt
total 44
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile3
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile2
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile1
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile7
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile6
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile5
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile4
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile9_bkp
-rw-r--r-- 1 root root 1073 Feb 24 23:27 myfile8
-rw-r--r-- 1 root root   10 Feb 25 01:04 myfile10
-rw-r--r-- 1 root root    6 Feb 25 01:08 myfile9
[email protected]~/random_dest#

How to add Progress bar in rsync?

Here I have taken the same folder as example but added 100 files inside random to show the demo. You need to basically add the option –info=progress2 to see the progress.

[email protected]~$ rsync -r -t  --info=progress2 random/ random_dest/
     10,766,600 100%  129.58MB/s    0:00:00 (xfr#100, to-chk=0/101)
[email protected]~$

You might have noticed xfr#100 in the output, it means 100 files transferred.

Quick reference of options available in RSYNC command.

 -v, --verbose increase verbosity
 -q, --quiet decrease verbosity
 -c, --checksum always checksum
 -a, --archive archive mode. It is a quick way of saying you want recursion and want to preserve everything.
 -r, --recursive recurse into directories
 -R, --relative use relative path names
 -u, --update update only (don't overwrite newer files)
 -t, --times preserve times
 -n, --dry-run show what would have been transferred
 -W, --whole-file copy whole files, no incremental checks
 -I, --ignore-times Normally rsync will skip any files that are already the same length and have the same time-stamp. This option turns off this behavior.
 --existing, only update files that already exist
 --delete, delete files that don't exist on the sending side
 --delete-after, delete after transferring, not before
 --force, force deletion of directories even if not empty
 -c, --checksum always checksum
 --size-only, only use file size when determining if a file should be transferred
 --progress, show progress during transfer
 -z, --compress compress file data
 --exclude=PATTERN, exclude files matching PATTERN
 --daemon, run as a rsync daemon
 --password-file=FILE, get password from FILE


Related Post

2 thoughts on “How to take backup using rsync with examples”
  1. thank you for your article,My problem has been resolved. 12.YES! I finally found this web page! I’ve been looking just for this article for so long!!

Leave a Reply

Your email address will not be published. Required fields are marked *