December 4, 2007

CLI Magic: Quick and easy backup with lftp

Author: Dmitri Popov

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.

Categories:

  • Backup & Data Recovery
  • Networking
  • Tools & Utilities