Skip to content

Commit a168483

Browse files
committed
Merge pull request hestiacp#335 from ss-abramchuk/sftp
Backup via SFTP
2 parents f78a078 + 70490f5 commit a168483

File tree

3 files changed

+344
-0
lines changed

3 files changed

+344
-0
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
@@ -692,6 +692,158 @@ ftp_backup() {
692692
fi
693693
}
694694

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

@@ -700,6 +852,7 @@ for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\\n}"); do
700852
case $backup_type in
701853
local) local_backup ;;
702854
ftp) ftp_backup ;;
855+
sftp) sftp_backup ;;
703856
esac
704857
done
705858

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)