Difference between revisions of "Rsync"

From ArchWiki
Jump to: navigation, search
(Added method to clone a running system with rsync)
(Undo revision 292748 by FSMaxB (talk) Sorry, I didn't see that there's already an entire article for that)
Line 180: Line 180:
To make things really, really simple this script can be run from a systemd unit.
To make things really, really simple this script can be run from a systemd unit.
==== Cloning a running Archlinux installation ====
With rsync it is possible to clone a running archlinux to another partition. This can be useful if you want to move the system to another storage device with minimal downtime, eg. from a hdd to a newly installed ssd. The only thing you have to do to is mounting your new root partition ( to {{ic|/mnt}} for example ) and copy your system with the following command:
# rsync --delete -avHAXx / /mnt
You can run this command multiple times if you have changed anything important in your running system while it was being cloned that you also want to be copied.
After the cloning is finished you have to update {{ic|/mnt/etc/fstab}} according to your new system. After chrooting to your cloned system ( with UEFI don't forget to bind-mount your UEFI system partition to the cloned system ), you have to update / reinstall the bootloader and update the initrd using the mkinitcpio command ( only necessary if the filesystem changed ).
Now reboot into your cloned system.
== Graphical frontends ==
== Graphical frontends ==
[[pacman|Install]] {{Pkg|grsync}} from the [[official repositories]].
[[pacman|Install]] {{Pkg|grsync}} from the [[official repositories]].

Revision as of 20:47, 13 January 2014

rsync is an open source utility that provides fast incremental file transfer.


Install the rsync from the official repositories.


For more examples, search the Community Contributions and General Programming forums.

As a cp alternative

rsync can be used as an advanced alternative for the cp command, especially for copying larger files:

$ rsync -P source destination

The -P option is the same as --partial --progress, which keeps partially transferred files and shows a progress bar during transfer.

You may want to use the -r --recursive option to recurse into directories, or the -R option for using relative path names (recreating entire folder hierarchy on the destination folder).

As a backup utility

The rsync protocol can easily be used for backups, only transferring files that have changed since the last backup. This section describes a very simple scheduled backup script using rsync, typically used for copying to removable media. For a more thorough example and additional options required to preserve some system files, see Full System Backup with rsync.

Automated backup

For the sake of this example, the script is created in the /etc/cron.daily directory, and will be run on a daily basis if a cron daemon is installed and properly configured. Configuring and using cron is outside the scope of this article.

First, create a script containing the appropriate command options:

rsync -a --delete /folder/to/backup /location/to/backup &> /dev/null
indicates that files should be archived, meaning that most of their characteristics are preserved (but not ACLs, hard links or extended attributes such as capabilities)
means files deleted on the source are to be deleted on the backup as well

Here, /folder/to/backup should be changed to what needs to be backed-up (/home, for example) and /location/to/backup is where the backup should be saved (/media/disk, for instance).

Finally, the script must be executable:

# chmod +x /etc/cron.daily/rsync.backup

Automated backup with SSH

If backing-up to a remote host using SSH, use this script instead:

rsync -a --delete -e ssh /folder/to/backup remoteuser@remotehost:/location/to/backup &> /dev/null
-e ssh 
tells rsync to use SSH
is the user on the host remotehost
groups all these options -rlptgoD (recursive, links, perms, times, group, owner, devices)

Automated backup with NetworkManager

This script starts a backup when you plugin your wire.

First, create a script containing the appropriate command options:


if [ x"$2" = "xup" ] ; then
        rsync --force --ignore-errors -a --delete --bwlimit=2000 --files-from=files.rsync /folder/to/backup /location/to/backup
group all this options -rlptgoD recursive, links, perms, times, group, owner, devices
read the relative path of /folder/to/backup from this file
limit I/O bandwidth; KBytes per second

Automated backup with systemd and inotify

  • Due to the limitations of inotify and systemd (see this question and answer), recursive filesystem monitoring is not possible. Although you can watch a directory and its contents, it will not recurse into subdirectories and watch the contents of them; you must explicitly specify every directory to watch, even if that directory is a child of an already watched directory.
  • This setup is based on a systemd/User instance.

Instead of running time interval backups with time based schedules, such as those implemented in cron, it is possible to run a backup every time one of the files you are backing up changes. systemd.path units use inotify to monitor the filesystem, and can be used in conjunction with systemd.service files to start any process (in this case your rsync backup) based on a filesystem event.

First, create the systemd.path file that will monitor the files you are backing up:

Description=Checks if paths that are currently being backed up have changed



Then create a systemd.service file that will be activated when it detects a change. By default a service file of the same name as the path unit (in this case backup.path) will be activated, except with the .service extension instead of .path (in this case backup.service).

Note: If you need to run multiple rsync commands, use Type=oneshot. This allows you to specify multiple ExecStart= parameters, one for each rsync command, that will be executed. Alternatively, you can simply write a script to perform all of your backups, just like cron scripts.
Description=Backs up files

ExecStart=/usr/bin/rsync %h/./documents %h/./music -CERrltm --delete ubuntu:

Now all you have to do is start/enable backup.path like a normal systemd service and it will start monitoring file changes and automatically starting backup.service:

systemctl --user start backup.path
systemctl --user enable backup.path

Differential backup on a week

This is a useful option of rsync, creating a full backup and a differential backup for each day of a week.

First, create a script containing the appropriate command options:


DAY=$(date +%A)

if [ -e /location/to/backup/incr/$DAY ] ; then
  rm -fr /location/to/backup/incr/$DAY

rsync -a --delete --inplace --backup --backup-dir=/location/to/backup/incr/$DAY /folder/to/backup/ /location/to/backup/full/ &> /dev/null
implies --partial update destination files in-place

Snapshot backup

The same idea can be used to maintain a tree of snapshots of your files. In other words, a directory with date-ordered copies of the files. The copies are made using hardlinks, which means that only files that did change will occupy space. Generally speaking, this is the idea behind Apple's TimeMachine.

This script implements a simple version of it:


## my own rsync-based snapshot-style backup procedure
## (cc) marcio rps AT gmail.com

# config vars

SRC="/home/username/files/" #dont forget trailing slash!
OPTS="-rltgoi --delay-updates --delete --chmod=a-w"

# run this process with real low priority

ionice -c 3 -p $$
renice +12  -p $$

# sync

rsync $OPTS $SRC $SNAP/latest >> $SNAP/rsync.log

# check if enough has changed and if so
# make a hardlinked copy named as the date

COUNT=$( wc -l $SNAP/rsync.log|cut -d" " -f1 )
if [ $COUNT -gt $MINCHANGES ] ; then
        DATETAG=$(date +%Y-%m-%d)
        if [ ! -e $SNAP/$DATETAG ] ; then
                cp -al $SNAP/latest $SNAP/$DATETAG
                chmod u+w $SNAP/$DATETAG
                mv $SNAP/rsync.log $SNAP/$DATETAG
               chmod u-w $SNAP/$DATETAG

To make things really, really simple this script can be run from a systemd unit.

Graphical frontends

Install grsync from the official repositories.