Просмотр исходного кода

#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...
Jaap Marcus 5 лет назад
Родитель
Сommit
70ed813468
3 измененных файлов с 100 добавлено и 55 удалено
  1. 31 15
      bin/v-download-backup
  2. 48 0
      bin/v-schedule-user-backup-download
  3. 21 40
      web/download/backup/index.php

+ 31 - 15
bin/v-download-backup

@@ -1,6 +1,6 @@
 #!/bin/bash
 # info: Download backup
-# options: BACKUP 
+# options: USER BACKUP 
 #
 # The function download back-up from remote server
 
@@ -13,7 +13,8 @@
 source /etc/profile
 
 # Argument definition
-backup=$1
+user=$1
+backup=$2
 
 # Define backup dir
 if [ -z "$BACKUP" ]; then
@@ -137,14 +138,22 @@ google_download() {
 #                    Verifications                         #
 #----------------------------------------------------------#
 
-args_usage='BACKUP'
-check_args '1' "$#" "$args_usage"
-is_format_valid 'backup'
+check_args '2' "$#" 'USER BACKUP'
+is_format_valid 'user' 'backup'
 
 #----------------------------------------------------------#
 #                       Action                             #
 #----------------------------------------------------------#
 
+
+# Checking available disk space
+disk_usage=$(df $BACKUP |tail -n1 |tr ' ' '\n' |grep % |cut -f 1 -d %)
+if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
+    echo "Error: Not enough disk space" |$SENDMAIL -s "$subj" $email $notify
+    sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
+    check_result $E_DISK "Not enough disk space"
+fi
+
 # Checking local backup
 if [ ! -e "$BACKUP/$backup" ]; then
     if [[ "$BACKUP_SYSTEM" =~ "google" ]]; then
@@ -165,22 +174,29 @@ if [ ! -e "$BACKUP/$backup" ]; then
         if [ -e "$BACKUP/$backup" ]; then
         chmod 0640 $BACKUP/$backup
         chown admin:admin $BACKUP/$backup
-        echo "#!/bin/bash 
-# Reset at timer every 15 min after 1st hour if the file is still downloading / Check if hestia-ng is still accessing $BACKUP/$backup
-
-if lsof $BACKUP/$backup | grep hestia-ng; then
-    at -f /$BACKUP/$backup.sh now + 15 minutes
-else 
-rm $BACKUP/$backup;
-rm $BACKUP/$backup.sh;
-fi" >  $BACKUP/$backup.sh;
-        at -f /$BACKUP/$backup.sh now + 60 minutes
+        echo "rm $BACKUP/$backup" | at now + 1 day
         fi
     fi
 fi
+
 #----------------------------------------------------------#
 #                       Hestia                             #
 #----------------------------------------------------------#
 
+# Send notification
+if [ -e "$BACKUP/$backup" ]; then
+    cd $BACKUP
+    subj="$user → Download of $backup has been completed"
+    email=$(get_user_value '$CONTACT')
+    echo "Download of $backup has been completed you are able to download it for 12 hours" |$SENDMAIL -s "$subj" $email $notify
+    $BIN/v-add-user-notification $user "$subj" "Download of $backup has been completed you are able to download it for 12 hours"
+    
+fi
+
+# Cleaning restore queue
+sed -i "/v-download-backup $user /d" $HESTIA/data/queue/backup.pipe
+
+# Logging
+log_event "$OK" "$ARGUMENTS"
 
 exit

+ 48 - 0
bin/v-schedule-user-backup-download

@@ -0,0 +1,48 @@
+#!/bin/bash
+# info: Schedule a backup
+# options: USER BACKUP 
+#
+# The function for scheduling user backup creation.
+
+
+#----------------------------------------------------------#
+#                    Variable&Function                     #
+#----------------------------------------------------------#
+
+# Importing system variables
+source /etc/profile
+
+# Argument definition
+user=$1
+backup=$2
+
+# Includes
+source $HESTIA/func/main.sh
+source $HESTIA/func/domain.sh
+source $HESTIA/func/db.sh
+source $HESTIA/conf/hestia.conf
+
+#----------------------------------------------------------#
+#                    Verifications                         #
+#----------------------------------------------------------#
+
+check_args '2' "$#" 'USER BACKUP'
+is_format_valid 'user' 'backup'
+
+#----------------------------------------------------------#
+#                       Action                             #
+#----------------------------------------------------------#
+
+# Adding backup task to  the queue
+echo "$BIN/v-download-backup $user $backup 2>&1" >>\
+    $HESTIA/data/queue/backup.pipe
+
+
+#----------------------------------------------------------#
+#                       Hestia                             #
+#----------------------------------------------------------#
+
+# Logging
+log_event "$OK" "$ARGUMENTS"
+
+exit

+ 21 - 40
web/download/backup/index.php

@@ -1,50 +1,31 @@
 <?php
 // Init
 error_reporting(NULL);
+ob_start();
 session_start();
 include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
-$backup = basename($_GET['backup']);
 
-$downloaded = false;
-// Check if the backup exists
-if (!file_exists('/backup/'.$backup)) {
-    //try to download
-    if ((!empty($_SESSION['user'])) && ($_SESSION['user'] != 'admin')) {
-        if (strpos($backup, $user.'.') === 0) {
-            exec(HESTIA_CMD."v-download-backup " . escapeshellarg($backup), $output, $return_var);
-            check_return_code($return_var,$output);
-            unset($output);
-            if(!$_SESSION['error_msg']){
-                $downloaded = true;
-            }
-        }else{
-            exit(0);
-        }
-    }else{
-        exec(HESTIA_CMD."v-download-backup " . escapeshellarg($backup),$output, $return_var);
-        check_return_code($return_var,$output);
-        unset($output);
-        if(!$_SESSION['error_msg']){
-            $downloaded = true;
-        }
-    }
-    if (!file_exists('/backup/'.$backup)) {
-        exit(0);
-        $downloaded = false;
-    }
+// Check token
+if ((!isset($_GET['token'])) || ($_SESSION['token'] != $_GET['token'])) {
+    header('Location: /login/');
+    exit();
 }
 
-// Data
-if ($_SESSION['user'] == 'admin') {
-    header('Content-type: application/gzip');
-    header("Content-Disposition: attachment; filename=\"".$backup."\";" ); 
-    header("X-Accel-Redirect: /backup/" . $backup);
-}
+$v_username = escapeshellarg($user);
+exec (HESTIA_CMD."v-schedule-user-backup ".$v_username, $output, $return_var);
+if ($return_var == 0) {
+    $_SESSION['error_msg'] = __('BACKUP_SCHEDULED');
+} else {
+    $_SESSION['error_msg'] = implode('<br>', $output);
+    if (empty($_SESSION['error_msg'])) {
+        $_SESSION['error_msg'] = __('Error: hestia did not return any output.');
+    }
 
-if ((!empty($_SESSION['user'])) && ($_SESSION['user'] != 'admin')) {
-    if (strpos($backup, $user.'.') === 0) {
-        header('Content-type: application/gzip');
-        header("Content-Disposition: attachment; filename=\"".$backup."\";" ); 
-        header("X-Accel-Redirect: /backup/" . $backup);
+    if ($return_var == 4) {
+        $_SESSION['error_msg'] = __('BACKUP_EXISTS');
     }
-}
+
+}
+unset($output);
+header("Location: /list/backup/");
+exit;