How to Backup Linux? 15 rsync Command Examples

by Sasikala on September 9, 2010

rsync stands for remote sync.

rsync is used to perform the backup operation in UNIX / Linux.

rsync utility is used to synchronize the files and directories from one location to another in an effective way. Backup location could be on local server or on remote server.

Important features of rsync

  • Speed: First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.
  • Security: rsync allows encryption of data using ssh protocol during transfer.
  • Less Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less compared to other file transfer protocols.
  • Privileges: No special privileges are required to install and execute rsync

Syntax

$ rsync options source destination

Source and destination could be either local or remote. In case of remote, specify the login name, remote server name and location.

Example 1. Synchronize Two Directories in a Local Server

To sync two directories in a local computer, use the following rsync -zvr command.

$ rsync -zvr /var/opt/installation/inventory/ /root/temp
building file list ... done
sva.xml
svB.xml
.
sent 26385 bytes  received 1098 bytes  54966.00 bytes/sec
total size is 44867  speedup is 1.63
$

In the above rsync example:

  • -z is to enable compression
  • -v verbose
  • -r indicates recursive

Now let us see the timestamp on one of the files that was copied from source to destination. As you see below, rsync didn’t preserve timestamps during sync.

$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml
-r--r--r-- 1 bin  bin  949 Jun 18  2009 /var/opt/installation/inventory/sva.xml
-r--r--r-- 1 root bin  949 Sep  2  2009 /root/temp/sva.xml

Example 2. Preserve timestamps during Sync using rsync -a

rsync option -a indicates archive mode. -a option does the following,

  • Recursive mode
  • Preserves symbolic links
  • Preserves permissions
  • Preserves timestamp
  • Preserves owner and group

Now, executing the same command provided in example 1 (But with the rsync option -a) as shown below:

$ rsync -azv /var/opt/installation/inventory/ /root/temp/
building file list ... done
./
sva.xml
svB.xml
.
sent 26499 bytes  received 1104 bytes  55206.00 bytes/sec
total size is 44867  speedup is 1.63
$

As you see below, rsync preserved timestamps during sync.

$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml
-r--r--r-- 1 root  bin  949 Jun 18  2009 /var/opt/installation/inventory/sva.xml
-r--r--r-- 1 root  bin  949 Jun 18  2009 /root/temp/sva.xml

Example 3. Synchronize Only One File

To copy only one file, specify the file name to rsync command, as shown below.

$ rsync -v /var/lib/rpm/Pubkeys /root/temp/
Pubkeys

sent 42 bytes  received 12380 bytes  3549.14 bytes/sec
total size is 12288  speedup is 0.99

Example 4. Synchronize Files From Local to Remote

rsync allows you to synchronize files/directories between the local and remote system.

$ rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/
Password:
building file list ... done
./
rpm/
rpm/Basenames
rpm/Conflictname

sent 15810261 bytes  received 412 bytes  2432411.23 bytes/sec
total size is 45305958  speedup is 2.87

While doing synchronization with the remote server, you need to specify username and ip-address of the remote server. You should also specify the destination directory on the remote server. The format is username@machinename:path

As you see above, it asks for password while doing rsync from local to remote server.

Sometimes you don’t want to enter the password while backing up files from local to remote server. For example, If you have a backup shell script, that copies files from local to remote server using rsync, you need the ability to rsync without having to enter the password.

To do that, setup ssh password less login as we explained earlier.

Example 5. Synchronize Files From Remote to Local

When you want to synchronize files from remote to local, specify remote path in source and local path in target as shown below.

$ rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames
.
sent 406 bytes  received 15810230 bytes  2432405.54 bytes/sec
total size is 45305958  speedup is 2.87

Example 6. Remote shell for Synchronization

rsync allows you to specify the remote shell which you want to use. You can use rsync ssh to enable the secured remote connection.

Use rsync -e ssh to specify which remote shell to use. In this case, rsync will use ssh.

$ rsync -avz -e ssh thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames

sent 406 bytes  received 15810230 bytes  2432405.54 bytes/sec
total size is 45305958  speedup is 2.87

Example 7. Do Not Overwrite the Modified Files at the Destination

In a typical sync situation, if a file is modified at the destination, we might not want to overwrite the file with the old file from the source.

Use rsync -u option to do exactly that. (i.e do not overwrite a file at the destination, if it is modified). In the following example, the file called Basenames is already modified at the destination. So, it will not be overwritten with rsync -u.

$ ls -l /root/temp/Basenames
total 39088
-rwxr-xr-x 1 root root        4096 Sep  2 11:35 Basenames

$ rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/

sent 122 bytes  received 505 bytes  114.00 bytes/sec
total size is 45305958  speedup is 72258.31

$ ls -lrt
total 39088
-rwxr-xr-x 1 root root        4096 Sep  2 11:35 Basenames

Example 8. Synchronize only the Directory Tree Structure (not the files)

Use rsync -d option to synchronize only directory tree from source to the destination. The below example, synchronize only directory tree in recursive manner, not the files in the directories.

$ rsync -v -d thegeekstuff@192.168.200.10:/var/lib/ .
Password:
receiving file list ... done
logrotate.status
CAM/
YaST2/
acpi/

sent 240 bytes  received 1830 bytes  318.46 bytes/sec
total size is 956  speedup is 0.46

Example 9. View the rsync Progress during Transfer

When you use rsync for backup, you might want to know the progress of the backup. i.e how many files are copies, at what rate it is copying the file, etc.

rsync –progress option displays detailed progress of rsync execution as shown below.

$ rsync -avz --progress thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ...
19 files to consider
./
Basenames
     5357568 100%   14.98MB/s    0:00:00 (xfer#1, to-check=17/19)
Conflictname
       12288 100%   35.09kB/s    0:00:00 (xfer#2, to-check=16/19)
.
.
.
sent 406 bytes  received 15810211 bytes  2108082.27 bytes/sec
total size is 45305958  speedup is 2.87

You can also use rsnapshot utility (that uses rsync) to backup local linux server, or backup remote linux server.

Example 10. Delete the Files Created at the Target

If a file is not present at the source, but present at the target, you might want to delete the file at the target during rsync.

In that case, use –delete option as shown below. rsync delete option deletes files that are not there in source directory.

# Source and target are in sync. Now creating new file at the target.
$ > new-file.txt

$ rsync -avz --delete thegeekstuff@192.168.200.10:/var/lib/rpm/ .
Password:
receiving file list ... done
deleting new-file.txt
./

sent 26 bytes  received 390 bytes  48.94 bytes/sec
total size is 45305958  speedup is 108908.55

Target has the new file called new-file.txt, when synchronize with the source with –delete option, it removed the file new-file.txt

Example 11. Do not Create New File at the Target

If you like, you can update (Sync) only the existing files at the target. In case source has new files, which is not there at the target, you can avoid creating these new files at the target. If you want this feature, use –existing option with rsync command.

First, add a new-file.txt at the source.

[/var/lib/rpm ]$ > new-file.txt

Next, execute the rsync from the target.

$ rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ .
root@192.168.1.2's password:
receiving file list ... done
./

sent 26 bytes  received 419 bytes  46.84 bytes/sec
total size is 88551424  speedup is 198991.96

If you see the above output, it didn’t receive the new file new-file.txt

Example 12. View the Changes Between Source and Destination

This option is useful to view the difference in the files or directories between source and destination.

At the source:

$ ls -l /var/lib/rpm
-rw-r--r-- 1 root root  5357568 2010-06-24 08:57 Basenames
-rw-r--r-- 1 root root    12288 2008-05-28 22:03 Conflictname
-rw-r--r-- 1 root root  1179648 2010-06-24 08:57 Dirnames

At the destination:

$ ls -l /root/temp
-rw-r--r-- 1 root root    12288 May 28  2008 Conflictname
-rw-r--r-- 1 bin  bin   1179648 Jun 24 05:27 Dirnames
-rw-r--r-- 1 root root        0 Sep  3 06:39 Basenames

In the above example, between the source and destination, there are two differences. First, owner and group of the file Dirname differs. Next, size differs for the file Basenames.

Now let us see how rsync displays this difference. -i option displays the item changes.

$ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
>f.st.... Basenames
.f....og. Dirnames

sent 48 bytes  received 2182544 bytes  291012.27 bytes/sec
total size is 45305958  speedup is 20.76

In the output it displays some 9 letters in front of the file name or directory name indicating the changes.

In our example, the letters in front of the Basenames (and Dirnames) says the following:

> specifies that a file is being transferred to the local host.
f represents that it is a file.
s represents size changes are there.
t represents timestamp changes are there.
o owner changed
g group changed.

Example 13. Include and Exclude Pattern during File Transfer

rsync allows you to give the pattern you want to include and exclude files or directories while doing synchronization.

$ rsync -avz --include 'P*' --exclude '*' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
./
Packages
Providename
Provideversion
Pubkeys

sent 129 bytes  received 10286798 bytes  2285983.78 bytes/sec
total size is 32768000  speedup is 3.19

In the above example, it includes only the files or directories starting with ‘P’ (using rsync include) and excludes all other files. (using rsync exclude ‘*’ )

Example 14. Do Not Transfer Large Files

You can tell rsync not to transfer files that are greater than a specific size using rsync –max-size option.

$ rsync -avz --max-size='100K' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
./
Conflictname
Group
Installtid
Name
Sha1header
Sigmd5
Triggername

sent 252 bytes  received 123081 bytes  18974.31 bytes/sec
total size is 45305958  speedup is 367.35

max-size=100K makes rsync to transfer only the files that are less than or equal to 100K. You can indicate M for megabytes and G for gigabytes.

Example 15. Transfer the Whole File

One of the main feature of rsync is that it transfers only the changed block to the destination, instead of sending the whole file.

If network bandwidth is not an issue for you (but CPU is), you can transfer the whole file, using rsync -W option. This will speed-up the rsync process, as it doesn’t have to perform the checksum at the source and destination.

#  rsync -avzW  thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp
Password:
receiving file list ... done
./
Basenames
Conflictname
Dirnames
Filemd5s
Group
Installtid
Name

sent 406 bytes  received 15810211 bytes  2874657.64 bytes/sec
total size is 45305958  speedup is 2.87

Additional rsync Tutorials


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 63 comments… read them below or add one }

1 diptanu paul September 9, 2010 at 3:48 am

Of the track I recently found one good option for backup (fresh install) of a system which contains lot of customised packages, we can use the “Ghost for linux”. This can be used to take a cold backup of entire system (including the customised packages + OS), which can used to redeploy the same setup in a diffrent server or be used in the same server incase of crash of the system.
This saves the pain of reinstalling the OS and the packages from the begining.

2 Gopinath September 9, 2010 at 11:38 am

Hi ,
Thanks Its very use full for me

3 mike September 9, 2010 at 3:47 pm

hi folks.

here are two items i always mention when teaching people about ‘rsync’.

i always test it first with a dry run (simulation). append –dry-run or -n to your options to see what files *would* be copied or deleted without actually doing the action.

also, including a trailing slash vs omitting it produces different results.

without the trailing slash…
rsync -avz 192.168.200.10:/tmp/dir .
would produce
dir/file01
dir/file02

whereas including the trailing slash…
rsync -avz 192.168.200.10:/tmp/dir/ .
would produce
file01
file02

keep up the great job on The Geek Stuff! thanks. -mike

4 LinuxGeek September 10, 2010 at 2:50 am

What’s the point of compressing data that is only copied from one directory to another on the same computer? (example 1)

5 Oso September 10, 2010 at 12:33 pm

EXCELLENT!!!

Mike comment too .)

6 SG September 13, 2010 at 8:59 am

Great tutorial Ramesh – thanks again for taking the time to put this together!

7 vasiauvi October 16, 2010 at 1:53 am

Hello,
nice tips!
How can I do an incremental backup? I have a folder and want to backup only the modified files…
Thank you!

8 Rajesh November 11, 2010 at 9:21 am

How to delete file at source after it is copied to destination

9 zeek December 25, 2010 at 2:50 pm

Sasikala –> Thanks, very helpful, off to try them out
Mike –> Thanks for your example in your comment (and the dry run option).
Happy Holidays.

10 ali January 20, 2011 at 3:29 pm

hi
my english isnt very well
when we remot to a server by ssh the first directory is the home directory of that user so when we write command like this
rsync -avz -e ssh alieblice@192.168.16.12:var/www home/backup

it says that thers isnt any var/www directory

my question is : how can i backup a folder out of the home directory ?

11 Cissy January 25, 2011 at 6:43 am

Well, thanks..
I have learned a lot.. They are useful.. lol

12 Carson January 28, 2011 at 1:04 am

Hi Ali,

Specify this, if you want to do something outside the relative path: rsync -avz -e ssh alieblice@192.168.16.12:/var/www /home/backup

Using absolute paths is a nice way to be 100% sure of what you are copying.

Regards,

Carson.

13 kaling February 3, 2011 at 5:52 am

hi all,
it backup with automatic in one server to another server without password How to anyone help!!!
advance thanks

with regards
Kaling…

14 sherwin March 16, 2011 at 7:42 pm

thank you very much! i have been trying to use rsync to one-way sync an itunes library from one mac to another machine. is there a way to get more details in to a log file? like date/timestamp? i’ve added >> /path/to/log.txt and it works well. just need to add date/timestamp. also would it be possible to get an email when rsync is done?

15 Gabriel SOE March 20, 2011 at 2:54 am

Thanks for a great tutorial. in depth and clear.

16 rakitha beminiwatttha March 29, 2011 at 12:34 pm

This is a good summary of rsync stuff that is important.
This was helpful for me!
keep posting stuff like these.

17 p.a.blok April 20, 2011 at 2:16 am

When using the -d option to copy Only the Directory Tree, not only the dir-tree gets copied. FILES in the top source dir also get copied!!

18 p.a.blok April 20, 2011 at 2:35 am

You had better control this with:
rsync -v -d –max-size=’10K’ ~/Music/ .

19 Pier July 12, 2011 at 2:31 pm

@diptanu paul

I know it is late!! but this is just to point out that in order to get homogenics workstations we do use at my company the mechanised installation through the Kickstart + PXE boot process. In my opinion it is a lot better than deploying HDD clones, as the KS process is not dependent to the hardware…

just my opinion and my experiences.

20 sunoop July 18, 2011 at 8:04 am

nice tutrl………………..thnks alot

21 TK Nallappan August 6, 2011 at 5:11 pm

Hi Ramesh,

Thanks for your awesome rsync article. I would like to include the 16 rsync example command here below.

Rsync with custom SSH port no :
==========================
# rsync -avz -e “ssh -p 2323″ nagios-plugins-1.4.1.tar.gz root@xx.xx.xx.xx:/root
root@xx.xx.xx.xx‘s password:
nagios-plugins-1.4.1.tar.gz

sent 967995 bytes received 42 bytes 45024.98 bytes/sec
total size is 967552 speedup is 1.00
==========================

22 kamal September 5, 2011 at 7:16 am

Realy it’s help me a lots . Thanks buddy.

23 Robbie October 11, 2011 at 5:13 pm

When running rsync between 2 local directories, you dont want to use -z (compress), it’ll just load up your processor and slow down the transfer.

24 debianuser October 20, 2011 at 2:13 pm

Thank’s for this very usefull article. Thank’s to you too TK Nallappan, for the ssh tips

25 dixhuit October 25, 2011 at 5:18 am

Very, very useful. I learnt so much, so quickly thanks to this post. Many thanks.

26 thomas November 8, 2011 at 3:14 am

Is it possible to tell rsync to copy only files belonging to a certain user ?
I have the problem that when I tell rsync to copy links I don’t want to end up
with a lot of files from other people (if the links point there).

27 Anonymous November 11, 2011 at 3:36 am

Your tutorials are easy to understand for me. Please Keep writing

28 G November 28, 2011 at 10:19 am

Very good article, and usefull topics.

U ROCK

29 jmazaredo December 21, 2011 at 7:18 pm

Very well written and easy to understand. A+

30 Gausstein February 7, 2012 at 10:16 am

Thanks!!!!

31 srdlr February 21, 2012 at 1:37 am

ssh: connect to host 192.168.3.6 port 22: Connection refused
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(601) [sender=3.0.8]

what is the problem with this?
THANK YOU!

32 Alieblice February 21, 2012 at 12:20 pm

Use ” -v ” to get more data

ynu can use up to 3 “v”

33 aditya March 13, 2012 at 6:38 am

really awesome. even offical site for rsync is not so clear. explained with examples really rocks.
Thanks.

34 skn March 18, 2012 at 9:12 pm

I want to transfer my output data files(vtk/png files) only from remote to local computer. What can I do in this regard?

35 gkontos April 20, 2012 at 9:01 am

Excellent tutorial!

36 siraj May 1, 2012 at 1:42 am

# rsync -avz administrator@10.120.1.1:/win7/a /home/siraj/
connection ssh port 22 refused
(i am getting an error when trying to copy from windows to linux
please help)

37 Joseph May 1, 2012 at 4:07 pm

Great article, and well explained.
I’d like to find some further information on the: “only sending the changed blocks”. I find this amazing in situations where there is a large file– such as a VM file.

38 Charlie May 6, 2012 at 12:33 am

Nice article, for bonus points: is there any way to capture all changed files and put them somewhere on each run? I’m using –backup-dir but that only retains one version of each file. Maybe the trick is to gzip that tree and erase it after each run, but I also want to set up so that only the original rsync command can be executed ( have to push from a shared host, want to secure the backup server better ). Basically looking for a version-control-like setup that captures most versions of files in development at least at the intervals I set.

Siraj, you can debug your connection using an interactive ssh. Best thing to do is to use certificates if you want an unattended backup, this isn’t covered in this article but search rsync ssh-keygen to find several good articles on doing those.

39 Amol Pujari May 17, 2012 at 11:11 pm

Can achieve the following …

updating of a single file locally, should update the remote copy instantly

40 somenath May 30, 2012 at 2:38 pm

very useful!!!!!!!!!!!!

41 Ram June 6, 2012 at 3:27 am

Hi, i am using rsync to backup from remote to local but in backup it not recognise the extension of file such as font that not have extension like .ttf etc. in backup copy not reconised as font but it seems a document and i cant use that font from backup. Is something special for risolve this in rsync? Thanks to all if someone can help me.

42 zero June 13, 2012 at 11:08 am

I have errors at server and can not update.

Repeat calls to rysnc to syncronize, but it deletes file on the destination folder. So an error can delete many gigs of already copied data the next time rsync is called.

43 Chandan Kumar August 1, 2012 at 1:29 am

Hi ,

its really very useful , please keep it up .. it help us a lot

Regards
Chandan Kumar

44 Gaurav September 26, 2012 at 5:29 am

Ramesh ,

Can you help me for one rsync scenario . if i removed some source files then how we can refelect this changes on destination using rsync. any help from you and others will be appriciated . Thanks in advance.

45 Joseph September 26, 2012 at 8:41 am

Guarav:
If you use the –delete option, it will delete the files on the target, causing a truly synchronized system. IE:

rsync -av –delete /path/to/source /path/to/destination

46 Gaurav October 9, 2012 at 2:32 am

Thanks,
Joseph for your help !!!

47 John December 29, 2012 at 1:06 am

Awesome info and example. I do one question about the -W option. I am rsync on a LAN network work as oppose to the over the wan, should I use the -W option? I don’t see much of -W option any of the other documentations and examples. Just curious. Looking to foward to hear from you.

48 Don Fink March 5, 2013 at 5:42 am

This is extremely useful. It provides all the information I have been researching for the past several days about syncing from my remote server back to my local machine.

Thanks,

49 chetan March 15, 2013 at 9:32 am

Can u have any steps to take full backup of OS and restore it on another server using live CD.

50 nareshtallet April 3, 2013 at 12:06 am

excellent geekss……good stufff..

51 feld May 23, 2013 at 10:43 am

Great guide. Useful e simple to read ! thanks a lot.

52 Francois Scheurer August 12, 2013 at 8:37 am

rsync can use hard links for create incremental backups, but even so some kind of backup rotation is needed to limit the required disk space. Check here.

10 backups gives usually 10 days or retention.
I writed a rsync shellscript with a non-linear rotation to extend this retention.
10 backups is then enough to retain 1 year of the backups, e.g. with daily snapshots 1, 2, 3, 4, 6, 10, 18, 50, 114, 242.

53 naveen September 2, 2013 at 1:15 am

good

54 Frank Kim September 2, 2013 at 8:21 pm

Nice article! Very helpful. :)

55 Mohan September 3, 2013 at 7:21 am

Thank you Mr.Ramesh.

56 Mark September 18, 2013 at 1:04 pm

I’ve been using RSYNC (actually GRSYNC) to backup my entire Home directory to an external harddrive for years. Then when it comes time to upgrade to the next release, I tend to install new (instead of allow system to update to the next release) and have used my saved files on my external harddrive to recover my data. It has worked great. The first copy will take awhile, dependent upon the number and size of your files, but after that it will look for what has changed and only copy of those files. Very quick.

57 Disco Babu November 13, 2013 at 4:55 am

Very nicely done

58 Oliver Nicholson March 8, 2014 at 6:33 pm

Hi there,
Thanks for the great info! Just wanted to mention though, and correct me if I’m wrong, but I think you’ve got the ls -l examples of backup preserving or not-preserving timestamps the wrong way round.
Cheers again,
Ollie.
************************

59 safeer May 9, 2014 at 10:22 am

Very nicely documented..
Thanks Ramesh for this…

60 Chang May 29, 2014 at 1:01 am

Very nice description. Thanks a bunch buddy!

61 Anonymous June 12, 2014 at 6:20 am

how to transfer only one particular file when we want it to be transferred to remote system. Can’t use scp/sftp

62 Laith Al Obaidy September 3, 2014 at 12:10 pm

I still can’t see that big difference between rsync and scp!

63 George F. Rice November 8, 2014 at 7:33 am

Very clearly written, thanks much!

Leave a Comment

Previous post:

Next post: