A quick introduction to rsyc

E

encryptedbytes

Guest
As a Linux noob, I recently discovered the rsync command and come to love it very quickly! Rsync stands for remote sync(hronization) and is typically used for backup operations in Linux by synchronizing files and directories from one location to another, either local or remote, in an effective way.

Rsync is fast! The first time you sync files, rsync replicates the entire contents of the source to the destination. Once the entire source has been replicated, subsequent transfers only sync data that has been changed, rather than copy everything over again.

Rsync allows data encryption using the SSH protocol, making is safe to use across public and other untrusted networks.

Rsync uses less bandwidth than conventional file copying mechanisms like cp and FTP by using compression and decompression of data block by block.

No special privileges are required to install and use rsync!

I've already implemented rsync in a simple bash script to backup the Linux-based file server on my home network, check it out:


Code:
#!/bin/bash

DEST1=/mnt/soho_storage/samba/shares/BACKUP_001
DEST2=/mnt/soho_storage/samba/shares/BACKUP_002

rsync -varhP --delete ./aud-books $DEST1
rsync -varhP --delete ./aud-misc $DEST1
rsync -varhP --delete ./aud-music $DEST1
rsync -varhP --delete ./docs-books $DEST1
rsync -varhP --delete ./docs-cards $DEST1
rsync -varhP --delete ./docs-comics $DEST1
rsync -varhP --delete ./docs-misc $DEST1
rsync -varhP --delete ./img-misc $DEST1
rsync -varhP --delete ./img-photos $DEST1
rsync -varhP --delete ./sft-apps $DEST1
rsync -varhP --delete ./sft-emu $DEST1
rsync -varhP --delete ./sft-games $DEST1
rsync -varhP --delete ./sft-misc $DEST1
rsync -varhP --delete ./sft-system $DEST1
rsync -varhP --delete ./vid-comedy $DEST1
rsync -varhP --delete ./vid-edu $DEST1
rsync -varhP --delete ./vid-home $DEST1
rsync -varhP --delete ./vid-misc $DEST1
rsync -varhP --delete ./vid-music $DEST1
rsync -varhP --delete ./vid-toons $DEST1
rsync -varhP --delete ./private $DEST1
rsync -varhP --delete ./_incoming $DEST1

rsync -varhP --delete ./vid-tv $DEST2
rsync -varhP --delete ./vid-movies $DEST2

The syntax for rsync (on this system at least) is:

Code:
Usage: rsync [OPTION]... SRC [SRC]... DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
  or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
  or   rsync [OPTION]... [USER@]HOST:SRC [DEST]
  or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
  or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect
to an rsync daemon, and require SRC or DEST to start with a module name.

Use rsync --help to get the proper syntax for your system.

The command I am using in my backup script, rsync -varhP --delete [SRC] [DEST] means "synchronize verbosely, in archive mode, recursively, with human readable numbers, preserving permissions, and deleting extraneous files in the destnation directory, the contents of [this directory] to [that directory]. I define the destination directories at the beginning of the script to make it easier to adjust them. Rather than edit every line of the script, I just edit the values of the variables at the top of the script.

Rsync can do a whole lot more than what you see here in my little script so check it out and harness the power of rsync!

Check out How to backup Linux? 15 rsync Command Examples for more on what you can do with the handy command.
 


Awesome! Thank you! I have nevr heard of the rsync command before. This looks exactly like something I would use when copying files from mounts! You are my hero!
 
We use rsync in our backups at our hosting company.. it's a huge script, but in reality it's just rsync grabbing changed files.
 

Members online


Top