|
| 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 |
0 commit comments