How to use rsync

Here's how I use rsync to keep the hard drive on my moble mp3 musicbox in sync with the hard drive on my home system.  The idea is that the music collection on the mobile system must be an exact copy of that on the home system.  rsync provides a really easy way to do that, despite its many many options and intimidating documentation.  It really is an easy-to-use backup tool once you figure it out.

I'll call the home system "homebox" and the mobile system "mobilebox".  I run rsync as a daemon (server) on the home system, and as a client on the mobile system, so I have mobilebox connecting to the server on homebox and downloading files from it (and also deleting any files from itself that have been deleted from homebox).  So I'm not syncing the entire hard drive, just the mp3 folder; however the principles and commands are the same if you'd like to sync an entire hard drive.

  1. First, make sure that rsync is installed on both homebox and mobilebox.

  2. On homebox, create the file /etc/rsyncd.conf and put this in it:

    log file =  /var/log/rsyncd.log
    pid file =  /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    
    [mymp3s]
      path = /music/music
      comment = My Master Music Directory
      uid = nobody
      gid = nobody
      read only = yes
      hosts allow = mobilebox
    


  3. On homebox, run rsync --daemon (might need to be root).

  4. On mobilebox, run rsync -a -v --progress --delete homebox::mymp3s /dest/on/mobilebox to perform the sync operation.

  5. Once it's done, run killall rsync on homebox, unless you want to keep that running for some reason.

Notes:

  • hosts allow is optional, there is also hosts deny, and both can contain hostnames, IP addresses, and wildcards.  Check the manpage for rsyncd.conf for all the details.

  • I set read only to yes because 1) I don't ever want the slave (mobilebox) to alter the contents of the master (homebox), and 2) I'm not using any sort of authentication here, so I don't want to allow remote modification of the master directory under any circumstances.

  • -a basically says "make the destination an exact copy of the source", -v enables verbose output, and --progress shows the files as it copies them.

  • While -a copies all new/changed files, it does not remove files from the destination that have been deleted from the source since the last sync.  That's what --delete is for.

  • On your first try, use the -n option too; this does a "dry run" which shows you all the operations it would perform, without actually performing any of them.

Verification

That's all there is to it.  After that completes, /dest/on/mobilebox will be an exact copy of /music/music on the homebox.  Use du -s /music/music and du -s /dest/on/mobilebox to see the sizes of the source and destination when you're done, to make sure they're the same.  You can also use ls -R1A /path|grep -P "\S"|grep -v ":"|wc -l to get an item count, which will include all files and directories:

  • R=recursive, 1=print one item per line, and A=show all files except . and ..

  • grep -P "\S" means count only lines that aren't blank

  • grep -v ":" means skip lines with colons in them (lines that ls prints as directory headings)

  • wc -l ("dash-L" not "dash-ONE") counts the lines instead of printing the lines

Other Ways to use Rsync

To transfer files from one Linux machine to another, you don't need to run rsync as a server one either of the machines.  You just need to have rsync installed on both, and something like an SSH server running on one of the machines.  Here's what I do -- run this on the source machine:

rsync -r -e 'ssh -p <portnum>' /source/dir user@dest:/dest/dir

That copies /source/dir recursively (-r) into /dest/dir on the machine "dest", and will prompt you to log in to the SSH server (running on portnum) on "dest" with the password for "user".