How to do website backup using lftp

This article is about using the lftp command line for backup of website data. Of course, if you are backing up data to another VPS or server, rsync backup is recommended, but if you are backing up data to a virtual server that only has an FTP server host, then lftp backup is the best choice.

LFTP is a sophisticated file transfer program supporting a number of network protocols (ftp, http, sftp, fish, torrent). Like BASH, it has job control and uses the readline library for input. It has bookmarks, a built-in mirror command, and can transfer several files in parallel. It was designed with reliability in mind. LFTP is free software, distributed under the GNU GPL license.

If lftp is not installed, on CentOS systems, you can be installed using yum install lftp. In order to introduce this kind of backup method, we will introduce the use of lftp step by step. If the ftp server supports anonymous connections, you can simply use the following command to connect.
lftp ftpsite
If you need a password, you can use the following command to connect.
lftp -u username,password ftpsite
In order to synchronize a local folder to a remote folder, lftp adds the mirror command. Without setting other options, you only need to specify the local directory and the remote directory.
mirror -R local_directory remote_directory
The mirror command provides several useful options to control the synchronization process. For example, using the -delete option, the mirror command deletes files that exist on the remote backup server but files that do not exist on the local server. When using the -only-newer option, lftp uploads only new files. Another option is -exclude, which allows you to specify folders that do not need to be synchronized. If you want to monitor the progress of the synchronization, you can use the -verbose option.
You only need to add the -e option to tell lftp to execute the connection to the server. Then run the specified command.
lftp -u username,password -e “mirror -R –delete –only-newer –verbose local_directory remote_directory” ftpsite

lftp provides some intelligent functions. The at option can easily specify when to run the program. The following command indicates to run every morning:

lftp at 00:00 -u username,password -e “mirror -R –delete –only-newer –verbose local_directory remote_directory” ftpsite &

Note the (&) symbol, which means that the command runs in the background without having to keep the terminal open.

Now you know how to use lftp to synchronize the local directory and the remote directory, but how to recover when the local server data is lost? Quite simply, you just need to remove the -R option, lftp will download the remote file to the local. Such as:

lftp -u username,password -e “mirror –delete –only-newer –verbose local_directory remote_directory” ftpsite

If you need to know more about lftp commands, you can use the man lftp command to view all the lftp commands.