Using lftp to synchronize folders with a FTP account

10611

lftp is a powerfull FTP client than can be used to sync with a remote account. In Ubuntu 9.04 it is already installed so all you have to do is figure out how to use it. 🙂

First, you’ll need 2 “scripts”, one to download files from the remote FTP server to your computer an one to upload them from your computer to the server.

Download script:

Create a file named download.x with the following content:

open -u user,password -p [port][server]
mirror -c -e /remote_directory/local_directory
exit

You will need to write your username and password; also specify the port, usually 22, and the server address (eg: ftp://domain.com – you can also use sftp://). Also insert the absolute paths to the remote and  local directories.

The effect of the option -e  in the second line is that files that don’t exist anymore in the remote directory will be deleted from the local directory; you may want to change this if you don’t need this option.

Upload script:

open -u user,password -p [port] [server]
mirror -c -e -R/local_directory /remote_directory
exit

 

There are only a few things changed in the upload script: the -R option is used because we want to upload from the local directory to the remote one. Also note that the order of the two folders changed from the download script.

There are many other options for lftp; just, you know, man lftp.

Now, to download the files from the remote FTP server to the local directory open a terminal an type in:

$ lftp -f download.x

Note: if the download.x file is not in your home directory, you’ll have to write the path to it.

To upload the files to the remote directory use the command:

$ lftp -f upload.x

 

Hope this helps.