Skip to content

Instantly share code, notes, and snippets.

@pixeline
Last active September 28, 2023 09:46
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save pixeline/0f9f922cffb5a6bba97a to your computer and use it in GitHub Desktop.
Save pixeline/0f9f922cffb5a6bba97a to your computer and use it in GitHub Desktop.
Bash script using lftp to mirror remote directory to local directory, thus keeping the local directory synchronized with the remote one.
#!/bin/sh
# @author: Alexandre Plennevaux
# @description: MIRROR DISTANT FOLDER TO LOCAL FOLDER VIA FTP
#
# FTP LOGIN
HOST='sftp://ftp.domain.com'
USER='ftpusername'
PASSWORD='ftppassword'
# DISTANT DIRECTORY
REMOTE_DIR='/absolute/path/to/remote/directory'
#LOCAL DIRECTORY
LOCAL_DIR='/absolute/path/to/local/directory'
# RUNTIME!
echo
echo "Starting download $REMOTE_DIR from $HOST to $LOCAL_DIR"
date
lftp -u "$USER","$PASSWORD" $HOST <<EOF
# the next 3 lines put you in ftpes mode. Uncomment if you are having trouble connecting.
# set ftp:ssl-force true
# set ftp:ssl-protect-data true
# set ssl:verify-certificate no
# transfer starts now...
set sftp:auto-confirm yes
mirror --use-pget-n=10 $REMOTE_DIR $LOCAL_DIR;
exit
EOF
echo
echo "Transfer finished"
date
@luizhtss
Copy link

How to get subdirectories ?

@tatoosh
Copy link

tatoosh commented Dec 13, 2017

what to change to download from webdav?

@tamboer
Copy link

tamboer commented Jul 8, 2020

Error: mirror: Fatal error: Host key verification failed.
This worked for me. set sftp:auto-confirm yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment