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
Finally, you can compile and install lftp with the usual commands:
# cd lftp-X.y.Z # ./configure; make; make install
Remember you can only write files in /usr (or /usr/local) as root; maybe you need to ‘su’ or ‘sudo’ before the make install.
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.
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.
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“
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.