Skip to content

Commit 003cbbe

Browse files
committed
union calls for FTP/SFTP backups
1 parent 7a9c937 commit 003cbbe

File tree

5 files changed

+106
-206
lines changed

5 files changed

+106
-206
lines changed

bin/v-add-backup-ftp-host

Lines changed: 0 additions & 106 deletions
This file was deleted.
Lines changed: 92 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
11
#!/bin/bash
2-
# info: add backup sftp host
3-
# options: HOST USERNAME PASSWORD [PATH] [PORT]
2+
# info: add backup host
3+
# options: TYPE HOST USERNAME PASSWORD [PATH] [PORT]
44
#
5-
# The function adds sftp host for system backups
5+
# The function adds backup host
6+
67

78
#----------------------------------------------------------#
89
# Variable&Function #
910
#----------------------------------------------------------#
1011

1112
# Argument defenition
12-
sftp_host=$1
13-
sftp_user=$2
14-
password=$3
15-
sftp_path=${4-backup}
16-
sftp_port=${5-22}
13+
type=$1
14+
host=$2
15+
user=$3
16+
password=$4
17+
path=${5-/backup}
18+
port=$6
1719

1820
# Includes
1921
source $VESTA/func/main.sh
2022
source $VESTA/conf/vesta.conf
2123

2224
# Hiding password
23-
A3='******'
25+
A4='******'
2426
EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
2527

26-
# sftp command function
28+
# Defining ftp command function
29+
ftpc() {
30+
ftp -p -n $host $port <<EOF
31+
quote USER $user
32+
quote PASS $password
33+
binary
34+
$1
35+
$2
36+
$3
37+
quit
38+
EOF
39+
}
40+
41+
# Defining sftp command function
2742
sftpc() {
2843
expect -f "-" <<EOF "$@"
2944
set count 0
3045
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o \
31-
Port=$sftp_port $sftp_user@$sftp_host
46+
Port=$port $user@$host
3247
expect {
3348
"password:" {
3449
send "$password\r"
@@ -81,42 +96,78 @@ EOF
8196
# Verifications #
8297
#----------------------------------------------------------#
8398

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
90-
fi
99+
check_args '4' "$#" "TYPE HOST USERNAME PASSWORD [PATH] [PORT]"
100+
validate_format 'host' 'user'
91101
is_password_valid
102+
if [ "$type" = 'sftp' ]; then
103+
which expect >/dev/null 2>&1
104+
if [ $? -ne 0 ];then
105+
echo "Error: expect utility not found"
106+
log_event "$E_NOTEXIST" "$EVENT"
107+
exit $E_NOTEXIST
108+
fi
109+
fi
110+
92111

93112
#----------------------------------------------------------#
94113
# Action #
95114
#----------------------------------------------------------#
96115

97-
# Checking network connection and write permissions
98-
sftmpdir="$sftp_path/vst.bK76A9SUkt"
99-
sftpc "mkdir $sftp_path" > /dev/null 2>&1
100-
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
101-
rc=$?
102-
if [[ "$rc" != 0 ]]; then
103-
case $rc in
104-
$E_CONNECT) echo "Error: can't login to sftp host";;
105-
$E_FTP) echo "Error: can't create temp folder on the sftp host";;
106-
esac
107-
log_event "$rc" "$EVENT"
108-
exit "$rc"
116+
# Checking network connection
117+
if [ "$type" = 'ftp' ]; then
118+
if [ -z $port ]; then
119+
port=21
120+
fi
121+
fconn=$(ftpc 2>&1)
122+
ferror=$(echo $fconn |\
123+
grep -i -e failed -e error -e "can't" -e "not conn" -e "incorrect")
124+
if [ ! -z "$ferror" ]; then
125+
echo "Error: can't login to ftp server"
126+
log_event "$E_CONNECT" "$EVENT"
127+
exit $E_CONNECT
128+
fi
129+
130+
# Checking write permissions
131+
ftpc "mkdir $path" > /dev/null 2>&1
132+
ftmpdir="$path/vst.bK76A9SUkt"
133+
ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir"|grep -v Trying)
134+
if [ ! -z "$ftp_result" ] ; then
135+
echo "$ftp_result"
136+
rm -rf $tmpdir
137+
echo "Error: can't create $ftmpdir folder on the ftp"
138+
log_event "$E_FTP" "$EVENT"
139+
exit $E_FTP
140+
fi
141+
fi
142+
if [ "$type" = 'sftp' ]; then
143+
if [ -z $port ]; then
144+
port=22
145+
fi
146+
sftmpdir="$sftp_path/vst.bK76A9SUkt"
147+
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
148+
rc=$?
149+
if [[ "$rc" != 0 ]]; then
150+
case $rc in
151+
$E_CONNECT) echo "Error: can't login to sftp host";;
152+
$E_FTP) echo "Error: can't create temp folder on the sftp host";;
153+
esac
154+
log_event "$rc" "$EVENT"
155+
exit "$rc"
156+
fi
109157
fi
110158

111-
# Adding sftp backup config file
112-
echo "HOST='$sftp_host'
113-
USERNAME='$sftp_user'
114-
PASSWORD='$password'
115-
BPATH='$sftp_path'
116-
PORT='$sftp_port'
117-
TIME='$TIME'
118-
DATE='$DATE'" > $VESTA/conf/sftp.backup.conf
119-
chmod 660 $VESTA/conf/sftp.backup.conf
159+
160+
# Adding backup host
161+
if [ $type != 'local' ]; then
162+
echo "HOST='$host'
163+
USERNAME='$user'
164+
PASSWORD='$password'
165+
BPATH='$path'
166+
PORT='$port'
167+
TIME='$TIME'
168+
DATE='$DATE'" > $VESTA/conf/$type.backup.conf
169+
chmod 660 $VESTA/conf/$type.backup.conf
170+
fi
120171

121172

122173
#----------------------------------------------------------#
@@ -125,9 +176,9 @@ chmod 660 $VESTA/conf/sftp.backup.conf
125176

126177
# Update vesta.conf
127178
if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
128-
echo "BACKUP_SYSTEM='sftp'" >> $VESTA/conf/vesta.conf
179+
echo "BACKUP_SYSTEM='$type'" >> $VESTA/conf/vesta.conf
129180
else
130-
bckp=$(echo "$BACKUP_SYSTEM,sftp" |\
181+
bckp=$(echo "$BACKUP_SYSTEM,$type" |\
131182
sed "s/,/\n/g"|\
132183
sort -r -u |\
133184
sed "/^$/d"|\

bin/v-delete-backup-sftp-host

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)