Using rsync with libguestfs

Since libguestfs 1.20 it has been possible to use rsync to upload or download files to a disk image incrementally. This is one way to do backups, but note that it won’t work on live guests unless you take a snapshot.

rsync involves using a network connection into or out of the appliance, and is therefore a lot more involved to set up. The script below shows one way to do this, by running an rsync daemon on the host, and letting the libguestfs appliance connect to it.

The script runs rsync inside the appliance, copying /home from the attached disk image out to /tmp/backup on the host. If the operation is repeated, then only incrementally changed files will be copied out. (To incrementally delete files on the target, add the deletedest:true flag).

Note you will need to open port 2999 on your host’s firewall for this to work.

#!/bin/bash -

set -x

# The target directory.
mkdir -p /tmp/backup

# Create the daemon.
rm -f /tmp/

cat <<EOF > /tmp/rsyncd.conf
port = 2999
pid file = /tmp/

  path = /tmp/backup
  use chroot = false
  read only = false

rsync --daemon --config=/tmp/rsyncd.conf

# Run guestfish and attach to the guest.
guestfish --ro --network -a /dev/fedora/f19rawhidex32 -i <<EOF
trace on
rsync-out /home rsync://rjones@ archive:true

# Kill the rsync daemon.
kill `cat /tmp/`


Filed under Uncategorized

8 responses to “Using rsync with libguestfs

  1. This is pretty cool, however I think it makes a lot more sense to have the host rsync in, to keep the logical flow of security… Eg: more secure things log into less secure things, and not vice versa.

    • rich

      Unfortunately the crappy qemu user networking that we have to use doesn’t allow incoming connections. To allow those would mean we have to run the appliance as root (not very sensible) or have some “fun” interactions with libvirt.

  2. Richard, what would the syntax be for a Windows guest? We’re trying to copy a few files continuously, as they are modified, from a live Windows guest using guestfs_rsync_out API. We have:

    guestfs_rsync_out(g, “c:\\HostShared” “rsync://root@”, -1)

    If we combine this with your rsync daemon approach, are we on the right track? Thanks.

  3. Rich-

    Thanks very much. We’ve tried combinations similar to:

    guestfs_rsync_out(g, “/HostShared”, “rsync://root@ -av”, -1)

    surroundng it with APIs as in the recipe examples, for example replacing libguestfs_cat(), with no luck. Typically in the rsync daemon log we see something like:

    receiving file list
    sent 17 bytes received 24 bytes total size 0

    but no data is transferred. We are successful using rsync from the Linux command line, and we can get libguestfs APIs like guestfs_cat() to work fine. But we’ve had zero luck with rsync_out over the last week. Unfortunately there are no actual API examples online, no “recipes” on the site, etc. Do you know where there are some actual libguestfs_rsync_out() working examples?



  4. Rich-

    Additional note… to match your example, the syntax we used is:

    guestfs_rsync_out(g, “/HostShared”, “rsync://root@ -av”, -1)

    where HostShared refers to a folder C:\HostShared on the Win7 guest.

  5. Ok saw your reply, have subscribed to the mailing list and will post there. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.