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


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

# Argument defenition
user=$1

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


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

check_args '1' "$#" 'user'
validate_format 'user'
is_system_enabled "$BACKUP_SYSTEM"
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_backup_enabled
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") Load Average $la"
    echo
    sleep 60
    if [ "$i" -ge "15" ]; then
        echo "Error: LA is too high"
        log_event "$E_LA" "$EVENT"
        exit $E_LA
    fi
    (( ++i))
done


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

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

# Creating temporary random directory
tmpdir=$(mktemp -p $BACKUP -d)

# Prinitng status
echo "$(date "+%F %T") System backup for user $user"
echo "TMPDIR is $tmpdir"
echo

# Addding backup and vesta version
echo "1.0" > $tmpdir/backup_version
echo "$VERSION" > $tmpdir/vesta_version

# Vesta
echo "-- VESTA --"
vst='yes'
mkdir $tmpdir/vesta

# Backingup vesta configs
echo -e "$(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"
    cp -r $USER_DATA/stats.log $tmpdir/vesta/
fi

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

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

echo

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

# Checking excludes
OLD_IFS="$IFS"
IFS=$'\n'
if [ -e "$USER_DATA/backup.excludes" ]; then
    echo "-- Excludes --"

    for exclude in $(cat $USER_DATA/backup.excludes); do
        echo -e "$exclude"
        # Indirect variable references (a bit of black magic)
        eval ${exclude%%=*}=${exclude#*=}
    done
    echo
fi
IFS="$OLD_IFS"

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

    # Parsing unsuspeneded domains
    conf="$USER_DATA/web.conf"
    for domain in $(search_objects 'web' 'SUSPENDED' "*" 'DOMAIN'); do
	check_exl=$(echo "$WEB"|grep -w $domain)
        if [ -z "$check_exl" ]; then
            web_list="$web_list $domain"
        fi
    done
    web_list=$(echo "$web_list" | sed -e "s/  */\ /g" -e "s/^ //")

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

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

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

        # Apache config
        if [ "$WEB_SYSTEM" = 'apache' ]; then
            # Parsing httpd.conf
            tpl_file="$WEBTPL/apache_$TPL.tpl"
            conf="$HOMEDIR/$user/conf/web/httpd.conf"
            get_web_config_brds
            sed -n "$top_line,$bottom_line p" $conf > conf/httpd.conf

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

        # Nginx config
        if [ ! -z "$NGINX" ] ; then
            tpl_file="$WEBTPL/ngingx_$NGINX.tpl"
            conf="$HOMEDIR/$user/conf/web/nginx.conf"
            get_web_config_brds
            sed -n "$top_line,$bottom_line p" $conf > conf/nginx.conf

            # SSL check
            if [ "$SSL" = 'yes' ] ; then
                tpl_file="$WEBTPL/ngingx_$NGINX.stpl"
                conf="$HOMEDIR/$user/conf/web/snginx.conf"
                get_web_config_brds
                sed -n "$top_line,$bottom_line p" $conf > conf/snginx.conf
            fi
        fi

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

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

        # Packing data folders
        cd $HOMEDIR/$user/web/$domain
        domain_direcotries=$(ls |grep -v logs)
        tar -cf $tmpdir/web/$domain/domain_data.tar $domain_direcotries

        if [ ! -z "$BACKUP_GZIP" ]; then
            gzip -$BACKUP_GZIP $tmpdir/web/$domain/domain_data.tar
        fi
    done
    echo
fi

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

    # Parsing unsuspeneded domains
    for domain in $(search_objects 'dns' 'SUSPENDED' "*" 'DOMAIN'); do
	check_exl=$(echo "$DNS"|grep -w $domain)
        if [ -z "$check_exl" ]; then
            dns_list="$dns_list $domain"
        fi
    done
    dns_list=$(echo "$dns_list" | sed -e "s/  */\ /g" -e "s/^ //")

    for domain in $dns_list; do
        echo -e "$(date "+%F %T") $domain"
        # Building directory tree
        mkdir -p $tmpdir/dns/$domain/conf
        mkdir -p $tmpdir/dns/$domain/vesta

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

        # Backingup dns recods
        cp $USER_DATA/dns/$domain.conf vesta/$domain.conf
        cp $HOMEDIR/$user/conf/dns/$domain.db conf/$domain.db
    done
    echo
fi


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

    # Parsing unsuspeneded domains
    conf="$USER_DATA/mail.conf"
    for domain in $(search_objects 'mail' 'SUSPENDED' "*" 'DOMAIN'); do
	check_exl=$(echo "$MAIL"|grep -w $domain)
        if [ -z "$check_exl" ]; then
            mail_list="$mail_list $domain"
        fi
    done
    mail_list=$(echo "$mail_list" | sed -e "s/  */\ /g" -e "s/^ //")

    for domain in $mail_list; do
        echo -e "$(date "+%F %T") $domain"
        #mkdir -p $tmpdir/mail/$domain/accounts
        mkdir -p $tmpdir/mail/$domain/conf
        mkdir -p $tmpdir/mail/$domain/vesta

        # Creating exim config
        cd $tmpdir/mail/$domain/
        cp $HOMEDIR/$user/conf/mail/$domain/* conf/

        # Creating vesta config
        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

        # Packing mailboxes
        cd $HOMEDIR/$user/mail/$domain
        accounts=$(ls)
        if [ ! -z "$accounts" ]; then
            tar -cf $tmpdir/mail/$domain/accounts.tar  $accounts
        fi

        if [ ! -z "$BACKUP_GZIP" ] && [ ! -z $accounts ]; then
            gzip -$BACKUP_GZIP $tmpdir/mail/$domain/accounts.tar
        fi
    done
    echo
fi


# DatbaBases
if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then
    echo "-- DB --"
    mkdir $tmpdir/db/

    # Parsing unsuspeneded domains
    for database in $(search_objects 'db' 'SUSPENDED' "*" 'DB'); do
	check_exl=$(echo "$DB"|grep -w $database)
        if [ -z "$check_exl" ]; then
            db_list="$db_list $database"
        fi
    done
    db_list=$(echo "$db_list" | sed -e "s/  */\ /g" -e "s/^ //")

    for database in $db_list; do
        mkdir -p $tmpdir/db/$database/conf
        mkdir -p $tmpdir/db/$database/vesta
        cd $tmpdir/db/$database/

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

        get_database_values
        dump="$tmpdir/db/$database/$database.$TYPE.sql"
        grants="$tmpdir/db/$database/conf/$database.$TYPE.$DBUSER"
        echo -e "$(date "+%F %T") $database $TYPE"
        case $TYPE in
            mysql) dump_mysql_database ;;
            pgsql) dump_pgsql_database ;;
        esac
        if [ ! -z "$BACKUP_GZIP" ]; then 
            gzip -$BACKUP_GZIP $dump
        fi
    done
    echo
fi

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

    # Backingup cron recods
    echo -e "$(date "+%F %T") cron.conf"
    cp $USER_DATA/cron.conf $tmpdir/cron/
    echo -e "$(date "+%F %T") system cron"

    if [ -e "/var/spool/cron/$user" ]; then
        cron_list='yes'
        cp /var/spool/cron/$user $tmpdir/cron/
    fi
    echo
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(){
    echo "-- STORAGE --"
    echo -e "$(date "+%F %T") ARCHIVE $BACKUP/$user.$DATE.tar"

    # Removing dublicate for this day
    if [ -e "$BACKUP/$user.$DATE.tar" ]; then
        deprecated="$DATE"
        echo -e "Deleting old backup for $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 "\tDeleteing old backup for $backup_date"
            rm -f $BACKUP/$backup
        done
    fi

    # Checking disk space
    disk_usage=$(df $BACKUP | awk '{print $5}'|tail -n1|cut -f 1 -d '%')
    if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
        echo "Error: Not enough disk space"
        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 nginx:$user $BACKUP/$user.$DATE.tar
    localbackup='yes'
    echo

    U_BACKUPS=$(ls $BACKUP/|grep "^$user."|wc -l)
    update_user_value "$user" '$U_BACKUPS' "$U_BACKUPS"
}


# Defining ftp command function
ftpc() {
    ftp -n $HOST $PORT <<EOF
    quote USER $USERNAME
    quote PASS $PASSWORD
    binary
    cd $BPATH
    $1
    quit
EOF
}

# Defining ftp storage function
ftp_backup(){
    echo "-- FTP --"

    # Checking config
    source $VESTA/conf/ftp.backup.conf
    if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ] ||\
        [ -z "$BPATH" ]; then
        echo "Error: Parsing error"
        log_event "$E_PARSING" "$EVENT"
        exit $E_PARSING
    fi

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

    # 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 "$E_FTP" "$EVENT"
        exit $E_FTP
    fi

    # Checking retention
    backup_list=$(ftpc "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 "\tDeleteing old backup for $backup_date"
            ftpc "delete $backup"
        done
    fi

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

# 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") Backup took $run_time $min"
echo
echo


#----------------------------------------------------------#
#                       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 VESTA='$vst'"
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 RUNTIME='$run_time' TIME='$TIME' DATE='$DATE'"
echo "$backup_str" >> $USER_DATA/backup.conf
chmod 660 $USER_DATA/backup.conf

# Logging
log_event "$OK" "$EVENT"

exit
