Keep in sync two hosts on Internet using LFTP

by Roberto on April 21, 2009

in General

There are many softwares and solutions that permit to keep in sync two or more hosts (like Rsync, Unison, etc), but when the sync must be made through Internet and you have access only to the ftp or http service your choices narrow down. This is the typical situation you can face on a shared hosting solution.
A great solution to synchronize two directories on different hosts through ftp or http protocol is LFTP :

LFTP is sophisticated ftp/http client, file transfer program supporting a number of network protocols. Like BASH, it has job control and uses readline library for input. It has bookmarks, built-in mirror, can transfer several files in parallel. It was designed with reliability in mind

LFTP has many useful features, like :

  • FISH and SFTP protocols support
  • Built-in mirror and reverse mirror (mirror -R)
  • Job queueing
  • Job execution at specified time
  • Comprehensive scripting support
  • FXP transfers support (between two FTP servers, bypassing client machine)

See the complete list of features

To keep in sync two directories on two hosts on Internet you need to create a little shell script and set a cron job for it on one host and create an ftp access on the other host.
For example for BASH shell :

#!/bin/bash
#Synchronization script
lftp -u [remote username],[remote password] [remote host] <<EOF
set ftp:ssl-allow no
mirror [remote directory to sync] [local directory to sync]
quit 0
EOF

Parameters :

  • [remote username] : remote ftp username
  • [remote password] : remote ftp password
  • [remote host] : remote hostname or ip address
  • [local directory to sync] : local directory to synchronize
  • [remote directory to sync] : remote directory to synchronize

Other resources : LFTP manual, LFTP tutorial

{ 5 comments… read them below or add one }

Daniele September 9, 2009 at 16:42

Hi Roberto,
the bash example is not complete, but I’d like to have it :)

Thanks!
Daniele

Roberto September 10, 2009 at 09:03

Many thanks.
I’ve fixed it now.

Roberto

Tyler Leavitt November 6, 2009 at 11:05

Hey bro… you have the remote and local dir flipped in example (remote is first, followed by local). Damn this drove me crazy ! =)

But props for posting this… there isn’t much info on lftp out there.

Roberto November 6, 2009 at 11:15

Hi Tyler.
I’ve fixed it.
Many thanks.

Shaikh Dawood February 3, 2010 at 12:19

Hi Roberto,

Many thanks for the above script, it was really very useful.

Thanks,
Shaikh

Leave a Comment

Previous post:

Next post: