Skip to content

Commit

Permalink
Create beta release
Browse files Browse the repository at this point in the history
- add SSH option (fixes #1)
- add folder or content transfer option (fixes #3)
- small improvements
  • Loading branch information
mhellmeier committed Sep 7, 2020
1 parent d04f4ec commit 41e742a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 76 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ A simple bash script to move files over FTP / SSH from a source to a (different)

## About The Project

**Current version**: 0.0.1 (Alpha Phase)
**Current version**: 0.1 (Beta Phase)

There are many cases where people want to transfer files and folders from a source server `A` to a destination server `B` (for example websites or backups). In most of the cases, the only possible solution to migrate a website to a new host is to download all files manually with a FTP client (like FileZilla) on your local computer and upload it to the new server afterwards. Direct transfers over FXP aren't possible in most of the cases due to restrictions in many (shared) webhosting packages. This small bash script will do the job for you!

Expand All @@ -32,10 +32,11 @@ If there is access to a VPS or Root Server `C`, I suggest executing the script o

### Pre-Requirements

To use the script, these are the only things you need:
To use the script, these are the only things you need (will be installed with the install script):
- Linux machine (or Mac or Winodws 10 with Linux Subsystem installed)
- `ftp` installed (`sudo apt install ftp`)
- `lftp` installed (`sudo apt install lftp`)
- `sshpass` installed (`sudo apt install sshpass`)

### Installation

Expand All @@ -58,7 +59,7 @@ sh install.sh
### Important Information

- The script asks for passwords to connect to the source and destination hosts. Keep in mind that these will be used in the commands (plaintext!) to connect and download / upload content. It can be a security concern if the commands will be stored in the log history
- To get rid of some certificate verifiaction errors, the scripts doesn't make certificate checks
- To get rid of some certificate verification errors, the scripts doesn't make certificate checks
- Remember to open FTP / SSH relevant ports in your firewall
- Use it at your own risk!

Expand All @@ -70,7 +71,7 @@ Alle planned features, bugs and discussions can be found in the [open issues](ht

## Contributing

Feel free to fork the project, work in your personal branch and create a pull request your simple interact in the [issue section](https://github.com/mhellmeier/FTP-Move-Server-Files/issues).
Feel free to fork the project, work in your personal branch and create a pull request or you simple interact in the [issue section](https://github.com/mhellmeier/FTP-Move-Server-Files/issues).

**This is an open source project! Every contribution is greatly appreciated!**

Expand Down
1 change: 1 addition & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

sudo apt install ftp -y
sudo apt install lftp -y
sudo apt install sshpass -y

chmod +x moveServerFiles.sh
169 changes: 97 additions & 72 deletions moveServerFiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,114 +4,139 @@
# ***** SOURCE *****
# ******************

read -p "Do you want to use SSH (1) or FTP (2) for the connection to the source host? [1/2]: " sourceConnectionMode
# Collect information and source credentials
read -p "Do you want to use SSH / SFTP (1) or FTP (2) for the connection to the source host? [1/2]: " sourceConnectionMode

# FTP connection
if [[ $sourceConnectionMode == "2" ]]; then
read -p "[Source] Host name / URL / IP: " sourceFtpHostName
read -p "[Source] Host name / URL / IP: " sourceHostName

read -p "[Source] Username: " sourceUsername

stty -echo
printf "[Source] Password: "
read sourcePassword
stty echo

echo
read -p "[Source] Path to download recursively ('/' for current directory): " sourceDownloadPath

read -p "[Source] Username: " sourceFtpUsername
read -p "Do you want to transfer the folder itself (1) or only the content of $sourceDownloadPath (2)? [1/2]: " sourceDownloadMode
echo

stty -echo
printf "[Source] Password: "
read sourceFtpPassword
stty echo
printf "\n"
if [[ $sourceDownloadPath != /* ]]; then
sourceDownloadPath="/"$sourceDownloadPath
fi
if [[ $sourceDownloadPath != */ ]]; then
sourceDownloadPath=$sourceDownloadPath"/"
fi

# Recreate download directory
rm -rf tmpDownload
mkdir tmpDownload
cd tmpDownload

echo "Trying to connect to source host and display files ..."
echo "Trying to connect to source host ..."

# FTP connection
if [[ $sourceConnectionMode == "2" ]]; then

ftp -n $sourceFtpHostName <<END_SCRIPT
quote USER $sourceFtpUsername
quote PASS $sourceFtpPassword
ftp -n $sourceHostName <<END_SCRIPT
quote USER $sourceUsername
quote PASS $sourcePassword
ls
quit
END_SCRIPT

echo
read -p "[Source] Path to download recursively ('/' for current directory): " sourceFtpDownloadPath
echo
if [[ $sourceFtpDownloadPath != /* ]]; then
sourceFtpDownloadPath="/"$sourceFtpDownloadPath
fi
if [[ $sourceFtpDownloadPath != */ ]]; then
sourceFtpDownloadPath=$sourceFtpDownloadPath"/"
fi
echo "Downloading files and folders from $sourceDownloadPath ..."

echo "Downloading files and folders ..."

# recreate download directory
rm -rf tmpDownload
mkdir tmpDownload
cd tmpDownload
# Download files
sourceCutDirsNumber=$(echo "$destinationUploadPath" | tr -cd "/" | wc -c)
if [[ $sourceDownloadPath != "/" ]]; then
sourceCutDirsNumber=$((sourceCutDirsNumber + 1))
fi

# download files
sourceCutDirsNumber=$(echo "$destinationFtpUploadPath" | tr -cd "/" | wc -c)
if [[ $sourceFtpDownloadPath != "/" ]]; then
if [[ $sourceDownloadMode == "2" ]]; then
sourceCutDirsNumber=$((sourceCutDirsNumber + 1))
fi
wget -r -N -l inf -q --show-progress -np -nH --cut-dirs $sourceCutDirsNumber ftp://$sourceFtpUsername:$sourceFtpPassword@$sourceFtpHostName$sourceFtpDownloadPath

echo
echo "Successful downloaded files and folders from source host!"
wget -r -N -l inf -q --show-progress -np -nH --cut-dirs $sourceCutDirsNumber ftp://$sourceUsername:$sourcePassword@$sourceHostName$sourceDownloadPath

# SSH / SFTP connection
else
echo "SSH connection will be implemented soon ..."
exit 0
if [[ $sourceDownloadMode == "2" ]]; then
if [[ $sourceDownloadPath != *\* ]]; then
sourceDownloadPath=$sourceDownloadPath"*"
fi
fi

echo "Downloading files and folders from $sourceDownloadPath ..."

sshpass -p "$sourcePassword" scp -r -v -o "StrictHostKeyChecking=no" $sourceUsername@$sourceHostName:$sourceDownloadPath $PWD

fi

echo
echo "Successful downloaded files and folders from source host!"

# *****************
# ** DESTINATION **
# *****************

echo
read -p "Do you want to use SSH (1) or FTP (2) for the connection to the destination host? [1/2]: " destinationConnectionMode
read -p "Do you want to use SSH / SFTP (1) or FTP (2) for the connection to the destination host? [1/2]: " destinationConnectionMode

# FTP connection
if [[ $destinationConnectionMode == "2" ]]; then
read -p "[Destination] Host name / URL / IP: " destinationFtpHostName
read -p "[Destination] Host name / URL / IP: " destinationHostName

read -p "[Destination] Username: " destinationUsername

stty -echo
printf "[Destination] Password: "
read destinationPassword
stty echo

echo
read -p "[Source] Path to upload ('/' for current directory): " destinationUploadPath
echo

read -p "[Destination] Username: " destinationFtpUsername
echo "Trying to connect to destination host ..."

stty -echo
printf "[Destination] Password: "
read destinationFtpPassword
stty echo
printf "\n"
if [[ $destinationUploadPath != /* ]]; then
destinationUploadPath="/"$destinationUploadPath
fi
if [[ $destinationUploadPath != */ ]]; then
destinationUploadPath=$destinationUploadPath"/"
fi

echo "Trying to connect to destination host and display files ..."
# FTP connection
if [[ $destinationConnectionMode == "2" ]]; then

ftp -n $destinationFtpHostName <<END_SCRIPT
quote USER $destinationFtpUsername
quote PASS $destinationFtpPassword
ftp -n $destinationHostName <<END_SCRIPT
quote USER $destinationUsername
quote PASS $destinationPassword
ls
quit
END_SCRIPT

echo
read -p "[Source] Path to upload ('/' for current directory): " destinationFtpUploadPath
echo

if [[ $destinationFtpUploadPath != /* ]]; then
destinationFtpUploadPath="/"$destinationFtpUploadPath
fi
if [[ $destinationFtpUploadPath != */ ]]; then
destinationFtpUploadPath=$destinationFtpUploadPath"/"
fi

localFilePath=$(pwd)

echo "Uploading files and folders ..."
echo "Uploading files and folders to $destinationUploadPath ..."
echo

lftp -e "set ftp:ssl-allow no; mirror -R $localFilePath/ $destinationFtpUploadPath ; quit" -u $destinationFtpUsername,$destinationFtpPassword $destinationFtpHostName
lftp -e "set ftp:ssl-allow no; mirror -R $PWD/ $destinationUploadPath ; quit" -u $destinationUsername,$destinationPassword $destinationHostName
# SSH / SFTP connection
else
echo "Uploading files and folders to $destinationUploadPath ..."

cd ..
rm -rf tmpDownload
sshpass -p "$destinationPassword" scp -r -v -o "StrictHostKeyChecking=no" $PWD/* $destinationUsername@$destinationHostName:$destinationUploadPath

echo
echo "Successful uploaded files and folders to destination host!"
else
echo "SSH connection will be implemented soon ..."
exit 0
fi

echo
echo "Successful uploaded files and folders to destination host!"

echo "Deleting temporary download folder ..."

cd ..
rm -rf tmpDownload

echo "Done!"

exit 0

0 comments on commit 41e742a

Please sign in to comment.