|
4 | 4 | # ***** SOURCE ***** |
5 | 5 | # ****************** |
6 | 6 |
|
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 |
8 | 9 |
|
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 |
12 | 21 |
|
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 |
14 | 24 |
|
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 |
20 | 36 |
|
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 |
22 | 41 |
|
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 |
26 | 45 | ls |
27 | 46 | quit |
28 | 47 | END_SCRIPT |
29 | 48 |
|
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 ..." |
39 | 50 |
|
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 |
46 | 56 |
|
47 | | - # download files |
48 | | - sourceCutDirsNumber=$(echo "$destinationFtpUploadPath" | tr -cd "/" | wc -c) |
49 | | - if [[ $sourceFtpDownloadPath != "/" ]]; then |
| 57 | + if [[ $sourceDownloadMode == "2" ]]; then |
50 | 58 | sourceCutDirsNumber=$((sourceCutDirsNumber + 1)) |
51 | 59 | fi |
52 | | - wget -r -N -l inf -q --show-progress -np -nH --cut-dirs $sourceCutDirsNumber ftp://$sourceFtpUsername:$sourceFtpPassword@$sourceFtpHostName$sourceFtpDownloadPath |
53 | 60 |
|
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 |
56 | 64 | 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 | + |
59 | 75 | fi |
60 | 76 |
|
| 77 | +echo |
| 78 | +echo "Successful downloaded files and folders from source host!" |
| 79 | + |
61 | 80 | # ***************** |
62 | 81 | # ** DESTINATION ** |
63 | 82 | # ***************** |
64 | 83 |
|
65 | 84 | 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 |
67 | 86 |
|
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 |
71 | 99 |
|
72 | | - read -p "[Destination] Username: " destinationFtpUsername |
| 100 | +echo "Trying to connect to destination host ..." |
73 | 101 |
|
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 |
79 | 108 |
|
80 | | - echo "Trying to connect to destination host and display files ..." |
| 109 | +# FTP connection |
| 110 | +if [[ $destinationConnectionMode == "2" ]]; then |
81 | 111 |
|
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 |
85 | 115 | ls |
86 | 116 | quit |
87 | 117 | END_SCRIPT |
88 | 118 |
|
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 ..." |
103 | 120 | echo |
104 | 121 |
|
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 ..." |
106 | 126 |
|
107 | | - cd .. |
108 | | - rm -rf tmpDownload |
| 127 | + sshpass -p "$destinationPassword" scp -r -v -o "StrictHostKeyChecking=no" $PWD/* $destinationUsername@$destinationHostName:$destinationUploadPath |
109 | 128 |
|
110 | | - echo |
111 | | - echo "Successful uploaded files and folders to destination host!" |
112 | | -else |
113 | | - echo "SSH connection will be implemented soon ..." |
114 | 129 | exit 0 |
115 | 130 | fi |
116 | 131 |
|
| 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 | + |
117 | 142 | exit 0 |
0 commit comments