This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Backup & Data Recovery

CLI Magic: Quick and easy backup with lftp

By Dmitri Popov on December 04, 2007 (9:00:00 AM)

Share    Print    Comments   

No matter what Linux distribution you are using, chances are you'll find more than one graphical FTP client in its repositories, but if you are looking for a powerful command-line FTP tool, your best bet is lftp. Of course, you can always use the good old ftp command, but lftp takes the task of managing files and directories using the FTP protocol to a new level. To see what I mean, let's use lftp to write a script that creates a local backup copy of a Web site.

 To write the script, you need to know how to use lftp to connect to an FTP server and synchronize a remote directory with a local one. If your FTP server supports anonymous connections, you can connect to it using the simple command lftp ftpsite. If the server requires a user name and password, the connection command would look like lftp -u username,password ftpsite.

To synchronize a remote directory with a folder on your hard disk, lftp utilizes the mirror command. Used without switches, this command syncs the current local and remote directories. You can also specify explicitly the source and target directories:

mirror path/to/source_directory path/to/target_directory

The mirror command offers a comprehensive set of switches, which you can use to control the synchronization process. For example, used with the --delete switch, the mirror command deletes the files in the local folder that are not present in the remote directory, while the --only-newer option forces lftp to download only newer files. Another handy switch is --exclude; it allows you to specify which files and directories to skip during synchronization. And if you prefer to keep an eye on the syncing process, you can use the --verbose switch.

Typing all those switches every time you want to synchronize two directories can be a bit of a bother. Fortunately, lftp understands complex commands that can perform several actions in one fell swoop. All you have to do is to use the -e switch, so lftp stays connected and runs the specified commands:

lftp -u username,password -e "mirror --delete --only-newer --verbose path/to/source_directory path/to/target_directory" ftpsite

Using this command, lftp connects to the FTP server using the provided credentials, and then runs the command(s) in the quotes. You can save the entire command in a text file, then run it by pointing lftp to it using the -f switch:

lftp -f /home/user/ftpscript.txt

lftp has a few other clever tricks up its sleeve. The at switch can come in handy when you want to run the backup at a specific time. The following command, for example, runs at midnight:

lftp at 00:00 -u username,password -e "mirror --delete --only-newer --verbose path/to/source_directory path/to/target_directory" ftpsite &

Notice the ampersand, which sends the command to the background so you don't have to keep the terminal window open.

Now you know how to create local backup of files and directories stored on an FTP server. But how do you restore the data if disaster strikes? Quite easily, actually. All you have to do is to add the --reverse switch to the mirror command:

lftp -u username,password -e "mirror --reverse --delete --only-newer --verbose path/to/source_directory path/to/target_directory" ftpsite

As the name suggests, the switch reverses the source and target directories, so lftp uploads files from the local directory to the remote FTP server.

That's all there is to it. Check lftp's man pages to get an overview of lftp's other useful options, and start FTPing like a pro.

Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, US, German, and Danish computer magazines.

Share    Print    Comments   

Comments

on CLI Magic: Quick and easy backup with lftp

Note: Comments are owned by the poster. We are not responsible for their content.

CLI Magic: Quick and easy backup with lftp

Posted by: Anonymous [ip: 213.39.169.35] on December 04, 2007 09:33 AM
Ha, I needed this yesterday. =D
Thanks!

#

CLI != magic

Posted by: Anonymous [ip: 169.233.25.231] on December 04, 2007 10:05 AM
I'm new here. Why do they refer to the CLI as magic? It's not.

#

Re: CLI != magic

Posted by: Anonymous [ip: 71.207.161.72] on December 04, 2007 11:56 AM
If you are new, why do you always post the same CLI != magic line? For some reason you seem to have a bug up your behind about either CLIs or magic and insist on being a twit.

#

Re: CLI != magic

Posted by: Anonymous [ip: 81.5.128.10] on December 09, 2007 03:30 PM
Animal Magic, the TV show from the 60's? What's magic about that? It's just animals ...talking to a zoo keeper! Magic Roundabout? Pinball Magic? Magical Mystery Tour? What the hell's going on here, I've just found a fishing magazine called 'Fishing Magic', I can't see one magical thing about it, it's all just fish in there. WOL? Nothing magic about them packets, I can assure you. Partition Magic? I just don't get it! Industrial Light and Magic?? Just who are THEY trying to kid?

You must be hours of fun at parties, Mr. Literal.

#

CLI Magic: Quick and easy backup with lftp

Posted by: Anonymous [ip: 202.180.72.118] on December 04, 2007 10:06 AM
"but if you are looking for a powerful command-line FTP tool, your best bet is lftp." I absolutely agree with you! I use lftp every day and love it.

#

ftp... for backup?

Posted by: Anonymous [ip: 169.233.25.231] on December 04, 2007 10:06 AM
Why not rsync? or unison? or bacula? or amanda?

#

DO NOT use ftp, use something else

Posted by: Anonymous [ip: 68.76.215.54] on December 04, 2007 12:10 PM
Please do not use ftp for backup. Use something like rsync. Its not as hard as people make it out to be and you don't have to run an ftp server. Just do something like this:

rsync -a -e ssh user@from.server.com /backup/to/location/locally

See, that's not hard. And it will provide you with a lot more professional features that ftp won't, like copying symlinks and device files as is.

Don't use the wrong solution.

#

Re: DO NOT use ftp, use something else

Posted by: Anonymous [ip: 62.36.225.225] on December 04, 2007 03:43 PM
Yeah, and FTP is not encrypted, so all data can be sniffed. lftp? I don't think so, ncftp is the best there is. And as the parent said, don't use ftp for something like this. rsync is the right tool, and can be encrypted. I know ftp can be tunneled with SSH, but not by default. Even scp is better than ftp.

BTW, why don't linux.com fix the stupid captcha so it works as it should?

#

Re(1): DO NOT use ftp, use something else

Posted by: Anonymous [ip: 24.248.89.66] on December 04, 2007 09:00 PM
Actually lftp supports sftp, https and ftps. Anyways, rsync is indeed the preferred tool for backups, but for a poor man backing up to a hosting provider, they may need ftp.

#

not ncftp please... but probably they really mean ftp

Posted by: Michael Shigorin on December 07, 2007 08:05 PM
ncftp sucks, sorry (was its user until stumbling on lftp which *is* a proper ftp shell, like zsh is a proper shell).


rsync is what came to my mind first upon spotting the RSS headline *but* I guess whether a lot virtual hosting providers do that... (on a dedicated server or VPS one must be crazy to use FTP wilfully)

#

Re: DO NOT use ftp, use something else

Posted by: Anonymous [ip: 209.17.181.250] on December 04, 2007 07:04 PM
Sometimes FTP is the only option.

#

CLI Magic: Quick and easy backup with lftp

Posted by: FrdPrefct on December 04, 2007 04:36 PM
rsync with ssh would be the way to go. It's also much more flexible and can get around some firewall issues better.

#

CLI Magic: Quick and easy backup with lftp

Posted by: Anonymous [ip: 24.250.130.176] on December 09, 2007 04:45 PM
For FTP/SFTP's backup and synchronization, some nice GUI tools like CrossFTP is much more reliable and easy to use for most people. For other protocol type of backups, rsync and unison are good choices.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya