Tag Archives: rsync

Examples of using rsync

Some of the main features of rsync include

  • can update whole directory trees and filesystems
  • optionally preserves symbolic links, hard links, file ownership, permissions, devices and times
  • internal pipelining reduces latency for multiple files
  • can use rsh, ssh or direct sockets as the transport
  • checksum based verification

Preserving permissions, updating whole directory trees and secure transfers over ssh makes this the ideal backup tool. And it can be easily scheduled using cron. It’s also incredibly fast if you make us of the rsync daemon and not ssh.

This article will cover a few examples so that you’ll be able to quickly make use of the primary features of rsync.

Things to note when you use rsync

Copy the /home/temp folder to the remote-host

$ rsync -v /home/temp/ username@remote-host:/home/temp/

Copy the folder & the files within /home/temp to the remote host

$ rsync -v /home/temp/* username@remote-host:/home/temp/

Copy the folder & the files within /home/temp to the remote host. And recursively all the folders and files within /home/temp.

$ rsync -rv /home/temp/* username@remote-host:/home/temp/

Note: If you append the “-n” parameter rsync will simulate the operation you’re trying to do.

-n, --dry-run         perform a trial run with no changes made

All the examples were tested using the following version of rsync

rsync  version 3.0.7  protocol version 30
Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes

Example 1 – Backing up a folder and its sub-folders to a remote location

We do not preserve file permissions here. But we do use rsyncs ability to checksum to verify that the content that we copy are 1 to 1.

$ rsync -vrc /home/temp/ username@remote-host:/home/temp/

Parameters used in the above example

  • -v for verbose
  • -r for recursive
  • -c for skip based on checksum, not mod-time & size

You may also want to add information of your progress, keep partially uploaded files so they can be resumed if anything goes wrong and metric information that makes sense. Your desired command will then be as follows.

$ rsync -vrcPh /home/temp/ username@remote-host:/home/temp/
  • -P for progress during transfer and keep partially transferred files
  • -h for output numbers in a human-readable format

Example 2 – Backing up a folder and its sub-folders to a remote location preserving file permissions

This is a classical backup scenario. We’re keeping the ownership and file permissions. And copying this off to a remote location. We’re also not overwriting files that are newer on the receiver.

$ rsync -auvrc /home/temp/ username@remote-host:/home/temp/
  • -a for archive mode; equals -rlptgoD
  • -u for skip files that are newer on the receiver
  • -v for verbose
  • -r for recursive
  • -c for skip based on checksum, not mod-time & size

Example 3 – Copy folders to & from a local system

We do not preserve file permissions here. But we do use rsyncs ability to checksum to verify that the content that we copy are 1 to 1. This also serves as a way to test for faulty hard drives. We also for this enable the progress information and human-readable formats.

$ rsync -vrhc --progress  /home/importantfiles/ /mnt/externaldisk/backup_of_importantfiles/

Parameters used in the above example

  • -v for verbose
  • -r for recursive
  • -h for output numbers in a human-readable format
  • -c for skip based on checksum, not mod-time & size
  • –progress for show progress during transfer

Example 4 – View the changes between the source and destination system

To accomplish this we use the itemize-changes and recursive parameter

$ rsync -ri /home/temp/ username@remote-host:/home/temp/

You’ll then see something that could look like this

Parameters used in the above example

  • -r for recursive
  • -i for output a change-summary for all updates

It’s worth noting that “decoding” the results from rsync with “-i” requires knowledge about all the references. Those are very well documented in the man page under the “-i, –itemize-changes” section. You can also tweak the output using –out-format.

Example 5 – Backing up a folder and its sub-folders to a remote location with a bandwidth limitation

$ rsync -vrc --bwlimit=10000 /home/temp/ username@remote-host:/home/temp/
  • -v for verbose
  • -r for recursive
  • -c for skip based on checksum, not mod-time & size
  • –bwlimit=KBPS for limiting I/O bandwidth by KBytes per second

// CrashMAG