lftp. How to “rsync” with ftp

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"

I’ve always been in complete control of my own servers¹, this mean they have all the tools I need. Recently they got me in charge of regularly backup a large site. This usually mean that I’m taking filesystem ‘snapshots’. But I’ve only have access to this large site via ftp, and my script needs rsync (and/or ssh). I need a way to do (almost) the same, after a research I’ve found lftp; this article will show you how to “rsync” with ftp.

¹ actually, the company own the servers. But even if I’ll ever left the company (and they wouldn’t let me bring those servers with me), they are still “mine“.

Install lftp

It’s probable the repositories of your distribution already include lftp, so you can try first

# apt-get (or yum) install lftp

But I wanted this tutorial to be distribution “agnostic”, so you can download the sources from here: https://lftp.yar.ru/get.html .

Next you need to extract that tar file with:

# tar jxvf lftp-X.y.Z.tar.bz2
unpacking lftp
how to rsync with ftp
unpacking downloaded lftp tarfile

Finally, you can compile and install lftp with the usual commands:

# cd lftp-X.y.Z
# ./configure; make; make install
configuring and compiling lftp
configuring, compiling and installing lftp

Remember you can only write files in /usr (or /usr/local) as root; maybe you need to ‘su’ or ‘sudo’ before the make install.

installing lftp
lftp installed

You can always change the files destination with ./configure --prefix=/some/other/path.

lftp basic usage

As with any command line ftp client, you can connect to a ftp server with the command:

lftp ftp://user@ftpserver

Then use commands like get <file> to download some file from the server, or put <file> to upload some file to the server or ls to see the files on the server.

how to rsync with ftp.
basic lftp interactive usage
downloading, uploading and listing a remote directory with lftp

You may have noticed on the previous screenshot an error about ssl certificate mismatch. That’s because I’m not connection with ftp over ssl or sftp. As a workaround you can execute the following command:

set ssl:verify-certificate no

But this will work on the current ftp session. If you want to make it permanent, and you are to use lftp only without ssl, you can create a ~/.lftprc file with that command. See the manpage for the other settings.

uploading a file to the ftp server
no ssl certificate error!

How to “rsync” with ftp

There are a lot of ftp clients to do ftp things interactively in a more friendly way. I’ve also promised² an alternative to rsync for when there is only ftp access to the shared hosting. So, here it is:

You can use lftp not interactively, for example in scripts, by writing lftp commands with the -c switch. There are a lot of commands actually, see the lftp manpage.

To synchronize remote directories we use the mirror command. To be like rsync we need to copy only new or modified files, use mirror with the -n switch. Also, if a file was delete on the origin, we need to delete on destination, -e switch. Then, the full mirror command will be like this:

lftp user@server:/path> mirror -ne /remote/path /local/backup/path

If you need to synchronize the other way, i.e. upload a local folder to the server, you need the -R switch:

lftp user@server:/path> mirror -neR /local/path /remote/path 

This is still an interactive use. To put it all in a script you can write the following command:

lftp -c “open -u user,password ftp://server/ ; mirror -ne /remote/path /local/backup/path

how to rsync with ftp. 
lftp sync between a remote and a local folder
I’m syncing the whole home directory

Finally, put it all in a cron job to do it automagically and we are done 😉

² no, I didn’t. But you can take that part of my articles where says “this article will show/teach you how to do something” as a kind of promise

³ fun fact: You may have noticed from the screenshots that I took the screenshots for this articles from two different computers. One in my office, then I’ve finished at home.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"
Gonzalo Rivero
Gonzalo Rivero
I am Gonzalo, I live in Salta, a city located in the NW of Argentina. I play the guitar and a little harmonica. I also like to bike.


Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook