Difference between revisions of "Rsync"

From Organic Design wiki
m
(maildirs)
Line 22: Line 22:
 
For more security, the command allowed can be restricted to just that specific rsync command. This can be done by manually running the rsync command with the ''-e'ssh -v' ''option which will output the exact command sent that can be used in the remote hosts ''authorized_keys'' file instead of just "rsync".
 
For more security, the command allowed can be restricted to just that specific rsync command. This can be done by manually running the rsync command with the ''-e'ssh -v' ''option which will output the exact command sent that can be used in the remote hosts ''authorized_keys'' file instead of just "rsync".
  
== Backing up a server with Rsync ==
+
== Backing up Maildirs with Rsync ==
Rsync is a very useful tool for backing up and synchronising data, here's a simple way to transfer everything from one server to a location on a remote server.
+
Backing up Maildirs can be a problem with many target systems (even non-Windows ones) because many filesystems don't allow colons in file names. This problem occurs for us using the [http://www.adrive.com ADrive backup service].
{{code|<bash>rsync -av –delete -e ssh / USER@HOST:/LOCATION --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found}</bash>}}
 
  
'''NOTE:''' there is a problem when trying to backup Maildirs like this if the target machine is Windows (like the ADrive service) because the file names contain colons. This has been addressed on the form of the [https://git.samba.org/?p=rsync-patches.git;a=blob;f=transliterate.diff;h=19d6393537903a1fb7d5581b8216b999fa82a450;hb=135a233d6f4d401c187117ae57fac147f2a863a4 transliterate path] which adds a '''--tr=BAD/GOOD''' option for mapping bad characters to good ones. To install the patch you need to download and unpack the latest source and the patches, then change into the source directory and do the following:
+
We've over come this problem using a two step solution. First we install rsync from source with the [https://git.samba.org/?p=rsync-patches.git;a=blob;f=transliterate.diff;h=19d6393537903a1fb7d5581b8216b999fa82a450;hb=135a233d6f4d401c187117ae57fac147f2a863a4 transliterate patch] applied which adds a '''--tr=BAD/GOOD''' option for mapping bad characters to good ones. And then second, we use the [http://curlftpfs.sourceforge.net/ CurlFtpFS] utility which allows one to mount a remote FTP storage resource to a local mount point. This allows one to use a patched local rsync to synchronise with a remote storage facility with the colons replaced with a more suitable character such as a semicolon.
 +
 
 +
To install the patch you need to download and unpack the latest source and the patches, then change into the source directory and do the following:
 
{{code|<bash>
 
{{code|<bash>
 
patch -p1 <patches/transliterate.diff
 
patch -p1 <patches/transliterate.diff
Line 34: Line 35:
 
</bash>}}
 
</bash>}}
  
This still doesn't completely solve the problem though because ADrive would need to have implemented this patch as well since it too will be running with the same command-line options. One potential way to deal with this could be [http://curlftpfs.sourceforge.net/ CurlFtpFS] which allows one to mount a remote FTP resource as a local filesystem, and is available in the standard APT repositories. This means that the Maildir could be synchronised to the locally mounted FTP resource using the ''--tr'' option.
+
Next, the CurlFtpFS utility can be installed via ''apt-get'' on Debian-based systems, then a script run on from ''crontab'' which mounts the remote resource and synchronises the Maildirs. This example script synchronises all the home directories. It assumes that a directory called ''/root/adrive'' already exists.
 +
{{code|<bash>
 +
curlftpfs -o user=foo@bar.baz:foopass ftp://ftp.adrive.com/ /root/adrive/
 +
rsync -av --inplace --tr=':/;' /home /root/adrive/
 +
fusermount -u /root/adrive
 +
</bash>}}
 
[[Category:Software]][[Category:Linux]]
 
[[Category:Software]][[Category:Linux]]

Revision as of 01:04, 13 July 2014

rsync is an open source utility that provides fast incremental file transfer. rsync is freely available under the GNU General Public License and is currently being maintained by Wayne Davison.

rsync uses the "rsync algorithm" which provides a very fast method for bringing remote files into sync. It does this by sending just the differences in the files across the link, without requiring that both sets of files are present at one of the ends of the link beforehand.

Some features of rsync include

  • Can update whole directory trees and filesystems
  • Optionally preserves symbolic links, hard links, file ownership, permissions, devices and times
  • Requires no special privileges to install
  • Internal pipelining reduces latency for multiple files
  • Can use rsh, ssh or direct sockets as the transport
  • Supports anonymous rsync which is ideal for mirroring

Using rsync over SSH

Sometimes it's useful to do a one-off backup of a file structure from one host to another, and since all the hosts (in our system) are guaranteed to be able to connect to each other with SSH (after adding appropriate RSA keys), using rsync over SSH is a good way to do this.

The transfer syntax is then done very similarly to SCP, for example to pull new changes from a remote directory to a local one, use:

<bash>rsync -avz -e ssh remoteuser@remotehost:/remote/dir /this/dir/</bash>

After the systems are confirmed as being able to connect over SSH you may want to lock them down so that the connection between them can only be used for rsync. The IP and command can be prepended to the key in the remote hosts ~/.ssh/authorized_keys file.

from="1.2.3.4",no-agent-forwarding,no-port-forwarding,no-pty,no-user-rc,no-X11-forwarding ssh-rsa AAAAB...

For more security, the command allowed can be restricted to just that specific rsync command. This can be done by manually running the rsync command with the -e'ssh -v' option which will output the exact command sent that can be used in the remote hosts authorized_keys file instead of just "rsync".

Backing up Maildirs with Rsync

Backing up Maildirs can be a problem with many target systems (even non-Windows ones) because many filesystems don't allow colons in file names. This problem occurs for us using the ADrive backup service.

We've over come this problem using a two step solution. First we install rsync from source with the transliterate patch applied which adds a --tr=BAD/GOOD option for mapping bad characters to good ones. And then second, we use the CurlFtpFS utility which allows one to mount a remote FTP storage resource to a local mount point. This allows one to use a patched local rsync to synchronise with a remote storage facility with the colons replaced with a more suitable character such as a semicolon.

To install the patch you need to download and unpack the latest source and the patches, then change into the source directory and do the following:

<bash>

patch -p1 <patches/transliterate.diff ./configure make make install </bash>

Next, the CurlFtpFS utility can be installed via apt-get on Debian-based systems, then a script run on from crontab which mounts the remote resource and synchronises the Maildirs. This example script synchronises all the home directories. It assumes that a directory called /root/adrive already exists.

{{{1}}}