Skip to content

Commit 41e742a

Browse files
committed
Create beta release
- add SSH option (fixes #1) - add folder or content transfer option (fixes #3) - small improvements
1 parent d04f4ec commit 41e742a

File tree

3 files changed

+103
-76
lines changed

3 files changed

+103
-76
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ A simple bash script to move files over FTP / SSH from a source to a (different)
2121

2222
## About The Project
2323

24-
**Current version**: 0.0.1 (Alpha Phase)
24+
**Current version**: 0.1 (Beta Phase)
2525

2626
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!
2727

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

3333
### Pre-Requirements
3434

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

4041
### Installation
4142

@@ -58,7 +59,7 @@ sh install.sh
5859
### Important Information
5960

6061
- 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
61-
- To get rid of some certificate verifiaction errors, the scripts doesn't make certificate checks
62+
- To get rid of some certificate verification errors, the scripts doesn't make certificate checks
6263
- Remember to open FTP / SSH relevant ports in your firewall
6364
- Use it at your own risk!
6465

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

7172
## Contributing
7273

73-
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).
74+
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).
7475

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

install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
sudo apt install ftp -y
44
sudo apt install lftp -y
5+
sudo apt install sshpass -y
56

67
chmod +x moveServerFiles.sh

moveServerFiles.sh

Lines changed: 97 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,114 +4,139 @@
44
# ***** SOURCE *****
55
# ******************
66

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

9-
# FTP connection
10-
if [[ $sourceConnectionMode == "2" ]]; then
11-
read -p "[Source] Host name / URL / IP: " sourceFtpHostName
10+
read -p "[Source] Host name / URL / IP: " sourceHostName
11+
12+
read -p "[Source] Username: " sourceUsername
13+
14+
stty -echo
15+
printf "[Source] Password: "
16+
read sourcePassword
17+
stty echo
18+
19+
echo
20+
read -p "[Source] Path to download recursively ('/' for current directory): " sourceDownloadPath
1221

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

15-
stty -echo
16-
printf "[Source] Password: "
17-
read sourceFtpPassword
18-
stty echo
19-
printf "\n"
25+
if [[ $sourceDownloadPath != /* ]]; then
26+
sourceDownloadPath="/"$sourceDownloadPath
27+
fi
28+
if [[ $sourceDownloadPath != */ ]]; then
29+
sourceDownloadPath=$sourceDownloadPath"/"
30+
fi
31+
32+
# Recreate download directory
33+
rm -rf tmpDownload
34+
mkdir tmpDownload
35+
cd tmpDownload
2036

21-
echo "Trying to connect to source host and display files ..."
37+
echo "Trying to connect to source host ..."
38+
39+
# FTP connection
40+
if [[ $sourceConnectionMode == "2" ]]; then
2241

23-
ftp -n $sourceFtpHostName <<END_SCRIPT
24-
quote USER $sourceFtpUsername
25-
quote PASS $sourceFtpPassword
42+
ftp -n $sourceHostName <<END_SCRIPT
43+
quote USER $sourceUsername
44+
quote PASS $sourcePassword
2645
ls
2746
quit
2847
END_SCRIPT
2948

30-
echo
31-
read -p "[Source] Path to download recursively ('/' for current directory): " sourceFtpDownloadPath
32-
echo
33-
if [[ $sourceFtpDownloadPath != /* ]]; then
34-
sourceFtpDownloadPath="/"$sourceFtpDownloadPath
35-
fi
36-
if [[ $sourceFtpDownloadPath != */ ]]; then
37-
sourceFtpDownloadPath=$sourceFtpDownloadPath"/"
38-
fi
49+
echo "Downloading files and folders from $sourceDownloadPath ..."
3950

40-
echo "Downloading files and folders ..."
41-
42-
# recreate download directory
43-
rm -rf tmpDownload
44-
mkdir tmpDownload
45-
cd tmpDownload
51+
# Download files
52+
sourceCutDirsNumber=$(echo "$destinationUploadPath" | tr -cd "/" | wc -c)
53+
if [[ $sourceDownloadPath != "/" ]]; then
54+
sourceCutDirsNumber=$((sourceCutDirsNumber + 1))
55+
fi
4656

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

54-
echo
55-
echo "Successful downloaded files and folders from source host!"
61+
wget -r -N -l inf -q --show-progress -np -nH --cut-dirs $sourceCutDirsNumber ftp://$sourceUsername:$sourcePassword@$sourceHostName$sourceDownloadPath
62+
63+
# SSH / SFTP connection
5664
else
57-
echo "SSH connection will be implemented soon ..."
58-
exit 0
65+
if [[ $sourceDownloadMode == "2" ]]; then
66+
if [[ $sourceDownloadPath != *\* ]]; then
67+
sourceDownloadPath=$sourceDownloadPath"*"
68+
fi
69+
fi
70+
71+
echo "Downloading files and folders from $sourceDownloadPath ..."
72+
73+
sshpass -p "$sourcePassword" scp -r -v -o "StrictHostKeyChecking=no" $sourceUsername@$sourceHostName:$sourceDownloadPath $PWD
74+
5975
fi
6076

77+
echo
78+
echo "Successful downloaded files and folders from source host!"
79+
6180
# *****************
6281
# ** DESTINATION **
6382
# *****************
6483

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

68-
# FTP connection
69-
if [[ $destinationConnectionMode == "2" ]]; then
70-
read -p "[Destination] Host name / URL / IP: " destinationFtpHostName
87+
read -p "[Destination] Host name / URL / IP: " destinationHostName
88+
89+
read -p "[Destination] Username: " destinationUsername
90+
91+
stty -echo
92+
printf "[Destination] Password: "
93+
read destinationPassword
94+
stty echo
95+
96+
echo
97+
read -p "[Source] Path to upload ('/' for current directory): " destinationUploadPath
98+
echo
7199

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

74-
stty -echo
75-
printf "[Destination] Password: "
76-
read destinationFtpPassword
77-
stty echo
78-
printf "\n"
102+
if [[ $destinationUploadPath != /* ]]; then
103+
destinationUploadPath="/"$destinationUploadPath
104+
fi
105+
if [[ $destinationUploadPath != */ ]]; then
106+
destinationUploadPath=$destinationUploadPath"/"
107+
fi
79108

80-
echo "Trying to connect to destination host and display files ..."
109+
# FTP connection
110+
if [[ $destinationConnectionMode == "2" ]]; then
81111

82-
ftp -n $destinationFtpHostName <<END_SCRIPT
83-
quote USER $destinationFtpUsername
84-
quote PASS $destinationFtpPassword
112+
ftp -n $destinationHostName <<END_SCRIPT
113+
quote USER $destinationUsername
114+
quote PASS $destinationPassword
85115
ls
86116
quit
87117
END_SCRIPT
88118

89-
echo
90-
read -p "[Source] Path to upload ('/' for current directory): " destinationFtpUploadPath
91-
echo
92-
93-
if [[ $destinationFtpUploadPath != /* ]]; then
94-
destinationFtpUploadPath="/"$destinationFtpUploadPath
95-
fi
96-
if [[ $destinationFtpUploadPath != */ ]]; then
97-
destinationFtpUploadPath=$destinationFtpUploadPath"/"
98-
fi
99-
100-
localFilePath=$(pwd)
101-
102-
echo "Uploading files and folders ..."
119+
echo "Uploading files and folders to $destinationUploadPath ..."
103120
echo
104121

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

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

110-
echo
111-
echo "Successful uploaded files and folders to destination host!"
112-
else
113-
echo "SSH connection will be implemented soon ..."
114129
exit 0
115130
fi
116131

132+
echo
133+
echo "Successful uploaded files and folders to destination host!"
134+
135+
echo "Deleting temporary download folder ..."
136+
137+
cd ..
138+
rm -rf tmpDownload
139+
140+
echo "Done!"
141+
117142
exit 0

0 commit comments

Comments
 (0)