Skip to content

Commit cb99788

Browse files
committed
improvements on sftp backups
1 parent 1bcdef6 commit cb99788

File tree

5 files changed

+135
-146
lines changed

5 files changed

+135
-146
lines changed

bin/v-add-backup-ftp-host

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@
1212
# Argument defenition
1313
host=$1
1414
ftp_user=$2
15-
ftp_password=$3
15+
password=$3
1616
ftp_path=${4-/backup}
1717
ftp_port=${5-21}
18-
A3='******'
1918

2019
# Includes
2120
source $VESTA/func/main.sh
2221
source $VESTA/conf/vesta.conf
2322

23+
# Hiding password
24+
A3='******'
25+
EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
26+
2427
# Defining ftp command function
2528
ftpc() {
2629
ftp -p -n $host $ftp_port <<EOF
2730
quote USER $ftp_user
28-
quote PASS $ftp_password
31+
quote PASS $password
2932
binary
3033
$1
3134
$2
@@ -40,18 +43,20 @@ EOF
4043
#----------------------------------------------------------#
4144

4245
check_args '3' "$#" "HOST USERNAME PASSWORD [PATH] [PORT]"
43-
validate_format 'host' 'ftp_user' 'ftp_password'
46+
validate_format 'host' 'ftp_user'
47+
is_password_valid
4448

4549

4650
#----------------------------------------------------------#
4751
# Action #
4852
#----------------------------------------------------------#
4953

5054
# Checking network connection
51-
fconn=$(ftpc)
52-
ferror=$(echo $fconn |grep -i -e failed -e error -e "Can't" -e "not conn")
55+
fconn=$(ftpc 2>&1)
56+
ferror=$(echo $fconn |\
57+
grep -i -e failed -e error -e "can't" -e "not conn" -e "incorrect")
5358
if [ ! -z "$ferror" ]; then
54-
echo "Error: can't login to ftp"
59+
echo "Error: can't login to ftp server"
5560
log_event "$E_CONNECT" "$EVENT"
5661
exit $E_CONNECT
5762
fi
@@ -71,7 +76,7 @@ fi
7176
# Adding backup host
7277
echo "HOST='$host'
7378
USERNAME='$ftp_user'
74-
PASSWORD='$ftp_password'
79+
PASSWORD='$password'
7580
BPATH='$ftp_path'
7681
PORT='$ftp_port'
7782
TIME='$TIME'
@@ -84,7 +89,7 @@ chmod 660 $VESTA/conf/ftp.backup.conf
8489
#----------------------------------------------------------#
8590

8691
# Update vesta.conf
87-
if [ -z "$(grep LANGUAGE $VESTA/conf/vesta.conf)" ]; then
92+
if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
8893
echo "BACKUP_SYSTEM='ftp'" >> $VESTA/conf/vesta.conf
8994
else
9095
bckp=$(echo "$BACKUP_SYSTEM,ftp" |\

bin/v-add-backup-sftp-host

Lines changed: 72 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# info: add backup sftp host
3-
# options: HOST USERNAME [PASSWORD] [PATH] [PORT]
3+
# options: HOST USERNAME PASSWORD [PATH] [PORT]
44
#
55
# The function adds sftp host for system backups
66

@@ -11,87 +11,84 @@
1111
# Argument defenition
1212
sftp_host=$1
1313
sftp_user=$2
14-
sftp_password=${3-******}
14+
password=$3
1515
sftp_path=${4-backup}
1616
sftp_port=${5-22}
1717

18-
A3='******'
19-
2018
# Includes
2119
source $VESTA/func/main.sh
2220
source $VESTA/conf/vesta.conf
2321

24-
# Replace password with ******
25-
if [[ $A3 != '******' ]]
26-
then
27-
EVENT="${EVENT/$sftp_password/******}"
28-
fi
22+
# Hiding password
23+
A3='******'
24+
EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
2925

3026
# sftp command function
3127
sftpc() {
32-
expect -f "-" <<EOF "$@"
33-
set count 0
34-
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$sftp_port $sftp_user@$sftp_host
35-
expect {
36-
"password:" {
37-
send "$sftp_password\r"
38-
exp_continue
39-
}
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-
48-
-re ".*denied.*(publickey|password)." {
49-
set output "Permission denied, wrong publickey or password."
50-
set rc $E_CONNECT
51-
}
52-
53-
"sftp>" {
54-
if {\$count < \$argc} {
55-
set arg [lindex \$argv \$count]
56-
send "\$arg\r"
57-
incr count
58-
} else {
59-
send "exit\r"
60-
set output "Disconnected."
61-
if {[info exists rc] != 1} {
62-
set rc $OK
63-
}
64-
}
65-
exp_continue
66-
}
67-
68-
timeout {
69-
set output "Connection timeout."
70-
set rc $E_CONNECT
71-
}
72-
}
73-
74-
if {[info exists output] == 1} {
75-
puts "\$output"
76-
}
77-
78-
exit \$rc
28+
expect -f "-" <<EOF "$@"
29+
set count 0
30+
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o \
31+
Port=$sftp_port $sftp_user@$sftp_host
32+
expect {
33+
"password:" {
34+
send "$password\r"
35+
exp_continue
36+
}
37+
38+
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
39+
set count \$argc
40+
set output "Disconnected."
41+
set rc $E_FTP
42+
exp_continue
43+
}
44+
45+
-re ".*denied.*(publickey|password)." {
46+
set output "Permission denied, wrong publickey or password."
47+
set rc $E_CONNECT
48+
}
49+
50+
"sftp>" {
51+
if {\$count < \$argc} {
52+
set arg [lindex \$argv \$count]
53+
send "\$arg\r"
54+
incr count
55+
} else {
56+
send "exit\r"
57+
set output "Disconnected."
58+
if {[info exists rc] != 1} {
59+
set rc $OK
60+
}
61+
}
62+
exp_continue
63+
}
64+
65+
timeout {
66+
set output "Connection timeout."
67+
set rc $E_CONNECT
68+
}
69+
}
70+
71+
if {[info exists output] == 1} {
72+
puts "\$output"
73+
}
74+
75+
exit \$rc
7976
EOF
8077
}
8178

79+
8280
#----------------------------------------------------------#
8381
# Verifications #
8482
#----------------------------------------------------------#
8583

86-
check_expect=$(which expect)
87-
if [[ ! -n $check_expect ]]
88-
then
89-
echo "Error: \"expect\" utility not found"
90-
log_event "$E_NOTEXIST" "$EVENT"
91-
exit $E_NOTEXIST
84+
check_args '3' "$#" "HOST USERNAME PASSWORD [PATH] [PORT]"
85+
which expect >/dev/null 2>&1
86+
if [ $? -ne 0 ];then
87+
echo "Error: expect utility not found"
88+
log_event "$E_NOTEXIST" "$EVENT"
89+
exit $E_NOTEXIST
9290
fi
93-
94-
check_args '2' "$#" "HOST USERNAME [PASSWORD] [PATH] [PORT]"
91+
is_password_valid
9592

9693
#----------------------------------------------------------#
9794
# Action #
@@ -100,35 +97,35 @@ check_args '2' "$#" "HOST USERNAME [PASSWORD] [PATH] [PORT]"
10097
# Checking network connection and write permissions
10198
sftmpdir="$sftp_path/vst.bK76A9SUkt"
10299
sftpc "mkdir $sftp_path" > /dev/null 2>&1
103-
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir"
100+
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
104101
rc=$?
105-
if [[ "$rc" != 0 ]]
106-
then
107-
case $rc in
102+
if [[ "$rc" != 0 ]]; then
103+
case $rc in
108104
$E_CONNECT) echo "Error: can't login to sftp host";;
109-
$E_FTP) echo "Error: can't create temp folder on the sftp host";;
110-
esac
111-
log_event "$rc" "$EVENT"
112-
exit "$rc"
105+
$E_FTP) echo "Error: can't create temp folder on the sftp host";;
106+
esac
107+
log_event "$rc" "$EVENT"
108+
exit "$rc"
113109
fi
114110

115111
# Adding sftp backup config file
116112
echo "HOST='$sftp_host'
117113
USERNAME='$sftp_user'
118-
PASSWORD='$sftp_password'
114+
PASSWORD='$password'
119115
BPATH='$sftp_path'
120116
PORT='$sftp_port'
121117
TIME='$TIME'
122118
DATE='$DATE'" > $VESTA/conf/sftp.backup.conf
123119
chmod 660 $VESTA/conf/sftp.backup.conf
124120

121+
125122
#----------------------------------------------------------#
126123
# Vesta #
127124
#----------------------------------------------------------#
128125

129126
# Update vesta.conf
130-
if [ -z "$(grep LANGUAGE $VESTA/conf/vesta.conf)" ]; then
131-
echo "BACKUP_SYSTEM='ftp'" >> $VESTA/conf/vesta.conf
127+
if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
128+
echo "BACKUP_SYSTEM='sftp'" >> $VESTA/conf/vesta.conf
132129
else
133130
bckp=$(echo "$BACKUP_SYSTEM,sftp" |\
134131
sed "s/,/\n/g"|\
@@ -139,7 +136,6 @@ else
139136
fi
140137

141138
# Logging
142-
echo "$sftp_host/$sftp_path successfully added as backup destination"
143139
log_event "$OK" "$EVENT"
144140

145141
exit

0 commit comments

Comments
 (0)