v-add-backup-host 5.4 KB

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