v-add-backup-host 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 'host'
  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. fi
  89. #----------------------------------------------------------#
  90. # Action #
  91. #----------------------------------------------------------#
  92. # Checking network connection
  93. if [ "$type" = 'ftp' ]; then
  94. if [ -z $port ]; then
  95. port=21
  96. fi
  97. fconn=$(ftpc 2>&1)
  98. ferror=$(echo $fconn |\
  99. grep -i -e failed -e error -e "can't" -e "not conn" -e "incorrect")
  100. if [ ! -z "$ferror" ]; then
  101. echo "Error: can't login to ftp $user@$host"
  102. log_event "$E_CONNECT" "$ARGUMENTS"
  103. exit $E_CONNECT
  104. fi
  105. # Checking write permissions
  106. if [ -z $path ]; then
  107. ftmpdir="vst.bK76A9SUkt"
  108. else
  109. ftpc "mkdir $path" > /dev/null 2>&1
  110. ftmpdir="$path/vst.bK76A9SUkt"
  111. fi
  112. ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir"|grep -v Trying)
  113. if [ ! -z "$ftp_result" ] ; then
  114. echo "$ftp_result"
  115. rm -rf $tmpdir
  116. echo "Error: can't create $ftmpdir folder on the ftp"
  117. log_event "$E_FTP" "$ARGUMENTS"
  118. exit $E_FTP
  119. fi
  120. fi
  121. if [ "$type" = 'sftp' ]; then
  122. if [ -z $port ]; then
  123. port=22
  124. fi
  125. if [ -z $path ]; then
  126. sftmpdir="vst.bK76A9SUkt"
  127. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  128. else
  129. if sftpc "mkdir $path" > /dev/null 2>&1 ; then
  130. sftmpdir="$path/vst.bK76A9SUkt"
  131. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  132. else
  133. sftmpdir="$path/vst.bK76A9SUkt"
  134. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  135. fi
  136. fi
  137. rc=$?
  138. if [[ "$rc" != 0 ]]; then
  139. case $rc in
  140. $E_CONNECT) echo "Error: can't login to sftp $user@$host";;
  141. $E_FTP) echo "Error: can't create temp folder on the sftp host";;
  142. esac
  143. log_event "$rc" "$ARGUMENTS"
  144. exit "$rc"
  145. fi
  146. fi
  147. # Adding backup host
  148. if [ $type != 'local' ]; then
  149. time_n_date=$(date +'%T %F')
  150. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  151. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  152. str="HOST='$host'\nUSERNAME='$user'\nPASSWORD='$password'"
  153. str="$str\nBPATH='$path'\nPORT='$port'\nTIME='$time'\nDATE='$date'"
  154. echo -e "$str" > $VESTA/conf/$type.backup.conf
  155. chmod 660 $VESTA/conf/$type.backup.conf
  156. fi
  157. #----------------------------------------------------------#
  158. # Vesta #
  159. #----------------------------------------------------------#
  160. # Update vesta.conf
  161. if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
  162. echo "BACKUP_SYSTEM='$type'" >> $VESTA/conf/vesta.conf
  163. else
  164. bckp=$(echo "$BACKUP_SYSTEM,$type" |\
  165. sed "s/,/\n/g"|\
  166. sort -r -u |\
  167. sed "/^$/d"|\
  168. sed ':a;N;$!ba;s/\n/,/g')
  169. sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
  170. fi
  171. # Logging
  172. log_event "$OK" "$ARGUMENTS"
  173. exit