How to Synchronize Remote / Local Using LFTP

Categories:

LFTP is a command-line program client for several file transfer protocols (FTP, FTPS, HTTP, HTTPS, FISH, SFTP, BitTorrent, and FTP over HTTP proxy). LFTP is reliable, non-fatal errors are handled and failed operations are retried automatically. So if downloading breaks, it will be restarted from that point automatically.

The program can be used to synchronize files from Local to Remote and the other way around.

How to install LFTP on Linux?

LFTP can be installed very easily on your favorite Linux distro.
Here are the commands for Ubuntu and CentOS:

Ubuntu

sudo apt install lftp

CentOS

sudo dnf install lftp

How to use LFTP to synchronize from Remote to Local?

Below is a script I made to sync a local folder from a remote server using FTP. Any files that are different on the remote server will be downloaded locally and any local files that are not present on the remote server will be deleted. After running the script the local and remote directories will be in sync.
Change the first 5 lines (FTP_HOST, FTP_USER, etc.) according to your needs.

#!/bin/bash
FTP_HOST='ftp.mydomain.com'
FTP_USER='mrFTP@mydomain.com'
FTP_PASSWORD='passwordToChange'
REMOTE_FOLDER='/'
LOCAL_FOLDER='/home/backup'

lftp -f "
open $FTP_HOST
user $FTP_USER $FTP_PASSWORD
lcd $LOCAL_FOLDER
mirror --continue --delete --verbose $REMOTE_FOLDER $LOCAL_FOLDER
bye
"

How to use LFTP to synchronize from Local to Remote?

The script below will upload the local files to the remote server using FTP. It will also delete any files on the remote server that are not present locally. Don’t forget to change FTP_HOST, FTP_USER, etc. with your credentials.

#!/bin/bash
FTP_HOST='ftp.mydomain.com'
FTP_USER='mrFTP@mydomain.com'
FTP_PASSWORD='passwordToChange'
REMOTE_FOLDER='/'
LOCAL_FOLDER='/home/backup'

lftp -f "
open $FTP_HOST
user $FTP_USER $FTP_PASSWORD
lcd $LOCAL_FOLDER
mirror --continue --reverse --delete --verbose $LOCAL_FOLDER $REMOTE_FOLDER
bye
"

In these scripts the FTP username and password are not encrypted. There are some options to hide the password better. If you need better security, you might want to look into that. If not, make sure your scripts have the correct rights.

Also, it’s usually a good idea not to access the most critical data using FTP. For example, if your remote server is a website, I would not access /public_html directly. When syncing data from the website to my local development drive I am simply downloading backups which are not stored in /public_html and the FTP account has no access to that directory. It’s not as cool as syncing every changed file in a few seconds but it’s safer.

If you need more information on how to create and run scripts based on the above examples, please visit How to Create and Run Linux Bash Scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *