Skip to content

Commit 70ed813

Browse files
committed
hestiacp#848 Addons
As discussed... Instead downloading direct from server and forward it to the user A task is created and execute with the /usr/local/hestia/bin/v-update-sys-queue backup command File downloaded and a notification send to the user so he can download it. After 24 hours it is removed...
1 parent dc87c93 commit 70ed813

File tree

3 files changed

+100
-55
lines changed

3 files changed

+100
-55
lines changed

bin/v-download-backup

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# info: Download backup
3-
# options: BACKUP
3+
# options: USER BACKUP
44
#
55
# The function download back-up from remote server
66

@@ -13,7 +13,8 @@
1313
source /etc/profile
1414

1515
# Argument definition
16-
backup=$1
16+
user=$1
17+
backup=$2
1718

1819
# Define backup dir
1920
if [ -z "$BACKUP" ]; then
@@ -137,14 +138,22 @@ google_download() {
137138
# Verifications #
138139
#----------------------------------------------------------#
139140

140-
args_usage='BACKUP'
141-
check_args '1' "$#" "$args_usage"
142-
is_format_valid 'backup'
141+
check_args '2' "$#" 'USER BACKUP'
142+
is_format_valid 'user' 'backup'
143143

144144
#----------------------------------------------------------#
145145
# Action #
146146
#----------------------------------------------------------#
147147

148+
149+
# Checking available disk space
150+
disk_usage=$(df $BACKUP |tail -n1 |tr ' ' '\n' |grep % |cut -f 1 -d %)
151+
if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
152+
echo "Error: Not enough disk space" |$SENDMAIL -s "$subj" $email $notify
153+
sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
154+
check_result $E_DISK "Not enough disk space"
155+
fi
156+
148157
# Checking local backup
149158
if [ ! -e "$BACKUP/$backup" ]; then
150159
if [[ "$BACKUP_SYSTEM" =~ "google" ]]; then
@@ -165,22 +174,29 @@ if [ ! -e "$BACKUP/$backup" ]; then
165174
if [ -e "$BACKUP/$backup" ]; then
166175
chmod 0640 $BACKUP/$backup
167176
chown admin:admin $BACKUP/$backup
168-
echo "#!/bin/bash
169-
# Reset at timer every 15 min after 1st hour if the file is still downloading / Check if hestia-ng is still accessing $BACKUP/$backup
170-
171-
if lsof $BACKUP/$backup | grep hestia-ng; then
172-
at -f /$BACKUP/$backup.sh now + 15 minutes
173-
else
174-
rm $BACKUP/$backup;
175-
rm $BACKUP/$backup.sh;
176-
fi" > $BACKUP/$backup.sh;
177-
at -f /$BACKUP/$backup.sh now + 60 minutes
177+
echo "rm $BACKUP/$backup" | at now + 1 day
178178
fi
179179
fi
180180
fi
181+
181182
#----------------------------------------------------------#
182183
# Hestia #
183184
#----------------------------------------------------------#
184185

186+
# Send notification
187+
if [ -e "$BACKUP/$backup" ]; then
188+
cd $BACKUP
189+
subj="$user → Download of $backup has been completed"
190+
email=$(get_user_value '$CONTACT')
191+
echo "Download of $backup has been completed you are able to download it for 12 hours" |$SENDMAIL -s "$subj" $email $notify
192+
$BIN/v-add-user-notification $user "$subj" "Download of $backup has been completed you are able to download it for 12 hours"
193+
194+
fi
195+
196+
# Cleaning restore queue
197+
sed -i "/v-download-backup $user /d" $HESTIA/data/queue/backup.pipe
198+
199+
# Logging
200+
log_event "$OK" "$ARGUMENTS"
185201

186202
exit
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
# info: Schedule a backup
3+
# options: USER BACKUP
4+
#
5+
# The function for scheduling user backup creation.
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Importing system variables
13+
source /etc/profile
14+
15+
# Argument definition
16+
user=$1
17+
backup=$2
18+
19+
# Includes
20+
source $HESTIA/func/main.sh
21+
source $HESTIA/func/domain.sh
22+
source $HESTIA/func/db.sh
23+
source $HESTIA/conf/hestia.conf
24+
25+
#----------------------------------------------------------#
26+
# Verifications #
27+
#----------------------------------------------------------#
28+
29+
check_args '2' "$#" 'USER BACKUP'
30+
is_format_valid 'user' 'backup'
31+
32+
#----------------------------------------------------------#
33+
# Action #
34+
#----------------------------------------------------------#
35+
36+
# Adding backup task to the queue
37+
echo "$BIN/v-download-backup $user $backup 2>&1" >>\
38+
$HESTIA/data/queue/backup.pipe
39+
40+
41+
#----------------------------------------------------------#
42+
# Hestia #
43+
#----------------------------------------------------------#
44+
45+
# Logging
46+
log_event "$OK" "$ARGUMENTS"
47+
48+
exit

web/download/backup/index.php

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,31 @@
11
<?php
22
// Init
33
error_reporting(NULL);
4+
ob_start();
45
session_start();
56
include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
6-
$backup = basename($_GET['backup']);
77

8-
$downloaded = false;
9-
// Check if the backup exists
10-
if (!file_exists('/backup/'.$backup)) {
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-
}
8+
// Check token
9+
if ((!isset($_GET['token'])) || ($_SESSION['token'] != $_GET['token'])) {
10+
header('Location: /login/');
11+
exit();
3512
}
3613

37-
// Data
38-
if ($_SESSION['user'] == 'admin') {
39-
header('Content-type: application/gzip');
40-
header("Content-Disposition: attachment; filename=\"".$backup."\";" );
41-
header("X-Accel-Redirect: /backup/" . $backup);
42-
}
14+
$v_username = escapeshellarg($user);
15+
exec (HESTIA_CMD."v-schedule-user-backup ".$v_username, $output, $return_var);
16+
if ($return_var == 0) {
17+
$_SESSION['error_msg'] = __('BACKUP_SCHEDULED');
18+
} else {
19+
$_SESSION['error_msg'] = implode('<br>', $output);
20+
if (empty($_SESSION['error_msg'])) {
21+
$_SESSION['error_msg'] = __('Error: hestia did not return any output.');
22+
}
4323

44-
if ((!empty($_SESSION['user'])) && ($_SESSION['user'] != 'admin')) {
45-
if (strpos($backup, $user.'.') === 0) {
46-
header('Content-type: application/gzip');
47-
header("Content-Disposition: attachment; filename=\"".$backup."\";" );
48-
header("X-Accel-Redirect: /backup/" . $backup);
24+
if ($return_var == 4) {
25+
$_SESSION['error_msg'] = __('BACKUP_EXISTS');
4926
}
50-
}
27+
28+
}
29+
unset($output);
30+
header("Location: /list/backup/");
31+
exit;

0 commit comments

Comments
 (0)