Home » , , » backup and sync with lftp

backup and sync with lftp

lftp is an amazing  command line ftp tool, which lets you operate remote files just like in a local filesystem in a terminal (bash).  If you work a lot with command line, I bet you would like it.  A frequently used functionality with lftp is backup or sync a remote directory with a local one.
Here is simple synchronization script using lftp.

#/bin/bash
lftp -u user,password publish.163.com <
set ftp:ssl-allow no
#a slash “/” in local dir is used to keep the dir structure
mirror   print/ ~/tmp_install/print/
mirror -R  ~/tmp_install/print/ print/
quit 0
EOF
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 FTPserver 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 mirrorcommand 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 –verboseswitch.
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.

0 Comments:

Popular Posts