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