Skip to content

Commit 8ef6558

Browse files
committed
manually merged
2 parents 44f9680 + 5c59126 commit 8ef6558

File tree

22 files changed

+557
-242
lines changed

22 files changed

+557
-242
lines changed

bin/v-add-backup-sftp-host

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/bin/bash
2+
# info: add backup sftp host
3+
# options: HOST USERNAME [PASSWORD] [PATH] [PORT]
4+
#
5+
# The function adds sftp host for system backups
6+
7+
#----------------------------------------------------------#
8+
# Variable&Function #
9+
#----------------------------------------------------------#
10+
11+
# Argument defenition
12+
sftp_host=$1
13+
sftp_user=$2
14+
sftp_password=${3-******}
15+
sftp_path=${4-backup}
16+
sftp_port=${5-22}
17+
18+
A3='******'
19+
20+
# Includes
21+
source $VESTA/func/main.sh
22+
source $VESTA/conf/vesta.conf
23+
24+
# Replace password with ******
25+
if [[ $A3 != '******' ]]
26+
then
27+
EVENT="${EVENT/$sftp_password/******}"
28+
fi
29+
30+
# sftp command function
31+
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
79+
EOF
80+
}
81+
82+
#----------------------------------------------------------#
83+
# Verifications #
84+
#----------------------------------------------------------#
85+
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
92+
fi
93+
94+
check_args '2' "$#" "HOST USERNAME [PASSWORD] [PATH] [PORT]"
95+
96+
#----------------------------------------------------------#
97+
# Action #
98+
#----------------------------------------------------------#
99+
100+
# Checking network connection and write permissions
101+
sftmpdir="$sftp_path/vst.bK76A9SUkt"
102+
sftpc "mkdir $sftp_path" > /dev/null 2>&1
103+
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir"
104+
rc=$?
105+
if [[ "$rc" != 0 ]]
106+
then
107+
case $rc in
108+
$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"
113+
fi
114+
115+
# Adding sftp backup config file
116+
echo "HOST='$sftp_host'
117+
USERNAME='$sftp_user'
118+
PASSWORD='$sftp_password'
119+
BPATH='$sftp_path'
120+
PORT='$sftp_port'
121+
TIME='$TIME'
122+
DATE='$DATE'" > $VESTA/conf/sftp.backup.conf
123+
chmod 660 $VESTA/conf/sftp.backup.conf
124+
125+
#----------------------------------------------------------#
126+
# Vesta #
127+
#----------------------------------------------------------#
128+
129+
# Update vesta.conf
130+
if [ -z "$(grep LANGUAGE $VESTA/conf/vesta.conf)" ]; then
131+
echo "BACKUP_SYSTEM='ftp'" >> $VESTA/conf/vesta.conf
132+
else
133+
bckp=$(echo "$BACKUP_SYSTEM,sftp" |\
134+
sed "s/,/\n/g"|\
135+
sort -r -u |\
136+
sed "/^$/d"|\
137+
sed ':a;N;$!ba;s/\n/,/g')
138+
sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
139+
fi
140+
141+
# Logging
142+
echo "$sftp_host/$sftp_path successfully added as backup destination"
143+
log_event "$OK" "$EVENT"
144+
145+
exit

bin/v-backup-user

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,158 @@ ftp_backup() {
687687
fi
688688
}
689689

690+
# sftp command function
691+
sftpc() {
692+
expect -f "-" <<EOF "$@"
693+
set timeout 60
694+
set count 0
695+
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$PORT $USERNAME@$HOST
696+
expect {
697+
"password:" {
698+
send "$PASSWORD\r"
699+
exp_continue
700+
}
701+
702+
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
703+
set count \$argc
704+
set output "Disconnected."
705+
set rc $E_FTP
706+
exp_continue
707+
}
708+
709+
-re ".*denied.*(publickey|password)." {
710+
set output "Permission denied, wrong publickey or password."
711+
set rc $E_CONNECT
712+
}
713+
714+
-re "\[0-9]*%" {
715+
exp_continue
716+
}
717+
718+
"sftp>" {
719+
if {\$count < \$argc} {
720+
set arg [lindex \$argv \$count]
721+
send "\$arg\r"
722+
incr count
723+
} else {
724+
send "exit\r"
725+
set output "Disconnected."
726+
if {[info exists rc] != 1} {
727+
set rc $OK
728+
}
729+
}
730+
exp_continue
731+
}
732+
733+
timeout {
734+
set output "Connection timeout."
735+
set rc $E_CONNECT
736+
}
737+
}
738+
739+
if {[info exists output] == 1} {
740+
puts "\$output"
741+
}
742+
743+
exit \$rc
744+
EOF
745+
}
746+
747+
sftp_backup() {
748+
#Checking expect installation
749+
check_expect=$(which expect)
750+
if [[ ! -n $check_expect ]]
751+
then
752+
echo "Error: \"expect\" utility not found"
753+
log_event "$E_NOTEXIST" "$EVENT"
754+
exit $E_NOTEXIST
755+
fi
756+
757+
# Checking config
758+
if [ ! -e "$VESTA/conf/sftp.backup.conf" ]; then
759+
sftp_conf_error="Can't open $VESTA/conf/sftp.backup.conf"
760+
echo "$sftp_conf_error" | $send_mail -s "$subj" $email
761+
echo "Error: $VESTA/conf/sftp.backup.conf doesn't exist"
762+
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
763+
log_event "$E_NOTEXIST" "$EVENT"
764+
exit $E_NOTEXIST
765+
fi
766+
767+
# Parse config
768+
source $VESTA/conf/sftp.backup.conf
769+
770+
# Set current data
771+
DATE=$(date +%F)
772+
TIME=$(date +%T)
773+
774+
# Set default port
775+
if [ -z "$(grep 'PORT=' $VESTA/conf/sftp.backup.conf)" ]; then
776+
PORT='22'
777+
fi
778+
779+
# Checking variables
780+
if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
781+
rm -rf $tmpdir
782+
echo "Can't parse sftp backup configuration" |\
783+
$send_mail -s "$subj" $email
784+
echo "Error: Parsing error"
785+
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
786+
log_event "$E_PARSING" "$EVENT"
787+
exit $E_PARSING
788+
fi
789+
790+
# Debug info
791+
echo -e "$(date "+%F %T") Remote: $HOST/$BPATH/$user.$DATE.tar"
792+
793+
# Checking network connection and write permissions
794+
echo -e "$(date "+%F %T") Checking network connection and write permissions ..."
795+
sftmpdir="$BPATH/vst.bK76A9SUkt"
796+
sftpc "mkdir $BPATH" > /dev/null 2>&1
797+
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
798+
rc=$?
799+
if [[ "$rc" != 0 ]]
800+
then
801+
rm -rf $tmpdir
802+
case $rc in
803+
$E_CONNECT) echo "Error: can't login to sftp host" | $send_mail -s "$subj" $email;;
804+
$E_FTP) echo "Error: can't create temp folder on the sftp host" | $send_mail -s "$subj" $email;;
805+
esac
806+
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
807+
log_event "$rc" "$EVENT"
808+
exit "$rc"
809+
fi
810+
echo -e "$(date "+%F %T") Connection established"
811+
812+
# Checking retention
813+
echo -e "$(date "+%F %T") Checking retention ..."
814+
backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}' |grep "^$user\.")
815+
backups_count=$(echo "$backup_list" | wc -l)
816+
if [ "$backups_count" -ge "$BACKUPS" ]; then
817+
backups_rm_number=$((backups_count - BACKUPS + 1))
818+
for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
819+
backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar.*$//")
820+
if [ -z $deprecated ]; then deprecated="$backup_date"; else deprecated="$deprecated $backup_date"; fi
821+
echo -e "$(date "+%F %T") Roated sftp backup: $backup_date"
822+
msg="$msg\n$(date "+%F %T") Roated sftp backup: $backup_date"
823+
sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
824+
done
825+
fi
826+
827+
# Uploading backup archive
828+
echo -e "$(date "+%F %T") Uploading $user.$DATE.tar ..."
829+
if [ "$localbackup" = 'yes' ]; then
830+
cd $BACKUP
831+
sftpc "cd $BPATH" "put $user.$DATE.tar" > /dev/null 2>&1
832+
else
833+
cd $tmpdir
834+
tar -cf $BACKUP/$user.$DATE.tar .
835+
cd $BACKUP/
836+
sftpc "cd $BPATH" "put $user.$DATE.tar" > /dev/null 2>&1
837+
rm -f $user.$DATE.tar
838+
fi
839+
echo -e "$(date "+%F %T") Upload complete"
840+
}
841+
690842
echo "-- SUMMARY --"
691843
msg="$msg\n-- SUMMARY --"
692844

@@ -695,6 +847,7 @@ for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\\n}"); do
695847
case $backup_type in
696848
local) local_backup ;;
697849
ftp) ftp_backup ;;
850+
sftp) sftp_backup ;;
698851
esac
699852
done
700853

bin/v-delete-backup-sftp-host

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
# info: delete backup sftp server
3+
# options: NONE
4+
#
5+
# The function deletes sftp backup host
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Includes
13+
source $VESTA/func/main.sh
14+
source $VESTA/conf/vesta.conf
15+
16+
17+
#----------------------------------------------------------#
18+
# Verifications #
19+
#----------------------------------------------------------#
20+
21+
22+
#----------------------------------------------------------#
23+
# Action #
24+
#----------------------------------------------------------#
25+
26+
# Delete configuration file
27+
rm -f $VESTA/conf/sftp.backup.conf
28+
29+
30+
#----------------------------------------------------------#
31+
# Vesta #
32+
#----------------------------------------------------------#
33+
34+
# Update vesta.conf
35+
bckp=$(echo "$BACKUP_SYSTEM" |\
36+
sed "s/,/\n/g"|\
37+
sed "s/sftp//" |\
38+
sed "/^$/d"|\
39+
sed ':a;N;$!ba;s/\n/,/g')
40+
sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
41+
42+
# Logging
43+
echo "sftp host successfulle removed from configuration"
44+
log_event "$OK" "$EVENT"
45+
46+
exit

0 commit comments

Comments
 (0)