Advertisement
smileface

ftp_upload.php

Feb 26th, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. [root@server scripts]# cat ftp_upload.php
  2. #!/bin/bash
  3. VERSION=1.2
  4. CURL=/usr/local/bin/curl
  5. if [ ! -e ${CURL} ]; then
  6. CURL=/usr/bin/curl
  7. fi
  8. DU=/usr/bin/du
  9. BC=/usr/bin/bc
  10. EXPR=/usr/bin/expr
  11. TOUCH=/bin/touch
  12. PORT=${ftp_port}
  13. FTPS=0
  14.  
  15. MD5=${ftp_md5}
  16.  
  17. if [ "${ftp_secure}" = "ftps" ]; then
  18. FTPS=1
  19. fi
  20.  
  21. CURL_TLS_HELP=$(${CURL} --help tls)
  22. CURL_VERSION=$(${CURL} --version | head -n 1 | cut -d ' ' -f 2)
  23. int_version() {
  24. local major minor patch
  25. major=$(cut -d . -f 1 <<< "$1")
  26. minor=$(cut -d . -f 2 <<< "$1")
  27. patch=$(cut -d . -f 3 <<< "$1")
  28. printf "%03d%03d%03d" "${major}" "${minor}" "${patch}"
  29. }
  30.  
  31. SSL_ARGS=""
  32. if grep -q 'ftp-ssl-reqd' <<< "${CURL_TLS_HELP}"; then
  33. SSL_ARGS="${SSL_ARGS} --ftp-ssl-reqd"
  34. elif grep -q 'ssl-reqd' <<< "${CURL_TLS_HELP}"; then
  35. SSL_ARGS="${SSL_ARGS} --ssl-reqd"
  36. fi
  37.  
  38. # curl 7.77.0 fixed gnutls ignoring --tls-max if --tlsv1.x was not specified.
  39. # https://curl.se/bug/?i=6998
  40. #
  41. # curl 7.61.0 fixes for openssl to treat --tlsv1.x as minimum required version instead of exact version
  42. # https://curl.se/bug/?i=2691
  43. #
  44. # curl 7.54.0 introduced --max-tls option and changed --tlsv1.x behaviur to be min version
  45. # https://curl.se/bug/?i=1166
  46. if [ "$(int_version "${CURL_VERSION}")" -ge "$(int_version '7.54.0')" ]; then
  47. SSL_ARGS="${SSL_ARGS} --tlsv1.1"
  48. fi
  49.  
  50. # curl 7.78.0 fixed FTP upload TLS 1.3 bug, we add `--tls-max 1.2` for older versions.
  51. # https://curl.se/bug/?i=7095
  52. if [ "$(int_version "${CURL_VERSION}")" -lt "$(int_version '7.78.0')" ] && grep -q 'tls-max' <<< "${CURL_TLS_HELP}"; then
  53. SSL_ARGS="${SSL_ARGS} --tls-max 1.2"
  54.  
  55. # curls older than 7.61.0 needs --tlsv.x parameter for --tls-max to work correctly
  56. # https://curl.se/bug/?i=2571 - openssl: acknowledge --tls-max for default version too
  57. fi
  58.  
  59. #######################################################
  60. # SETUP
  61.  
  62. if [ ! -e $TOUCH ] && [ -e /usr/bin/touch ]; then
  63. TOUCH=/usr/bin/touch
  64. fi
  65. if [ ! -x ${EXPR} ] && [ -x /bin/expr ]; then
  66. EXPR=/bin/expr
  67. fi
  68.  
  69. if [ ! -e "${ftp_local_file}" ]; then
  70. echo "Cannot find backup file ${ftp_local_file} to upload";
  71.  
  72. /bin/ls -la ${ftp_local_path}
  73.  
  74. /bin/df -h
  75.  
  76. exit 11;
  77. fi
  78.  
  79. get_md5() {
  80. MF=$1
  81.  
  82. MD5SUM=/usr/bin/md5sum
  83. if [ ! -x ${MD5SUM} ]; then
  84. return
  85. fi
  86.  
  87. if [ ! -e ${MF} ]; then
  88. return
  89. fi
  90.  
  91. FMD5=`$MD5SUM $MF | cut -d\ -f1`
  92.  
  93. echo "${FMD5}"
  94. }
  95.  
  96. #######################################################
  97.  
  98. CFG=${ftp_local_file}.cfg
  99. /bin/rm -f $CFG
  100. $TOUCH $CFG
  101. /bin/chmod 600 $CFG
  102.  
  103. RET=0;
  104.  
  105. #######################################################
  106. # FTP
  107. upload_file_ftp()
  108. {
  109. if [ ! -e ${CURL} ]; then
  110. echo "";
  111. echo "*** Backup not uploaded ***";
  112. echo "Please install curl";
  113. echo "";
  114. exit 10;
  115. fi
  116.  
  117. /bin/echo "user = \"$ftp_username:$ftp_password_esc_double_quote\"" >> $CFG
  118.  
  119. if [ ! -s ${CFG} ]; then
  120. echo "${CFG} is empty. curl is not going to be happy about it.";
  121. ls -la ${CFG}
  122. ls -la ${ftp_local_file}
  123. df -h
  124. fi
  125.  
  126. #ensure ftp_path ends with /
  127. ENDS_WITH_SLASH=`echo "$ftp_path" | grep -c '/$'`
  128. if [ "${ENDS_WITH_SLASH}" -eq 0 ]; then
  129. ftp_path=${ftp_path}/
  130. fi
  131.  
  132. ${CURL} --config ${CFG} --silent --show-error --ftp-create-dirs --upload-file $ftp_local_file ftp://$ftp_ip:${PORT}/$ftp_path$ftp_remote_file 2>&1
  133. RET=$?
  134.  
  135. if [ "${RET}" -ne 0 ]; then
  136. echo "curl return code: $RET";
  137. fi
  138. }
  139.  
  140. #######################################################
  141. # FTPS
  142. upload_file_ftps()
  143. {
  144. if [ ! -e ${CURL} ]; then
  145. echo "";
  146. echo "*** Backup not uploaded ***";
  147. echo "Please install curl";
  148. echo "";
  149. exit 10;
  150. fi
  151.  
  152. /bin/echo "user = \"$ftp_username:$ftp_password_esc_double_quote\"" >> $CFG
  153.  
  154. if [ ! -s ${CFG} ]; then
  155. echo "${CFG} is empty. curl is not going to be happy about it.";
  156. ls -la ${CFG}
  157. ls -la ${ftp_local_file}
  158. df -h
  159. fi
  160.  
  161. #ensure ftp_path ends with /
  162. ENDS_WITH_SLASH=`echo "$ftp_path" | grep -c '/$'`
  163. if [ "${ENDS_WITH_SLASH}" -eq 0 ]; then
  164. ftp_path=${ftp_path}/
  165. fi
  166.  
  167. ${CURL} --config ${CFG} ${SSL_ARGS} -k --silent --show-error --ftp-create-dirs --upload-file $ftp_local_file ftp://$ftp_ip:${PORT}/$ftp_path$ftp_remote_file 2>&1
  168. RET=$?
  169.  
  170. if [ "${RET}" -ne 0 ]; then
  171. echo "curl return code: $RET";
  172. fi
  173. }
  174.  
  175. #######################################################
  176. # Start
  177.  
  178. if [ "${FTPS}" = "1" ]; then
  179. upload_file_ftps
  180. else
  181. upload_file_ftp
  182. fi
  183.  
  184. if [ "${RET}" = "0" ] && [ "${MD5}" = "1" ]; then
  185. MD5_FILE=${ftp_local_file}.md5
  186. M=`get_md5 ${ftp_local_file}`
  187. if [ "${M}" != "" ]; then
  188. echo "${M}" > ${MD5_FILE}
  189.  
  190. ftp_local_file=${MD5_FILE}
  191. ftp_remote_file=${ftp_remote_file}.md5
  192.  
  193. if [ "${FTPS}" = "1" ]; then
  194. upload_file_ftps
  195. else
  196. upload_file
  197. fi
  198. fi
  199. fi
  200.  
  201. /bin/rm -f $CFG
  202.  
  203. exit $RET
  204.  
  205. [root@server scripts]#
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement