This article is part of the BackupYourSystem series. More introductory information can be found there.

Introduction

From the man page:

In other words, rsync is a tool for efficiently copying and backing up data from one location (the source) to another (the destination). It is efficient because it only transfers files which are different between the source and destination directories.

Rsync

Rsync is a command line utility. Users attempting to use it should be familiar with the command line (see Using the Terminal). If you prefer a graphical interface, see the Grsync section of this page.

Installation

Rsync is installed in Ubuntu by default. Be sure to check whether the following packages are installed before starting (see Installing a Package): rsync, xinetd, ssh.

Perform a Simple Backup

The simplest method for backing up over a network is to use rsync via SSH (using the -e ssh option). Alternatively, you can use the rsync daemon (see Rsync Daemon which requires much more configuration. Local backup only requires rsync and read/write access to the folders being synchronized. Below you will find examples of commands that can be used to backup in either case. It should be noted, that a network sync can be performed locally so long as the folder is shared (say by Samba) and then mounted to the machine with folder1. This process gets around having to use ssh but is less secure and should only be used in secure private networks, like at your home.

Local Backup

sudo rsync -azvv /home/path/folder1/ /home/path/folder2

Backup Over Network

sudo rsync --dry-run --delete -azvv -e ssh /home/path/folder1/ remoteuser@remotehost.remotedomain:/home/path/folder2

An explanation of above options to commands:

A complete synopsis of all the options with the rsync command can be found in the man pages under "Options Summary". The man page for rsync can also be found on linux.die.net

Grsync

Grsync is a GUI frontend for the rsync utility. The simple interface of the GUI exposes many of the basic options available with rsync. It is useful for those who prefer not to use the command line.

Installation

The program grsync does not come installed by default on Ubuntu or any other distrubtion but it is easily available from the main Repositories. To get grsync ensure Universe section of the Ubuntu repositories is enabled in your Software Sources. Then to install this software in Ubuntu, install the following package: grsync.

Configuration

To start up grsync go through the following menus: Applications --> System Tools --> grsync. Upon start up you'll be presented with the main window, where all the configuration takes place.

grsyn1.png

On this window are all of the options most users will ever need. To explain, the options will be listed and their effects mentioned.

grsyn2.png

Simulation and Execution

The last two buttons on the window are Simulation and Execute. The button for simulation is very useful when uncertain what will happen based on the options selected. The normal transfer dialog screen will pop up and in the main pane, a list of files that would have been copied over is listed. The user can then verify if this is as desired or make changes. Once the session is initiated with the Execute button, the dialog will appear again but this time it will actually process the folders accordingly. Ensure before pushing Execute that you are happy with the simulation.

Remote Backup

Backup over a network is possible, preferably the user should mount the network share to be backed up to prior to launching the program. The share would then be listed in the Browse GUI and could easily be added. There is no separate section for network, if more advanced features are required the user is encouraged to look at alternatives, of which there are many.

Alternatives

There are many alternatives, in various stages of development. For an incomplete list, see here.

Rsync Daemon

The rsync daemon is an alternative to SSH for remote backups. Although more difficult to configure, it does provide some benefits. For example, using SSH to make a remote backup of an entire system requires that the SSH daemon allow root login, which is considered a security risk. Using the rsync daemon allows for root login via SSH to be disabled.

Configuration of the rsync Daemon

1. Edit the file /etc/default/rsync to start rsync as daemon using xinetd. The entry listed below, should be changed from false to inetd.

RSYNC_ENABLE=inetd

2. Install xinetd because it's not installed by default.

$ sudo apt-get -y install xinetd

3. Create the file /etc/xinetd.d/rsync to launch rsync via xinetd. It should contain the following lines of text.

service rsync
{
    disable = no
    socket_type = stream
    wait = no
    user = root
    server = /usr/bin/rsync
    server_args = --daemon
    log_on_failure += USERID
    flags = IPv6
}

4. Create the file /etc/rsyncd.conf configuration for rsync in daemon mode. The file should contain the following. In the file, user should be replaced with the name of user on the remote machine being logged into.

max connections = 2
log file = /var/log/rsync.log
timeout = 300

[share]
comment = Public Share
path = /home/share
read only = no
list = yes
uid = nobody
gid = nogroup
auth users = user
secrets file = /etc/rsyncd.secrets

5. Create /etc/rsyncd.secrets for user's password. User should be the same as above, with password the one used to log into the remote machine as the indicated user.

$ sudo vim /etc/rsyncd.secrets 
user:password

6. This step sets the file permissions for rsyncd.secrets.

$ sudo chmod 600 /etc/rsyncd.secrets

7. Start/Restart xinetd

$ sudo /etc/init.d/xinetd restart

Testing

Run the following command to check if everything is ok. The output listed is just a sample, should be what is on your shared remote machine. Hostname can be replaced by the IP address of the machine.

$ sudo rsync user@hostname::share
  Password: 
  drwxr-xr-x        4096 2006/12/13 09:41:59 .
  drwxr-xr-x        4096 2006/11/23 18:00:03 folders

Backup With Rsync and Ssh

(scroll to bottom if you want a much less informative synopsis of what will be covered)

sudo apt-get install openssh-server

Next, we need to set up a key pair. You will receive a public key and private key.

ssh-keygen

You will be asked some questions, such as whether or not you want a password to the key pair, etc. I chose no and basically left everything else default. I went with no password because SSH keys are pretty -*- secure, and plus I wanted this to be automated. I was not sure how I could automate this process while still having a password on it.

ssh-copy-id jason@192.168.1.150

It'll ask you for your password. Put in your password to the user account you're authenticating against on the file server. Once done, you should be able to run:

ssh jason@192.168.1.150

If it did not ask for a password and your prompt changed, you're good to go. If it asked you for a password, something is likely off. Please note, if you mess around with the SSH keys (by deleting them, adding new ones, etc.) it'll require a reboot (some people have told me log out + log in works fine too) to reset. I don't know enough about that to explain what's happening besides taking the educated guess that the SSH key is getting locked to your session. Unless you plan to tinker around like I did, where I would delete the SSH keys and re-generate them over and over for learning purposes, you won't run into this issue. But if you do, I wanted to throw this out there.

Note - Personally, I would definitely recommend excluding .gvfs. .gvfs is the gnome virtual file system. It essentially acts as a mount point for network resources. Let's say your file server is accessible through .gvfs. If you rsync everything and don't exclude .gvfs, you're in essence duplicating the data on your file server that already exists, because it'll exist in its primary folder, as well as through .gvfs thanks to your file server.

jason@192.168.1.150:/media/NAS/jason

rsync -az /home/jason jason@192.168.1.150:/media/NAS/jason

If you want your entire home directory synchronized but with the exclusion of .gvfs and the --delete flag, use:

rsync -az --exclude=.gvfs --delete /home/jason jason@192.168.1.150:/media/NAS/jason

Getting the jist of it now?

rsync -az --exclude=.gvfs --delete /home/jason/Pictures /home/jason/Documents jason@192.168.1.150:/media/NAS/jason

You can then set up a Cron job for this to run at specific times. I never run rsync as root, so when I set it up in Cron I set it to launch as jason and just tagged the above rsync command in.

Summary

Server

sudo apt-get install openssh-server

Client

ssh-keygen

Client

ssh-copy-id jason@192.168.1.150

Client

rsync -az --exclude=.gvfs --delete /home/jason jason@192.168.1.150:/media/NAS/jason

Originally posted The Ubuntu Forums (ubuntuforums.org)


CategoryBackupRecovery CategoryCommandLine

rsync (last edited 2012-09-05 19:05:54 by mail)