Skip to content

Commit ee9aec6

Browse files
committed
hestiacp#848 Download file from server and send it user
Download remote backup from the storage server and saves file to local server. And allow user to download file normally When download has been completed it will delete the file. In case big files / slow connections the file will life will be extended.
1 parent 705e325 commit ee9aec6

File tree

3 files changed

+214
-2
lines changed

3 files changed

+214
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ All notable changes to this project will be documented in this file.
4040
- Improved "Forgot password" function prevent brute forcing.
4141
- Update Backup counter propperly when v-delete-user-backup ran.
4242
- Dropped support for Debian 8 according to EOL.
43+
- Improved Backup function.
4344

4445
## [1.1.1] - 2020-03-24 - Hotfix
4546
### Features

bin/v-download-backup

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#!/bin/bash
2+
# info: Download backup
3+
# options: BACKUP
4+
#
5+
# The function download back-up from remote server
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Import Hestia variable for cron launch
13+
source /etc/profile
14+
15+
# Argument definition
16+
backup=$1
17+
18+
# Define backup dir
19+
if [ -z "$BACKUP" ]; then
20+
BACKUP=/backup
21+
fi
22+
23+
# Includes
24+
source $HESTIA/func/main.sh
25+
source $HESTIA/func/domain.sh
26+
source $HESTIA/func/ip.sh
27+
source $HESTIA/func/db.sh
28+
source $HESTIA/func/rebuild.sh
29+
source $HESTIA/conf/hestia.conf
30+
31+
# Defining FTP command function
32+
ftpc() {
33+
/usr/bin/ftp -n $HOST $PORT <<EOF
34+
quote USER $USERNAME
35+
quote PASS $PASSWORD
36+
lcd $BACKUP
37+
binary
38+
$1
39+
$2
40+
$3
41+
quit
42+
EOF
43+
}
44+
45+
# FTP backup download function
46+
ftp_download() {
47+
source $HESTIA/conf/ftp.backup.conf
48+
if [ -z "$PORT" ]; then
49+
PORT='21'
50+
fi
51+
if [ -z $BPATH ]; then
52+
ftpc "get $1"
53+
else
54+
ftpc "cd $BPATH" "get $1"
55+
fi
56+
}
57+
58+
# SFTP command function
59+
sftpc() {
60+
expect -f "-" <<EOF "$@"
61+
set timeout 60
62+
set count 0
63+
spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
64+
-o Port=$PORT $USERNAME@$HOST
65+
expect {
66+
"password:" {
67+
send "$PASSWORD\r"
68+
exp_continue
69+
}
70+
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
71+
set count \$argc
72+
set output "Disconnected."
73+
set rc $E_FTP
74+
exp_continue
75+
}
76+
-re ".*denied.*(publickey|password)." {
77+
set output "Permission denied, wrong publickey or password."
78+
set rc $E_CONNECT
79+
}
80+
-re "\[0-9]*%" {
81+
exp_continue
82+
}
83+
"sftp>" {
84+
if {\$count < \$argc} {
85+
set arg [lindex \$argv \$count]
86+
send "\$arg\r"
87+
incr count
88+
} else {
89+
send "exit\r"
90+
set output "Disconnected."
91+
if {[info exists rc] != 1} {
92+
set rc $OK
93+
}
94+
}
95+
exp_continue
96+
}
97+
timeout {
98+
set output "Connection timeout."
99+
set rc $E_CONNECT
100+
}
101+
}
102+
if {[info exists output] == 1} {
103+
puts "\$output"
104+
}
105+
exit \$rc
106+
EOF
107+
}
108+
109+
# SFTP backup download function
110+
sftp_download() {
111+
source $HESTIA/conf/sftp.backup.conf
112+
if [ -z "$PORT" ]; then
113+
PORT='22'
114+
fi
115+
cd $BACKUP
116+
if [ -z $BPATH ]; then
117+
sftpc "get $1" > /dev/null 2>&1
118+
else
119+
sftpc "cd $BPATH" "get $1" > /dev/null 2>&1
120+
fi
121+
122+
}
123+
124+
# Google backup download function
125+
google_download() {
126+
source $HESTIA/conf/google.backup.conf
127+
gsutil="$HESTIA/3rdparty/gsutil/gsutil"
128+
export BOTO_CONFIG="$HESTIA/conf/.google.backup.boto"
129+
${gsutil} cp gs://$BUCKET/$BPATH/$1 $BACKUP/ > /dev/null 2>&1
130+
if [ "$?" -ne 0 ]; then
131+
check_result "$E_CONNECT" "gsutil failed to download $1"
132+
fi
133+
}
134+
135+
136+
#----------------------------------------------------------#
137+
# Verifications #
138+
#----------------------------------------------------------#
139+
140+
args_usage='BACKUP'
141+
check_args '1' "$#" "$args_usage"
142+
is_format_valid 'backup'
143+
144+
#----------------------------------------------------------#
145+
# Action #
146+
#----------------------------------------------------------#
147+
148+
# Checking local backup
149+
if [ ! -e "$BACKUP/$backup" ]; then
150+
if [[ "$BACKUP_SYSTEM" =~ "google" ]]; then
151+
google_download $backup
152+
downloaded='yes'
153+
fi
154+
if [[ "$BACKUP_SYSTEM" =~ "sftp" ]] && [ -z "$downloaded" ]; then
155+
sftp_download $backup
156+
downloaded='yes'
157+
fi
158+
if [[ "$BACKUP_SYSTEM" =~ "ftp" ]] && [ -z "$downloaded" ]; then
159+
ftp_download $backup
160+
downloaded='yes'
161+
fi
162+
if [ -z "$downloaded" ]; then
163+
check_result $E_NOTEXIST "backup file $backup doesn't exist in '${BACKUP}' folder"
164+
else
165+
chmod 0640 $BACKUP/$backup
166+
chown admin:admin $BACKUP/$backup
167+
fi
168+
fi
169+
170+
echo "
171+
#!/bin/bash
172+
# Reset at timer every 15 min after 1st hour if the file is still downloading / Check if hestia-ng is still accessing $BACKUP/$backup
173+
174+
if lsof $BACKUP/$backup | grep hestia-ng; then
175+
at -f /$BACKUP/$backup.sh now + 15 minutes
176+
else
177+
rm $BACKUP/$backup;
178+
rm $BACKUP/$backup.sh;
179+
fi" > $BACKUP/$backup.sh;
180+
181+
at -f /$BACKUP/$backup.sh now + 60 minutes
182+
#----------------------------------------------------------#
183+
# Hestia #
184+
#----------------------------------------------------------#
185+
186+
187+
exit

web/download/backup/index.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,33 @@
55
include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
66
$backup = basename($_GET['backup']);
77

8+
$downloaded = false;
89
// Check if the backup exists
910
if (!file_exists('/backup/'.$backup)) {
10-
exit(0);
11+
//try to download
12+
if ((!empty($_SESSION['user'])) && ($_SESSION['user'] != 'admin')) {
13+
if (strpos($backup, $user.'.') === 0) {
14+
exec(HESTIA_CMD."v-download-backup " . escapeshellarg($backup), $output, $return_var);
15+
check_return_code($return_var,$output);
16+
unset($output);
17+
if(!$_SESSION['error_msg']){
18+
$downloaded = true;
19+
}
20+
}else{
21+
exit(0);
22+
}
23+
}else{
24+
exec(HESTIA_CMD."v-download-backup " . escapeshellarg($backup),$output, $return_var);
25+
check_return_code($return_var,$output);
26+
unset($output);
27+
if(!$_SESSION['error_msg']){
28+
$downloaded = true;
29+
}
30+
}
31+
if (!file_exists('/backup/'.$backup)) {
32+
exit(0);
33+
$downloaded = false;
34+
}
1135
}
1236

1337
// Data
@@ -23,4 +47,4 @@
2347
header("Content-Disposition: attachment; filename=\"".$backup."\";" );
2448
header("X-Accel-Redirect: /backup/" . $backup);
2549
}
26-
}
50+
}

0 commit comments

Comments
 (0)