#!/bin/bash
# info: backup system user with all its objects
# options: USER NOTIFY
#
# The call is used for backing up user with all its domains and databases.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Importing system variables
source /etc/profile

# Argument defenition
user=$1
notify=${2-no}

# Includes
source $VESTA/func/main.sh
source $VESTA/func/domain.sh
source $VESTA/func/db.sh
source $VESTA/conf/vesta.conf


#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '1' "$#" 'USER [NOTIFY]'
validate_format 'user'
is_system_enabled "$BACKUP_SYSTEM" 'BACKUP_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_backup_enabled


#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Set backup directory if undefined
if [ -z "$BACKUP" ]; then
    BACKUP=/home/backup
fi
mkdir -p $BACKUP

# Get current time
start_time=$(date '+%s')

# Set notification email and subject
if [ "$notify" != 'no' ]; then
    subj="$user → backup failed"
    email=$(get_user_value '$CONTACT')
else
    subj="$user → backup failed"
    email=$(grep CONTACT $VESTA/data/users/admin/user.conf | cut -f 2 -d \')
fi
send_mail="$VESTA/web/inc/mail-wrapper.php"

# Check load average
la=$(cat /proc/loadavg | cut -f 1 -d ' ' | cut -f 1 -d '.')
i=0
while [ "$la" -ge "$BACKUP_LA_LIMIT" ]; do
    echo "$(date "+%F %T") LoadAverage $la is above threshold. Sleeping..."
    sleep 120
    if [ "$i" -ge "5" ]; then
        mail_top=$(top -b| head -n 30)
        mail_text="LoadAverage $i is above threshold\n\n$mail_top\n"
        echo -e "$mail_text" | $send_mail -s "$subj" $email
        echo "Error: LA is too high"
        sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
        log_event "$E_LA" "$EVENT"
        exit $E_LA
    fi
    la=$(cat /proc/loadavg | cut -f 1 -d ' ' | cut -f 1 -d '.')
    (( ++i))
done

# Creating temporary directory
tmpdir=$(mktemp -p $BACKUP -d)
if [ "$?" -ne 0 ]; then
    echo "Can't create tmp dir $tmpdir" | $send_mail -s "$subj" $email
    echo "Error: can't create tmp dir"
    log_event "$E_NOTEXIST" "$EVENT"
    exit $E_NOTEXIST
fi

# Backup sys configs
echo -e "-- SYSTEM --"
msg="-- SYSTEM --"
mkdir $tmpdir/vesta

echo -e "$(date "+%F %T") $user.conf"
msg="$msg\n$(date "+%F %T") $user.conf"
cp -r $USER_DATA/user.conf $tmpdir/vesta/

if [ -e "$USER_DATA/stats.log" ]; then
    echo -e "$(date "+%F %T") stats.log"
    msg="$msg\n$(date "+%F %T") stats.log"
    cp -r $USER_DATA/stats.log $tmpdir/vesta/
fi

if [ -e "$USER_DATA/history.log" ]; then
    echo -e "$(date "+%F %T") history.log"
    msg="$msg\n$(date "+%F %T") history.log"
    cp -r $USER_DATA/history.log $tmpdir/vesta/
fi

if [ -e "$USER_DATA/backup-excludes.conf" ]; then
    echo -e "$(date "+%F %T") backup-excludes.conf"
    msg="$msg\n$(date "+%F %T") backup-excludes.conf"
    cp -r $USER_DATA/backup-excludes.conf $tmpdir/vesta/
fi

# Backup PAM
mkdir $tmpdir/pam
echo -e "$(date "+%F %T") pam"
msg="$msg\n$(date "+%F %T") pam"
grep "^$user:" /etc/passwd > $tmpdir/pam/passwd
grep "^$user:" /etc/shadow > $tmpdir/pam/shadow
grep "^$user:" /etc/group > $tmpdir/pam/group
echo
msg="$msg\n"

# Parsing excludes
if [ -e "$USER_DATA/backup-excludes.conf" ]; then
    source $USER_DATA/backup-excludes.conf
fi

# WEB domains
if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB" != '*' ]; then
    echo "-- WEB --"
    msg="$msg\n-- WEB --"
    mkdir $tmpdir/web/

    # Parsing domain exclusions
    conf="$USER_DATA/web.conf"
    for domain in $(search_objects 'web' 'SUSPENDED' "*" 'DOMAIN'); do
        check_exl=$(echo -e "${WEB//,/\n}" |grep "^$domain$")
        if [ -z "$check_exl" ]; then
            web_list="$web_list $domain"
        else
            echo "$(date "+%F %T") excluding $domain"
            msg="$msg\n$(date "+%F %T") excluding $domain"
        fi
    done
    web_list=$(echo "$web_list" | sed -e "s/  */\ /g" -e "s/^ //")

    i=0

    for domain in $web_list; do
        ((i ++))
        echo -e "$(date "+%F %T") $domain"
        msg="$msg\n$(date "+%F %T") $domain"
        mkdir -p $tmpdir/web/$domain/conf
        mkdir -p $tmpdir/web/$domain/vesta

        # Get domain variables
        domain_idn=$(idn -t --quiet -a "$domain")
        get_domain_values 'web'

        # Backup web.conf
        cd $tmpdir/web/$domain/
        conf="$USER_DATA/web.conf"
        grep "DOMAIN='$domain'" $conf > vesta/web.conf

        # Backup vhost config
        tpl_file="$WEBTPL/$WEB_SYSTEM/$TPL.tpl"
        conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
        get_web_config_brds
        sed -n "$top_line,$bottom_line p" $conf > conf/$WEB_SYSTEM.conf

        # Backup ssl vhost
        if [ "$SSL" = 'yes' ]; then
            tpl_file="$WEBTPL/$WEB_SYSTEM/$TPL.stpl"
            conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
            get_web_config_brds
            sed -n "$top_line,$bottom_line p" $conf > conf/s$WEB_SYSTEM.conf
        fi

        # Backup proxy config
        if [ ! -z "$PROXY" ]; then
            tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.tpl"
            conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
            get_web_config_brds
            sed -n "$top_line,$bottom_line p" $conf > conf/$PROXY_SYSTEM.conf
        fi

        # Backup ssl proxy config
        if [ ! -z "$PROXY" ] && [ "$SSL" = 'yes' ] ; then
            tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.stpl"
            conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
            get_web_config_brds
            sed -n "$top_line,$bottom_line p" $conf > conf/s$PROXY_SYSTEM.conf
        fi

        # Backup custom config
        for sconfig in $(ls $HOMEDIR/$user/conf/web/|grep ".$domain.conf"); do
            cp $HOMEDIR/$user/conf/web/$sconfig conf/
        done

        # Backup ssl certificates
        if [ "$SSL" = 'yes' ] ; then
            cp $HOMEDIR/$user/conf/web/ssl.$domain.* conf/
            cp $USER_DATA/ssl/$domain.* vesta/
        fi

        # Define exclude arguments
        set -f
        fargs=()
        fargs+=(-not)
        fargs+=(-path)
        fargs+=("./logs*")
        check_exlusion=$(echo -e "${WEB//,/\n}" | grep "^$domain:")
        if [ ! -z "$check_exlusion" ]; then
            xdirs="$(echo -e "${check_exlusion//:/\n}" |grep -v $domain)"
            for xpath in $xdirs; do
                xpath="$(echo $xpath | sed -e 's/\/*$//' -e 's/^\/*//')"
                fargs+=(-not)
                fargs+=(-path)
                fargs+=("./$xpath/*")
                echo "$(date "+%F %T") excluding directory $xpath"
                msg="$msg\n$(date "+%F %T") excluding directory $xpath"
            done
        fi

        # Backup files
        cd $HOMEDIR/$user/web/$domain
        find . ${fargs[@]} -type f -print0 |\
            tar -cpf $tmpdir/web/$domain/domain_data.tar --null -T -

        # Backup empty folders
        find . ${fargs[@]} -type d -empty -print0 |\
            tar -rpf $tmpdir/web/$domain/domain_data.tar --null -T -

        # Compress archive
        gzip -$BACKUP_GZIP $tmpdir/web/$domain/domain_data.tar
        set +f
    done

    # Print total
    if [ "$i" -eq 1 ]; then
        echo -e "$(date "+%F %T") *** $i domain ***"
        msg="$msg\n$(date "+%F %T") *** $i domain ***"
    else
        echo -e "$(date "+%F %T") *** $i domains ***"
        msg="$msg\n$(date "+%F %T") *** $i domains ***"
    fi
    echo
    msg="$msg\n"
fi

# DNS domains
if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS" != '*' ]; then
    echo "-- DNS --"
    msg="$msg\n-- DNS --"
    mkdir $tmpdir/dns/

    # Parsing domain exclusions
    for domain in $(search_objects 'dns' 'SUSPENDED' "*" 'DOMAIN'); do
        check_exl=$(echo -e "${DNS//,/\n}" |grep "^$domain$")
        if [ -z "$check_exl" ]; then
            dns_list="$dns_list $domain"
        else
            echo "$(date "+%F %T") excluding $domain"
            msg="$msg\n$(date "+%F %T") excluding $domain"
        fi
    done
    dns_list=$(echo "$dns_list" | sed -e "s/  */\ /g" -e "s/^ //")

    i=0
    for domain in $dns_list; do
        ((i ++))
        echo -e "$(date "+%F %T") $domain"
        msg="$msg\n$(date "+%F %T") $domain"

        # Building directory tree
        mkdir -p $tmpdir/dns/$domain/conf
        mkdir -p $tmpdir/dns/$domain/vesta

        # Backup dns.conf
        cd $tmpdir/dns/$domain/
        conf="$USER_DATA/dns.conf"
        grep "DOMAIN='$domain'" $conf > vesta/dns.conf

        # Backup dns recods
        cp $USER_DATA/dns/$domain.conf vesta/$domain.conf
        if [ "$DNS_SYSTEM" != 'remote' ]; then
            cp $HOMEDIR/$user/conf/dns/$domain.db conf/$domain.db
        fi
    done

    # Print total
    if [ "$i" -eq 1 ]; then
        echo -e "$(date "+%F %T") *** $i domain ***"
        msg="$msg\n$(date "+%F %T") *** $i domain ***"
    else
        echo -e "$(date "+%F %T") *** $i domains ***"
        msg="$msg\n$(date "+%F %T") *** $i domains ***"
    fi
    echo
    msg="$msg\n"
fi

# Mail domains
if [ ! -z "$MAIL_SYSTEM" ] && [ "$MAIL" != '*' ]; then
    echo "-- MAIL --"
    msg="$msg\n-- MAIL --"
    mkdir $tmpdir/mail/

    # Parsing domain exclusions
    conf="$USER_DATA/mail.conf"
    for domain in $(search_objects 'mail' 'SUSPENDED' "*" 'DOMAIN'); do
        check_exl=$(echo -e "${MAIL//,/\n}" |grep "^$domain$")
        if [ -z "$check_exl" ]; then
            mail_list="$mail_list $domain"
        else
            echo "$(date "+%F %T") excluding $domain"
            msg="$msg\n$(date "+%F %T") excluding $domain"
        fi
    done
    mail_list=$(echo "$mail_list" | sed -e "s/  */\ /g" -e "s/^ //")

    i=0
    for domain in $mail_list; do
        ((i ++))
        echo -e "$(date "+%F %T") $domain"
        msg="$msg\n$(date "+%F %T") $domain"
        mkdir -p $tmpdir/mail/$domain/conf
        mkdir -p $tmpdir/mail/$domain/vesta
        domain_idn=$(idn -t --quiet -a "$domain")

        # Backup exim config
        if [[ "$MAIL_SYSTEM" =~ exim ]]; then
            cd $tmpdir/mail/$domain/
            cp $HOMEDIR/$user/conf/mail/$domain/* conf/
        fi

        # Backup mail.conf
        conf="$USER_DATA/mail.conf"
        grep "DOMAIN='$domain'" $conf > vesta/mail.conf
        cp $USER_DATA/mail/$domain.* vesta/
        if [ ! -z "$(ls $USER_DATA/mail/|grep *@$domain)" ]; then
            cp $USER_DATA/mail/*@$domain.* vesta/
        fi

        # Backup emails
        cd $HOMEDIR/$user/mail/$domain_idn
        for account in $(ls); do
            exclusion=$(echo -e "${MAIL//,/\n}" |grep "$domain:")
            exclusion=$(echo -e "${exclusion//:/\n}" |grep "^$account$")

            # Checking exlusions
            if [ -z "$exclusion" ] && [[ "$MAIL_SYSTEM" =~ exim ]]; then
                echo "$(date "+%F %T") + $account@$domain"
                msg="$msg\n$(date "+%F %T") + $account@$domain"
                touch $tmpdir/mail/$domain/accounts.tar
                tar -rpf $tmpdir/mail/$domain/accounts.tar $account
            else
                echo "$(date "+%F %T") excluding account $account"
                msg="$msg\n$(date "+%F %T") excluding account $account"
            fi
        done

        # Compress archive
        if [ -e "$tmpdir/mail/$domain/accounts.tar" ]; then
            gzip -$BACKUP_GZIP $tmpdir/mail/$domain/accounts.tar
        fi
    done

    # Print total
    if [ "$i" -eq 1 ]; then
        echo -e "$(date "+%F %T") *** $i domain ***"
        msg="$msg\n$(date "+%F %T") *** $i domain ***"
    else
        echo -e "$(date "+%F %T") *** $i domains ***"
        msg="$msg\n$(date "+%F %T") *** $i domains ***"
    fi
    echo
    msg="$msg\n"
fi


# Databases
if [ ! -z "$DB_SYSTEM" ] && [ "$DB" != '*' ]; then
    echo "-- DB --"
    msg="$msg\n-- DB --"
    mkdir $tmpdir/db/

    # Parsing database exclusions
    for database in $(search_objects 'db' 'SUSPENDED' "*" 'DB'); do
        check_exl=$(echo -e "${DB//,/\n}" |grep "^$database$")
        if [ -z "$check_exl" ]; then
            db_list="$db_list $database"
        else
            echo "$(date "+%F %T") excluding $database"
            msg="$msg\n$(date "+%F %T") excluding $database"
        fi
    done

    i=0
    conf="$USER_DATA/db.conf"
    db_list=$(echo "$db_list" | sed -e "s/  */\ /g" -e "s/^ //")
    for database in $db_list; do
        ((i ++))
        get_database_values

        echo -e "$(date "+%F %T") $database ($TYPE)"
        msg="$msg\n$(date "+%F %T") $database ($TYPE)"

        mkdir -p $tmpdir/db/$database/conf
        mkdir -p $tmpdir/db/$database/vesta

        cd $tmpdir/db/$database/
        grep "DB='$database'" $conf > vesta/db.conf

        dump="$tmpdir/db/$database/$database.$TYPE.sql"
        grants="$tmpdir/db/$database/conf/$database.$TYPE.$DBUSER"
        case $TYPE in
            mysql) dump_mysql_database ;;
            pgsql) dump_pgsql_database ;;
        esac

        # Compress dump
        gzip -$BACKUP_GZIP $dump
    done

    # Print total
    if [ "$i" -eq 1 ]; then
        echo -e "$(date "+%F %T") *** $i database ***"
        msg="$msg\n$(date "+%F %T") *** $i database ***"
    else
        echo -e "$(date "+%F %T") *** $i databases ***"
        msg="$msg\n$(date "+%F %T") *** $i databases ***"
    fi
    echo
    msg="$msg\n"
fi

# Cron jobs
if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON" != '*' ]; then
    echo "-- CRON --"
    msg="$msg\n-- CRON --"
    mkdir $tmpdir/cron/

    # Backup cron.conf
    cp $USER_DATA/cron.conf $tmpdir/cron/
    cron_record=$(wc -l $USER_DATA/cron.conf|cut -f 1 -d ' ')

    if [ -e "/var/spool/cron/$user" ]; then
        cron_list="$cron_record"
        cp /var/spool/cron/$user $tmpdir/cron/
    fi

    # Print total
    if [ "$cron_record" -eq 1 ]; then
        echo -e "$(date "+%F %T") *** $cron_record job ***"
        msg="$msg\n$(date "+%F %T") *** $cron_record job ***"
    else
        echo -e "$(date "+%F %T") *** $cron_record jobs ***"
        msg="$msg\n$(date "+%F %T") *** $cron_record jobs ***"
    fi
    echo
    msg="$msg\n"
fi

# User Directories
if [ "$USER" != '*' ]; then
    echo "-- User Dir --"
    msg="$msg\n-- User Dir --"
    mkdir $tmpdir/user_dir
    cd $HOMEDIR/$user

    # Parsing directory exlusions
    USER=''
    if [ -e "$USER_DATA/backup-excludes.conf" ]; then
        source $USER_DATA/backup-excludes.conf
    fi
    exlusion_list=$(echo -e "${USER//,/\n}")
    fargs=()
    for xpath in $exlusion_list; do
        fargs+=(-not)
        fargs+=(-path)
        fargs+=("./$xpath*")
        echo "$(date "+%F %T") excluding directory $xpath"
        msg="$msg\n$(date "+%F %T") excluding directory $xpath"
    done

    IFS=$'\n'
    set -f
    i=0
    for udir in $(ls |egrep -v "conf|web|dns|mail"); do
        check_exl=$(echo -e "${USER//,/\n}" |grep "^$udir$")
        if [ -z "$check_exl" ]; then
            ((i ++))
            udir_list="$udir_list $udir"
            echo -e "$(date "+%F %T") adding directory $udir"
            msg="$msg\n$(date "+%F %T") adding directory $udir"

            # Backup files
            find ./$udir ${fargs[@]} -type f -print0 |\
                tar -cpf $tmpdir/user_dir/$udir.tar --null -T -

            # Backup empty folders
            find ./$udir ${fargs[@]} -type d -empty -print0 |\
                tar -rpf $tmpdir/user_dir/$udir.tar --null -T -

            # Compress arhive
            gzip -$BACKUP_GZIP $tmpdir/user_dir/$udir.tar
        fi
    done
    set +f
    udir_list=$(echo "$udir_list" | sed -e "s/  */\ /g" -e "s/^ //")

    # Print total
    if [ "$i" -eq 1 ]; then
        echo -e "$(date "+%F %T") *** $i user directory ***"
        msg="$msg\n$(date "+%F %T") *** $i directory ***"
    else
        echo -e "$(date "+%F %T") *** $i directories ***"
        msg="$msg\n$(date "+%F %T") *** $i directories ***"
    fi
    echo
    msg="$msg\n"
fi

# Get backup size
size="$(du -shm $tmpdir | cut -f 1)"

# Get current time
end_time=$(date '+%s')
DATE=$(date +%F)
TIME=$(date +%T)


# Defining local storage function
local_backup(){
    # Removing dublicate for this day
    if [ -e "$BACKUP/$user.$DATE.tar" ]; then
        deprecated="$DATE"
        rm -f $BACKUP/$user.$DATE.tar
    fi

    # Checking retention
    backup_list=$(ls -lrt $BACKUP/ | awk '{print $9}' |grep "^$user\.")
    backups_count=$(echo "$backup_list" | wc -l)
    if [ "$BACKUPS" -le "$backups_count" ]; then
        backups_rm_number=$((backups_count - BACKUPS))
        (( ++backups_rm_number))

        for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
            # Removing old backup
            backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//")
            deprecated="$deprecated $backup_date"
            echo -e "$(date "+%F %T") Roated: $backup_date"
            msg="$msg\n$(date "+%F %T") Rotated: $backup_date"
            rm -f $BACKUP/$backup
        done
    fi

    # Checking disk space
    disk_usage=$(df $BACKUP |tail -n1 |tr ' ' '\n' |grep % |cut -f 1 -d %)

    if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
        rm -rf $tmpdir
        echo "Not enough disk space to run backup" |\
            $send_mail -s "$subj" $email
        echo "Error: Not enough disk space"
        sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
        log_event "$E_DISK" "$EVENT"
        exit $E_DISK
    fi

    # Creating final tarball
    cd $tmpdir
    tar -cf $BACKUP/$user.$DATE.tar .
    chmod 640 $BACKUP/$user.$DATE.tar
    chown admin:$user $BACKUP/$user.$DATE.tar
    localbackup='yes'
    echo -e "$(date "+%F %T") Local: $BACKUP/$user.$DATE.tar"
    msg="$msg\n$(date "+%F %T") Local: $BACKUP/$user.$DATE.tar"
    U_BACKUPS=$(ls $BACKUP/ | grep "^$user." | wc -l)
    update_user_value "$user" '$U_BACKUPS' "$U_BACKUPS"
}


# Defining ftp command function
ftpc() {
    /usr/bin/ftp -n $HOST $PORT <<EOF
    quote USER $USERNAME
    quote PASS $PASSWORD
    binary
    $1
    $2
    $3
    quit
EOF
}

# Defining ftp storage function
ftp_backup() {
    # Checking config
    if [ ! -e "$VESTA/conf/ftp.backup.conf" ]; then
        ftp_conf_error="Can't open  $VESTA/conf/ftp.backup.conf"
        echo "$ftp_conf_error" | $send_mail -s "$subj" $email
        echo "Error: $VESTA/conf/ftp.backup.conf doesn't exist"
        sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
        log_event "$E_NOTEXIST" "$EVENT"
        exit $E_NOTEXIST
    fi

    # Parse config
    source $VESTA/conf/ftp.backup.conf

    # Set current data
    DATE=$(date +%F)
    TIME=$(date +%T)

    # Set default port
    if [ -z "$(grep 'PORT=' $VESTA/conf/ftp.backup.conf)" ]; then
        PORT='21'
    fi

    # Checking variables
    if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
        rm -rf $tmpdir
        echo "Can't parse ftp backup configuration" |\
            $send_mail -s "$subj" $email
        echo "Error: Parsing error"
        sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
        log_event "$E_PARSING" "$EVENT"
        exit $E_PARSING
    fi


    # Debug info
    echo -e "$(date "+%F %T") Remote: ftp://$HOST$BPATH/$user.$DATE.tar"

    # Checking ftp connection
    fconn=$(ftpc)
    ferror=$(echo $fconn |grep -i -e failed -e error -e "Can't" -e "not conn")
    if [ ! -z "$ferror" ]; then
        rm -rf $tmpdir
        echo "Can't login to ftp://$USERNAME@$HOST" |\
            $send_mail -s "$subj" $email
        echo "Error: can't login to ftp ftp://$USERNAME@$HOST"
        sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
        log_event "$E_CONNECT" "$EVENT"
        exit $E_CONNECT
    fi

    # Check ftp permissions
    ftpc "mkdir $BPATH" > /dev/null 2>&1
    ftmpdir="$BPATH/vst.bK76A9SUkt"
    ftpc "mkdir $ftmpdir" "rm $ftmpdir"
    ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir" |grep -v Trying)
    if [ ! -z "$ftp_result" ] ; then
        rm -rf $tmpdir
        echo "Can't create ftp backup folder ftp://$HOST$BPATH" |\
            $send_mail -s "$subj" $email
        echo "Error: cant's create ftp folder ftp://$HOST$BPATH"
        sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
        log_event "$E_FTP" "$EVENT"
        exit $E_FTP
    fi

    # Checking retention
    backup_list=$(ftpc "cd $BPATH" "ls" |awk '{print $9}' |grep "^$user\.")
    backups_count=$(echo "$backup_list" | wc -l)
    if [ "$backups_count" -ge "$BACKUPS" ]; then
        backups_rm_number=$((backups_count - BACKUPS + 1))
        for backup in $(echo "$backup_list" | head -n $backups_rm_number); do 
            backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//")
            deprecated="$deprecated $backup"
            echo -e "$(date "+%F %T") Roated ftp backup: $backup_date"
            msg="$msg\n$(date "+%F %T") Roated ftp backup: $backup_date"
            ftpc "cd $BPATH" "delete $backup"
        done
    fi

    # Uploading backup archive
    if [ "$localbackup" = 'yes' ]; then
        cd $BACKUP
        ftpc "cd $BPATH" "put $user.$DATE.tar"
    else
        cd $tmpdir
        tar -cf $BACKUP/$user.$DATE.tar .
        cd $BACKUP/
        ftpc "cd $BPATH" "put $user.$DATE.tar"
        rm -f $user.$DATE.tar
    fi
}

echo "-- SUMMARY --"
msg="$msg\n-- SUMMARY --"

# Switching on backup system types
for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\\n}"); do
    case $backup_type in
        local) local_backup ;;
        ftp)   ftp_backup ;;
    esac
done

# Removing tmpdir
cd /
rm -rf $tmpdir

# Calculation run time
run_time=$((end_time - start_time))
run_time=$((run_time / 60))
current_time=$(date "+%T")
if [ "$run_time" -lt 1 ]; then
    run_time=1
fi
min=miutes
if [ "$run_time" -eq 1 ]; then
    min=minute
fi

echo "$(date "+%F %T") Size: $size Mb"
msg="$msg\n$(date "+%F %T") Size: $size Mb"
echo "$(date "+%F %T") Runtime: $run_time $min"
msg="$msg\n$(date "+%F %T") Runtime: $run_time $min"


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

# Deleting old backup records
for backup_record in $deprecated; do
    if [ -e "$USER_DATA/backup.conf" ]; then
        sed -i "/DATE='$backup_record/d" $USER_DATA/backup.conf
    fi
done

# Concatenating string
backup_str="BACKUP='$user.$DATE.tar'"
backup_str="$backup_str TYPE='$BACKUP_SYSTEM' SIZE='$size'"
backup_str="$backup_str WEB='${web_list// /,}'"
backup_str="$backup_str DNS='${dns_list// /,}'"
backup_str="$backup_str MAIL='${mail_list// /,}'"
backup_str="$backup_str DB='${db_list// /,}'"
backup_str="$backup_str CRON='$cron_list'"
backup_str="$backup_str UDIR='${udir_list// /,}'"
backup_str="$backup_str RUNTIME='$run_time' TIME='$TIME' DATE='$DATE'"
echo "$backup_str" >> $USER_DATA/backup.conf
chmod 660 $USER_DATA/backup.conf

# Clean backup queue
sed -i "/v-backup-user $user /d" $VESTA/data/queue/backup.pipe

# Send notification
if [ "$notify" != 'no' ]; then
    subj="$user → backup has been completed"
    email=$(get_user_value '$CONTACT')
    echo -e "$msg" | $send_mail -s "$subj" $email
fi

# Logging
log_event "$OK" "$EVENT"

exit
