Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save silentdth/e81b4477e3bc512f558dd077501c1834 to your computer and use it in GitHub Desktop.
Save silentdth/e81b4477e3bc512f558dd077501c1834 to your computer and use it in GitHub Desktop.
Bash script using lftp to mirror remote directory to local directory using FTPS and recursion, thus keeping the remote directory synchronized with the local one. This script uses 'mirror'.
#!/bin/sh
# @author: Simon Tipping, Ben Dodd
# @description: A script to mirror remote folder to local folder using lftp - Based on Alexandre Plennevaux's "MIRROR DISTANT FOLDER TO LOCAL FOLDER VIA FTP" script https://gist.github.com/pixeline/0f9f922cffb5a6bba97a
# This version is for FTPS and recurses all subdirectories.
# FTP LOGIN
HOST='ftps://ftp.domain.com'
USER='ftpusername'
PASSWORD='ftppassword'
# DISTANT DIRECTORY
SOURCE='/absolute/path/to/remote/directory'
# LOCAL DIRECTORY
TARGET='/absolute/path/to/local/directory'
# RUNTIME!
echo "Starting download $SOURCE from $HOST to $TARGET"
date
# lock code from user FeralHosting https://raw.githubusercontent.com/feralhosting/feralfilehosting/master/Feral%20Wiki/SFTP%20and%20FTP/LFTP%20-%20Automated%20sync%20from%20seedbox%20to%20home/scripts/lftp-sftp.sh
base_name="$(basename "$0")"
lock_file="/tmp/$base_name.lock"
trap "rm -f $lock_file; exit 0" SIGINT SIGTERM
if [[ -e "$lock_file" ]]
then
echo "$base_name is running already."
exit
else
touch "$lock_file"
lftp << EOF
# SSL FTP SETTINGS
set ftp:ssl-force true
set ftp:ssl-allow true
set ftp:ssl-protect-data true
# USE AT YOUR OWN RISK - OTHERWISE MANUALLY INSTALL THE CERT
set ssl:verify-certificate no
# CONNECT
open -u "$USER","$PASSWORD" "$HOST"
# Split file into 5 pieces when downloading
# MY FTP SERVER DIDNT LIKE THIS. Commented out.
#set mirror:use-pget-n 5
# Download 5 files at once
mirror -c -P5 --log="/var/log/$base_name.log" "$SOURCE" "$TARGET"
# QUIT
quit
EOF
rm -f "$lock_file"
trap - SIGINT SIGTERM
fi
echo
echo "Transfer finished"
date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment