#!/bin/bash

# site_name site_loc back_loc db_name db_user db_pass

NOW=$(date +%Y%m%d-%H%M%S)
LOG=/root/backup.log
FILES=1
DB=1

BACKUP_PSW_FILE=/root/.database


OLD_IFS="$IFS"
IFS=$'\n'

for LINE in `cat $BACKUP_PSW_FILE`; do
    IFS=' '
    ARRAY=($LINE)
    site_name=${ARRAY[0]}
    site_loc=${ARRAY[1]}
    back_loc=${ARRAY[2]}
    db_name=${ARRAY[3]}
    db_user=${ARRAY[4]}
    db_pass=${ARRAY[5]}
#    DB_SQL="${DB_NAME}.${DB_TYPE}.sql"

    echo "$(date)=====================START $site_name backup======================" >> $LOG

# ARCHIVING WEB SITES INTO TAR.GZ

    if tar -zcpf $back_loc/$NOW-$site_name.tar.gz $site_loc; then
        echo "$(date) $NOW-$site_name.tar.gz creation PASS" >> $LOG
        if gunzip -c $back_loc/$NOW-$site_name.tar.gz | tar t > /dev/null; then
       echo "$(date) $NOW-$site_name.tar.gz check PASS" >> $LOG
       FILES=0
        else
       echo "$(date) $NOW-$site_name.tar.gz check FAIL" >> $LOG
       rm $back_loc/$NOW-$site_name.tar.gz
        fi
    else
        echo "$(date) $NOW-$site_name.tar.gz creation FAIL" >> $LOG
    fi

done

IFS="$OLD_IFS"