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