Skip to content

Instantly share code, notes, and snippets.

@mugifly
Last active November 20, 2015 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mugifly/931624e375d40267afc4 to your computer and use it in GitHub Desktop.
Save mugifly/931624e375d40267afc4 to your computer and use it in GitHub Desktop.
FTP directory clone script for Unix OS (Required: bash & wget command)
#!/bin/bash
# FTP directory clone script for Unix OS
# Required: bash & wget command
# https://gist.github.com/mugifly/931624e375d40267afc4
# Copy source (Remote FTP server)
SRC_HOST="example.com"
SRC_USERNAME="xxxx"
SRC_PASSWORD="yyyy"
SRC_DIRECTORY="/home/xxxx/src"
# Copy destination directory (Local computer)
DST_DIRECTORY="/home/zzzz/dst"
# Maximum depth level of recursion directory search
MAX_RECURSIVE_DEPTH=50
# ----
# Confirm the settings
echo "-- FTP Directory Clone --"
echo "Copy source: "
echo "* Host: ${SRC_HOST}"
echo "* Username: ${SRC_USERNAME}"
echo "* Directory: ${SRC_DIRECTORY}"
echo ""
echo "Copy destionation: "
echo "* Host: localhost"
echo "* Directory: ${DST_DIRECTORY}"
if [ ! -d $DST_DIRECTORY ]; then
echo -e "\nThe copy destination directory was not found."
echo "Now created the directory: ${DST_DIRECTORY}"
mkdir $DST_DIRECTORY
fi
if [ -d "${DST_DIRECTORY}/${SRC_HOST}/" ]; then
echo -e "\nAn useless directory in the copy destination directory was found"
echo "Please check the files and delete that directory:"
echo "${DST_DIRECTORY}/${SRC_HOST}/"
exit 0
fi
# Confirm a execution
echo -e "\nWould you start download via FTP from ${SRC_HOST}?"
echo -e "[CAUTION] The copy destination directory will be OVERWRITTEN!:\n ${DST_DIRECTORY}"
echo -e "\nOkay? (yes/no)"
read ANS
if [ $ANS != "yes" ]; then
echo "Canceled"
exit 0
fi
# FTP download
echo -e "\nFTP downloading..."
wget -mckr -np -l ${MAX_RECURSIVE_DEPTH} --restrict-file-names=nocontrol --directory-prefix=${DST_DIRECTORY} --passive-ftp "ftp://${SRC_USERNAME}:${SRC_PASSWORD}@${SRC_HOST}/${SRC_DIRECTORY}"
# It's ok even if It shows a double slash (e.g. /foo//bar/) in the path.
# Check whether a download is valid (Because can't get a exit code as normally)
if [ ! -d "${DST_DIRECTORY}/${SRC_HOST}/" ]; then
echo "[Error] Could not download!"
exit 255
fi
# Delete useless files (Those generated by wget command)
find ${DST_DIRECTORY} -name '.listing' -exec rm -f {} \; > /dev/null 2>&1
# Fix a directory hierarchy
for file in "${DST_DIRECTORY}/${SRC_HOST}/${SRC_DIRECTORY}/*"
do
cp -af -R ${file} ${DST_DIRECTORY}
done
rm -rf "${DST_DIRECTORY}/${SRC_HOST}/"
# Complete
echo "Done. ${DST_DIRECTORY}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment