Sysresccd-manual-en Backup and transfer your data using rsync
Rsync is an open source file synchronization program. Designed to maintain a mirror of a directory, it is a very advanced tool that can be used to make backups, or to copy files to another disk or host in the event of a hardware or software catastrophe.
The advantages of rsync over alternate transfer methods include:
- efficiency: if a version of a file already exists at the destination, only changed portions are transferred. This significantly reduces resource requirements and elapsed time especially with large files. It uses an intelligent algorithm that detects redundancies which minimizes network load.
- robustness: in environments with marginal connections or random hardware failures (especially disks that become non-operational after heating up) , rsync can resume transfers that were interrupted
- flexibility: options include exclusion lists, "dry run" to verify options, tuning of compares to detect changes, various methods to handle links and more
- runs on Linux, Unix and under MS Windows with cygwin.
Rsync has few drawbacks:
- Mostly notable, since it is so flexible that there are many options, some of which are complementary, some are contradictory. The full documentation for Rsync is extensive and should be read several times.
- The destination filesystem must support the attributes. With archiving, the attributes are stored in the archive, and the archive can be on any filesystem.
Rsync is provided with SystemRescueCd and this documentation is intended to provide useful information and examples for use during the recovery process. After understanding these you will find rsync useful to increase system reliability during normal operations. See "Using rsync regularly to minimize the impact of a catastrophe"
rsync can be run as server (when started with --daemon) or as client to make copies of files on a local machine or across the network to another host. The recommended options include:
--progressdisplays activity for monitoring progress
--archivepreserve basic attributes (permissions, owner, type [ symbolic link will be preserved as a link]). Be very careful with the trailing slash.
--xattrspreserve eXtended attributes
--aclspreserve Access Control Lists
--hard-linkspreserve hard links (don't treat them as files)
--compressin flight data ( no effect on destination)
file copy on a local machine
Only one rsync process is involved for local backups. Here is an example which preserves all attributes (including xtended ones) and ACLs:
rsync --archive --xattrs --acls --hard-links --progress --compress \ /home/mydir/data/ /backups/data-20080810/
remote backup in standalone mode
If rsync is installed as a daemon (listening on port tcp/873) on example.com, the client running at myhost can either:
- pull files from the daemon
- push files to the daemon.
Here is an example having the client can push a directory to a remote host (preserving attributes, ACLs and hard links (using single letter options):
rsync -aXAHz --progress /home/mydata/ example.com::mybackups/data-20080810/
The client could also download the files from the remote hosts:
rsync -aXAHz --progress example.com::mybackups/data-20080810/ /home/mydata/
Notice two colons between the remote host and the path.
remote backup over ssh
If the port tcp/873 is blocked (by a firewall), use rsync through ssh by specifying
-e on the client side .
Here is an example where the clients send to the remote host:
rsync -aAXHz --progress -e ssh /home/mydata/ remote.com:/backups/data-20080810/
rsync -aAXHz --progress -e ssh remote.com:/backups/data-20080810/ /home/mydata/
Observe only one colon between the remote host address and the remote path.
Saving files with interruptions
When recovering large amount of data, either large files, or thousands of small file, using scp, ftp or http lacks a means of resuming a interrupted transfer. Manually restarting the transfer is awkward. If the process was interrupted at 99% of a large file, the transfer must be restarted at the beginning, all the data transmitted again and may mean the transfer never completes. Even with
wget -c a corrupt file may result.
This is also important in those cases where the hard disk only operates while cool. rsync can handle this well by saving files as long as the disk is operating. After the disk heats up and becomes un-responsive rsync will abort. Power off the system and wait an hour or 2 for the disk to cool down. Power up and rsync can continue saving files where it left off.
Transferring thousands of small files
Only the files which are not current at the destination will be transferred. By first comparing the source and the destination files only the ones which are different will be transfered. This facilitates the continuation after an interruption and reduces elapsed time and reduces bandwidth requirements for remote transfers.
Transferring files with marginal connections
Rsync is able to resume transfers of files if the connection is lost by using
‑‑inplace. This is especially important with large files.
--partial causes partially transferred files to be retained. By default rsync removes a partial file when interrupted. If the connection is lost rsync will stop. When the connection is reestablished and rsync resumes, data will need to be retransferred as rsync will start from the beginning of the file.
--inplace causes rsync to use original file name at the destination. By default rsync transfers data to files with a temp name during the transfer and renames the files once the file is complete. Without
--inplace, on restart rsync will create new temp files and must retransfer the file from the beginning.
-u --update must NOT used with
--inplace as the partial destination file would be considered the same as than the original file and skipped.
Rsync uses a very efficient algorithm to compare the source file with the destination file and only transfers the different parts of the file. To transfer several versions of the same files on a regular basis, copy the old version (that you have already transferred) to the new destination file, and rsync will skip all the common parts.
WIth these options, rsync can be interrupted then resumed. Data which has been transferred is preserved.
Here is an example of good command to copy a directory with large files to a remote host:
rsync --archive --partial --inplace --progress --compress \ /home/bigfiles/ MYMIRROR::mybackups/bigfiles/
Check for updates to rsync at 
Useful rsync options
rsync has a lot of options. Here are just a few which apply to recovering a failed system.
-a, --archive: preserve basic attributes
Very important. Preserves files attributes (permissions, times and type ). As a result, a symbolic link will be copied as a link, without
--archive the contents of the source file are copied in the destination! N.B.: Hard links, extended attributes (xattr), and ACLs(Access Control Lists) are not preserved with this option, see
--progress: show progress during transfer
-v, --verbose: list files. Use twice for more details
-x, --one-file-system: don't cross filesystem boundaries
Process only one filesystem. Important when processing a filesystem (ex:
/ ) with mounted volumes. Default processes filesystems with mount points within the source specification.
-z, --compress: compress file data during the transfer
Rsync can compress the data that are transferred to reduce network activity. The destination file will be the same as the original. Use it for remote transfers when synching files which are uncompressed and will have a large compression ratio (eg: large text files, CD images or raw partition images). This option is not efficient on files which are already compressed: zip, gz, bz2, taz, jpeg, pdf, ... This will cause a significant increase in the CPU usage on both sourcing and destination systems.
--inplace and --partial
Use these options to transfer large files and insure that the transfer will resume at a restart point in case of connection failure. See the sections about Transferring large files for more details.
--exclude=pattern: exclude files matching pattern
Use this option to exclude files or directories from the transfer. For instance temporary files or cache directories.
Rsync return status
The return status must be checked to determine whether or not the transfer was successful. When rsync returns 0, it means that the transfer was successful. Any other value indicates an error.
Some errors which may indicate a recoverable problem include:
- 22: Error allocating core memory buffers
- 23: Partial transfer due to error
Install and configuring the rsync daemon
The rsync client will only transfer data to an rsync server.
Be sure to see Rsync documentation for all the options.
The configuration default location is either
/etc/rsync/rsyncd.conf. After changing the configuration, send a HUP signal using
kill -HUP <pid-of-rsync>.
Several options are available to restrict connections :
- require a password to connect
- allow only several specific IP addresses to connect
- provide read-only access to the client
Here is an example of basic secured configuration:
# ======================/etc/rsyncd.conf====================== pid file = /var/run/rsyncd.pid read only = yes uid = root gid = root [share1] path = /mnt/share1 read only = yes hosts allow = myhost, 10.88.45.0/24 [backups] path = /var/tmp/catalyst/tmp read only = no hosts allow = mybiggie, 10.88.45.0/24 [rootfs] path = / read only = yes hosts allow = myhost, 10.88.45.0/24 [upload] path = /upload read only = no hosts allow = 172.16.0.0/16
MS Windows and cygwin
The rsync daemon can be run on an MS Windows system by using cygwin which provides a linux environment. Minimal information is provided here to get you started.
To install cygwin, run the setup.exe program found on the website.
The hard disks as seen as
/cygdrive/d/, ... .
Click on the cygwin icon to run a bash shell. Here is the command to use:
cygrunsrv.exe -I "cygrsyncd" -p /usr/bin/rsync.exe -a "--config=/etc/rsyncd.conf --daemon --no-detach"
Then, start the new service run
services.msc and start the service named "cygrsyncd"
Using rsync regularly to minimize the impact of a catastrophe
The previous sections of this document focus on the features and operation of rsync to help recover a system.
Here we present additional information we thought would be useful.
rsync is a utility that maintains a mirror of a filesystem. Updated regularly, the mirror can be a major asset for recovering a failed system.
Here are some addition notes for using rsync:
- rsync can make remote backups of your data files on windows, or it can replicate windows system backups created via ntbackup.
- Combined with LVM snapshots to make online backups which are consistent.
- Various backup strategies: full, differential backups, incrementals.
Backups can save only the files which have been modified since the previous backup.
- run rsync on a server every night to make a backups of its root file system. rsync will probably complain about files which have been deleted during the transfer (like temporary files).
consider the return status 23 and 24 as warnings on a live system
The rsync daemon can be installed in a dedicated listen mode (tcp port 873). Having xinetd configured to listen on the behalf of rsync uses less memory and is more secure. It also means rsync is started and the configuration file read on each connection so changes will effected on the next connection.
rsync traps intr so pressing ^C stops when the current file is completed. Use the less common quit ^\ to have it quit now.
Additional useful options:
--delete: remove deleted files from destination
When synchronizing to an existing destination, files in the destination directory will be removed if they are not in the source directory ( The files have been deleted ). By default files no longer in the source remain in the destination. Consider the scenario in which files are versioned by date and are deleted as a new one is created (
/transfers/SUBMITTED-2010_07_05.input ). The default will not remove old versions and they will accumulate. This will cause the destination to not be a "mirror" of the source and the space required for the destination will increase with each run. Processing a single run with this option will delete all accumulated files.
-u, --update: skip files that are newer on the destination
Defines this run to be an update of an existing destination. Use this option if files have been modified in the destination directory. For instance, if you are migrating data from an old server to a new one, and if people have already started working on the new server. Use
--update to prevent overwriting changes with an older version.
--remove-source-files: sender removes synchronized files (non-dir)
Move data to the destination. By default, rsync makes a copy leaving the original intact. With
--remove-source-files, the source files to be deleted if the transfer was successful.
-c, --checksum: compare based on checksum, not just modification-time & size
rsync uses the file modification-time and size to make the comparison when updating existing directories. A file that exists in the destination directory with the same name, date and size is considered to be the same. This is accurate in nearly all cases. The comparison is very quick because only the file attributes are read. In rare cases this is incorrect. There are some utilities which modify a file and retain the original modification time (the graphic editor irfanview provides an option to do this). If the modification does not change the size of the file (very rare) rsync will erroneously consider the source and destination files to be identical and not transfer the file. Specify
--checksum to force rsync to generate a checksum of the files and compare the checksum to determine if the files are the same. Be warned that this will add significant CPU time and I/O activity at both the source and destination since the entire contents of both files must be read to generate the checksum. The elapsed time will significantly increase when using --checksum. An occasional run, immediately after a successful backup, with --checksum is useful in locating directories containing files in which the default comparison is incorrect. As a secondary run, few files should be backed up and if run a with a nice priority and a low ionice priority on both the client and server the impact of the increased CPU utilization can be minimized. Analysis of the files transferred in this run will revel directories which regularly require a separate job to correctly backup subtle changes.
With the default behavior (comparison based on modification-time and size) interrupting the transfer and resuming will skip all the files which have already been copied,i.e. rsync will resume the transfer where it left off.
Again, refer to Rsync documentation
for the most current and complete information.