v-add-backup-host 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/bash
  2. # info: add backup host
  3. # options: TYPE HOST USERNAME PASSWORD [PATH] [PORT]
  4. #
  5. # This function adds a backup host
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. type=$1
  11. host=$2
  12. user=$3
  13. password=$4; HIDE=4
  14. path=${5-/backup}
  15. port=$6
  16. # Includes
  17. source $VESTA/func/main.sh
  18. source $VESTA/conf/vesta.conf
  19. # Defining ftp command function
  20. ftpc() {
  21. ftp -p -n $host $port <<EOF
  22. quote USER $user
  23. quote PASS $password
  24. binary
  25. $1
  26. $2
  27. $3
  28. quit
  29. EOF
  30. }
  31. # Defining sftp command function
  32. sftpc() {
  33. expect -f "-" <<EOF "$@"
  34. set count 0
  35. spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$port $user@$host
  36. expect {
  37. "password:" {
  38. send "$password\r"
  39. exp_continue
  40. }
  41. -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
  42. set count \$argc
  43. set output "Disconnected."
  44. set rc $E_FTP
  45. exp_continue
  46. }
  47. -re ".*denied.*(publickey|password)." {
  48. set output "Permission denied, wrong publickey or password."
  49. set rc $E_CONNECT
  50. }
  51. "sftp>" {
  52. if {\$count < \$argc} {
  53. set arg [lindex \$argv \$count]
  54. send "\$arg\r"
  55. incr count
  56. } else {
  57. send "exit\r"
  58. set output "Disconnected."
  59. if {[info exists rc] != 1} {
  60. set rc $OK
  61. }
  62. }
  63. exp_continue
  64. }
  65. timeout {
  66. set output "Connection timeout."
  67. set rc $E_CONNECT
  68. }
  69. }
  70. if {[info exists output] == 1} {
  71. puts "\$output"
  72. }
  73. exit \$rc
  74. EOF
  75. }
  76. #----------------------------------------------------------#
  77. # Verifications #
  78. #----------------------------------------------------------#
  79. if [ "$type" != 'local' ];then
  80. check_args '4' "$#" "TYPE HOST USERNAME PASSWORD [PATH] [PORT]"
  81. is_format_valid 'user' 'host' 'path' 'port'
  82. is_password_valid
  83. if [ "$type" = 'sftp' ]; then
  84. which expect >/dev/null 2>&1
  85. check_result $? "expect command not found" $E_NOTEXIST
  86. fi
  87. host "$host" >/dev/null 2>&1
  88. check_result $? "host connection failed" "$E_CONNECT"
  89. fi
  90. #----------------------------------------------------------#
  91. # Action #
  92. #----------------------------------------------------------#
  93. # Checking network connection
  94. if [ "$type" = 'ftp' ]; then
  95. if [ -z $port ]; then
  96. port=21
  97. fi
  98. fconn=$(ftpc 2>&1)
  99. ferror=$(echo $fconn |\
  100. grep -i -e failed -e error -e "can't" -e "not conn" -e "incorrect")
  101. if [ ! -z "$ferror" ]; then
  102. echo "Error: can't login to ftp $user@$host"
  103. log_event "$E_CONNECT" "$ARGUMENTS"
  104. exit $E_CONNECT
  105. fi
  106. # Checking write permissions
  107. if [ -z $path ]; then
  108. ftmpdir="vst.bK76A9SUkt"
  109. else
  110. ftpc "mkdir $path" > /dev/null 2>&1
  111. ftmpdir="$path/vst.bK76A9SUkt"
  112. fi
  113. ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir"|grep -v Trying)
  114. if [ ! -z "$ftp_result" ] ; then
  115. echo "$ftp_result"
  116. rm -rf $tmpdir
  117. echo "Error: can't create $ftmpdir folder on the ftp"
  118. log_event "$E_FTP" "$ARGUMENTS"
  119. exit $E_FTP
  120. fi
  121. fi
  122. if [ "$type" = 'sftp' ]; then
  123. if [ -z $port ]; then
  124. port=22
  125. fi
  126. if [ -z $path ]; then
  127. sftmpdir="vst.bK76A9SUkt"
  128. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  129. else
  130. if sftpc "mkdir $path" > /dev/null 2>&1 ; then
  131. sftmpdir="$path/vst.bK76A9SUkt"
  132. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  133. else
  134. sftmpdir="$path/vst.bK76A9SUkt"
  135. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  136. fi
  137. fi
  138. rc=$?
  139. if [[ "$rc" != 0 ]]; then
  140. case $rc in
  141. $E_CONNECT) echo "Error: can't login to sftp $user@$host";;
  142. $E_FTP) echo "Error: can't create temp folder on the sftp host";;
  143. esac
  144. log_event "$rc" "$ARGUMENTS"
  145. exit "$rc"
  146. fi
  147. fi
  148. # Adding backup host
  149. if [ $type != 'local' ]; then
  150. time_n_date=$(date +'%T %F')
  151. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  152. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  153. str="HOST='$host'\nUSERNAME='$user'\nPASSWORD='$password'"
  154. str="$str\nBPATH='$path'\nPORT='$port'\nTIME='$time'\nDATE='$date'"
  155. echo -e "$str" > $VESTA/conf/$type.backup.conf
  156. chmod 660 $VESTA/conf/$type.backup.conf
  157. fi
  158. #----------------------------------------------------------#
  159. # Vesta #
  160. #----------------------------------------------------------#
  161. # Update vesta.conf
  162. if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
  163. echo "BACKUP_SYSTEM='$type'" >> $VESTA/conf/vesta.conf
  164. else
  165. bckp=$(echo "$BACKUP_SYSTEM,$type" |\
  166. sed "s/,/\n/g"|\
  167. sort -r -u |\
  168. sed "/^$/d"|\
  169. sed ':a;N;$!ba;s/\n/,/g')
  170. sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
  171. fi
  172. # Logging
  173. log_event "$OK" "$ARGUMENTS"
  174. exit