| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #!/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
|