Basically what you need from LFTP in order to download a remote directory is the mirror utility. The built-in mirror action of LFTP can download or update a whole directory tree. There is also reverse mirror (mirror -R) which uploads or updates a directory tree on server. Mirror can also synchronize directories between two remote servers, using FXP if available.
In this example, we'll write an lftp script that is executed by the lftp utility. This is done to basically be able to set custom properties of lftp inline in our script using the set option. Get started by creating the download_example.lftp
file and according to your authentication method with your server add the following content to the file:
A. With Username & Password
If you use an username and a plain text password to access your server via SFTP, then this is the way to proceed. In the open instruction, provide the authentication string that specifies the protocol, the username to login that is usually root and the password followed by the IP address or domain of your server:
set ssl:verify-certificate no
set sftp:auto-confirm yes
open sftp://username:[email protected]
mirror --verbose --use-pget-n=8 -c --verbose /remote/directory/that-you-want-to-download /local/directory/to-save;
bye
B. With a SSH Key
If instead of basic authentication to access your server, you use SSH Keys, then you will need to use the OpenSSH Key to access it. Simply change the connect-program used by LFTP to SSH with the necessary arguments and specifying the identity file with the -i
argument:
set ssl:verify-certificate no
set sftp:auto-confirm yes
set sftp:connect-program "ssh -v -a -x -i C:\Users\<username>\.ssh\id_rsa"
open sftp://username:[email protected]
mirror --verbose --use-pget-n=8 -c --verbose /remote/directory/that-you-want-to-download /local/directory/to-save;
bye
The pget options gets the specified file (or directory) using several connections. This can speed up transfer, but loads the net and server heavily impacting other users. Use only if you really have to transfer the file ASAP, otherwise remove it from the example. The --use-pget-n
argument specifies the number pget commands used to transfer every single file under mirror. A closure can be matched against source or target host names, the minimum number greater than 0 is used. When the value is less than 2, pget is not used.
After saving the script, run it with lftp using the following command:
lftp -f download_example.lftp
This will start automatically the download of the directory and will output the progress thanks to the --verbose
argument, that provides a detailed output about the progress of the task:
When the script finishes, you will now have locally the remote directory in your local system thanks to LFTP.
Happy coding !