mirror of
https://github.com/serghey-rodin/vesta.git
synced 2026-09-08 11:00:35 +02:00
163 lines
3.9 KiB
Bash
Executable File
163 lines
3.9 KiB
Bash
Executable File
#!/bin/bash
|
|
# info: restore user
|
|
# options: user backup
|
|
#
|
|
# The function for resotring user from backup.
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Variable&Function #
|
|
#----------------------------------------------------------#
|
|
|
|
# Argument defenition
|
|
user=$1
|
|
backup=$2
|
|
|
|
# Importing variables
|
|
source $VESTA/conf/vars.conf
|
|
source $V_CONF/vesta.conf
|
|
source $V_FUNC/shared.func
|
|
source $V_FUNC/domain.func
|
|
source $V_FUNC/db.func
|
|
|
|
# Defining ftp command function
|
|
ftpc() {
|
|
ftp -n $HOST $PORT <<EOF
|
|
quote USER $USERNAME
|
|
quote PASS $PASSWORD
|
|
binary
|
|
cd $BPATH
|
|
$1
|
|
quit
|
|
EOF
|
|
}
|
|
|
|
init_ftp_variables() {
|
|
# Checking config
|
|
source $V_CONF/ftp.backup.conf
|
|
if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ] ||\
|
|
[ -z "$BPATH" ]; then
|
|
echo "Error: Parsing error"
|
|
log_event 'debug' "$E_PARSING $V_EVENT"
|
|
exit $E_PARSING
|
|
fi
|
|
}
|
|
|
|
check_ftp_connection(){
|
|
# Checking ftp permission
|
|
ftmpdir=$(mktemp -u -p $BPATH)
|
|
command="mkdir $ftmpdir
|
|
ls $ftmpdir
|
|
rm $ftmpdir"
|
|
if [ ! -z "$(ftpc "$command")" ] ; then
|
|
echo "Error: FTP error"
|
|
log_event 'debug' "$E_FTP $V_EVENT"
|
|
exit $E_FTP
|
|
fi
|
|
}
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Verifications #
|
|
#----------------------------------------------------------#
|
|
|
|
# Get current time
|
|
start_time=$(date '+%s')
|
|
echo "$(date "+%F %T") System restore for user $user"
|
|
echo
|
|
|
|
# Checking arg number
|
|
check_args '2' "$#" 'user backup'
|
|
|
|
# Checking argument format
|
|
format_validation 'user' 'backup'
|
|
|
|
# Checking backup system is enabled
|
|
is_system_enabled 'backup'
|
|
|
|
# Checking load averages
|
|
la=$(cat /proc/loadavg |cut -f 1 -d ' '|cut -f 1 -d '.')
|
|
i=0
|
|
while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do
|
|
echo "$(date "+%F %T") Load Average $la"
|
|
echo
|
|
sleep 60
|
|
if [ "$i" -ge "15" ]; then
|
|
echo "Error: LA is too high"
|
|
log_event 'debug' "$E_LA $V_EVENT"
|
|
exit $E_LA
|
|
fi
|
|
(( ++i))
|
|
done
|
|
|
|
# Checking local backup existance
|
|
if [ ! -e "$V_BACKUP/$user.$backup.tar" ]; then
|
|
if [ ! -z "$(echo $BACKUP_SYSTEM | grep -w ftp)" ]; then
|
|
init_ftp_variables
|
|
check_ftp_connection
|
|
if [ ! -z "$(ftpc ls |awk '{print $9}' |grep $user.$backup.)" ]; then
|
|
cd $V_BACKUP
|
|
echo "$(date "+%F %T") Downloading ftp backup"
|
|
ftpc "get $user.$backup.tar" >> /dev/null 2>/dev/null
|
|
echo "$(date "+%F %T") Downloaded $user.$backup.tar"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
if [ ! -e "$V_BACKUP/$user.$backup.tar" ]; then
|
|
echo "Error: $V_BACKUP/$user.$backup.tar backup not found"
|
|
log_event 'debug' "$E_NOTEXIST $V_EVENT"
|
|
exit $E_NOTEXIST
|
|
fi
|
|
|
|
# Checking arguments
|
|
if [ -z "$3" ]; then
|
|
# Define full backup variables
|
|
VESTA='yes'
|
|
PAM='yes'
|
|
WEB='yes'
|
|
DNS='yes'
|
|
DB='yes'
|
|
MAIL='yes'
|
|
SSL='yes'
|
|
CRON='yes'
|
|
else
|
|
args=("$@")
|
|
for (( i=2; i<${#@}; i++)); do
|
|
key=$(echo ${args[$i]} | cut -f 1 -d :| tr '[:lower:]' '[:upper:]')
|
|
opt=$(echo ${args[$i]} | cut -f 2 -d :)
|
|
if [ -z "$(echo ${args[$i]} |grep :)" ]; then
|
|
eval $key='yes'
|
|
else
|
|
eval $key='opt'
|
|
eval ${key}_OPT=$opt
|
|
fi
|
|
done
|
|
fi
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Action #
|
|
#----------------------------------------------------------#
|
|
|
|
# Creating temporary directory
|
|
tmpdir=$(mktemp -p $V_BACKUP -d)
|
|
echo "TMPDIR is $tmpdir"
|
|
cd $tmpdir
|
|
echo "$(date "+%F %T") Extracting files from backup"
|
|
tar -xf $V_BACKUP/$user.$backup.tar
|
|
echo "$(date "+%F %T") Backup has been unpacked"
|
|
|
|
# Checking Vesta
|
|
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Vesta #
|
|
#----------------------------------------------------------#
|
|
|
|
# Logging
|
|
log_event 'system' "$V_EVENT"
|
|
|
|
exit
|