########################################################################
#
# Purpose: Simple Restore of columnstore
# Example: sudo ./simpleRestore.bash
#
# If doing a Local Storage backup:
#    - loadDate
#    - backupLocation
#    - backupDestination
#    - scp (only required if restoreing a backup from a remote location)
#    - storage="LocalStorage"
#    - DBRootCount
# 
#  Examples:
#        ./simpleRestore.bash -bl /tmp/backups/ -bd Local -s LocalStorage -l 12-29-2021 -dbs 1
#        ./simpleRestore.bash -bl /tmp/backups/ -bd Remote -scp root@172.31.6.163 -s LocalStorage -l 12-29-2021 -dbs 3
#   
# If doing an S3 based backup  
#    - backupBucket
#    - storage="S3"
#    - s3-endpoint (optional)
#
#   Examples:
#        ./simpleRestore.bash -bb backup-clone-mcs-bucket-test-allen -s S3
#        ./simpleRestore.bash -bb on-premise-bucket -s S3 -l 12-29-2021 -url 127.0.0.1:9001
# 
########################################################################

# What date folder to load from the backupLocation
loadDate=''

# Where the backup to load is found
# Example: /mnt/backups/
backupLocation="/tmp/backups/"

# Is this backup on the same or remote server compared to where this script is running
# Options: "Local" or "Remote"
backupDestination="Local"

# Used only if backupDestination=Remote
# The user/credentials that will be used to scp the backup files
# Example: "centos@10.14.51.62"
scp=""

# What storage topogoly was used by Columnstore in the backup - found in storagemanager.cnf
# Options: "LocalStorage" or "S3" 
storage="LocalStorage"

# Only used if storage=S3
# Name of the bucket to copy into to store the backup S3 data
# Example: "backup-mcs-bucket"
backupBucket=''
pm=$(cat /var/lib/columnstore/local/module)

# Used by on premise S3 vendors
# Example: "127.0.0.1:9001"
s3url=""

# Number of DBroots - TODO: detect automatically
# Integer usually 1 or 3
DBRootCount=1

# Dynamic Arguments
while [[ $# -gt 0 ]]; do
  key="$1"

  case $key in
    -l|--load)
        loadDate="$2"
        shift # past argument
        shift # past value
        ;;
    -bl|--backuplocation)
        backupLocation="$2"
        shift # past argument
        shift # past value
        ;;
    -bd|--backupdestination)
        backupDestination="$2"
        shift # past argument
        shift # past value
        ;;
    -scp)
        scp="$2"
        shift # past argument
        shift # past value
        ;;
    -bb|--backupbucket)
        backupBucket="$2"
        shift # past argument
        shift # past value
        ;;
    -url| --s3-endpoint)
        s3url="$2"
        shift # past argument
        shift # past value
        ;;
    -s|--storage)
        storage="$2"
        shift # past argument
        shift # past value
        ;;
    -dbs|--dbroots)
        DBRootCount="$2"
        shift # past argument
        shift # past value
        ;;
    -h|--help)
        echo "
Simple Restore for Columnstore

    -l  | --load                   what backup to load
    -bl | --backuplocation         directory where the backup was saved
    -bd | --backupdestination      if the directory is 'Local' or 'Remote' to this script
    -scp                           scp connection to remote server if -bd 'Remote'
    -bb | --backupbucket           bucket name to load S3 backups
    -url| --s3-endpoint            onprem url to s3 storage api example: 127.0.0.1:9001
    -s  | --storage                The storage used by columnstore data 'LocalStorage' or 'S3'

    Examples:
        ./simpleRestore.bash -bl /tmp/backups/ -bd Local -s LocalStorage -l 12-29-2021
        ./simpleRestore.bash -bl /tmp/backups/ -bd Remote -scp root@172.31.6.163 -s LocalStorage -l 12-29-2021
        ./simpleRestore.bash -bb backup-clone-mcs-bucket-test-allen -s S3 -l 12-29-2021
        ./simpleRestore.bash -bb on-premise-bucket -s S3 -l 12-29-2021 -url 127.0.0.1:9001
        "
        exit 1;
        ;;
    *)    # unknown option
        echo "unknown flag: $1"
        exit 1
  esac
done

# Check MairaDB-backup installed
if ! command -v mariadb-backup  > /dev/null; then
    printf "\n\n Please make sure mariadb-backup is installed and executable\n\n"
    yum install MariaDB-backup
    printf "\n\n Please rerun backup \n\n"
    exit 1
fi

# Validate some arguments
if [ $storage != "LocalStorage" ] && [ $storage != "S3" ]; then echo "Invalid Script Variable storage: $storage"; exit 1; fi
if [ -z "$loadDate" ]; then echo "Invalid --load: $loadDate - is empty"; exit 1; fi


# Check if remote - that scp works 
if [ $backupDestination == "Remote" ]; then
    if ssh $scp echo ok 2>&1 ;then
        printf 'SSH Works\n\n'
    else
        echo "Failed Command: ssh $scp"
        exit 1;
    fi
fi

# Adjust s3api flags based on inputs
finalMessage="Restore Complete"
additionalS3apiFlags=""
if [ ! -z "$s3url" ];  then
    additionalS3apiFlags=" --s3-endpoint $s3url";
fi

# Make sure the database is offline
if [ -z $(pidof PrimProc) ]; then printf "[+] Columnstore is offline ... done\n"; else printf "[X] Columnstore is ONLINE - please turn off \n\n"; exit 1; fi
if [ -z $(pidof mariadbd) ]; then printf "[+] MariaDB is offline ... done\n"; else printf "[X] MariaDB is ONLINE - please turn off \n\n"; exit 1; fi

# Branch logic based on topology
if [ $storage == "LocalStorage" ]; then 
    # Validate addtional relevant arguments
    if [ -z "$backupLocation" ]; then echo "Invalid --backuplocation: $backupLocation - is empty"; exit 1; fi
    if [ -z "$backupDestination" ]; then echo "Invalid --backupdestination: $backupDestination - is empty"; exit 1; fi
    if [ $backupDestination == "Remote" ] && [ -d $backupLocation$loadDate ]; then echo "Switching to '-bd Local'"; backupDestination="Local"; fi
    
    # Branch logic based on where the backup resides
    if [ $backupDestination == "Local" ]; then
        
        # Validate addtional relevant arguments
        if [ ! -d $backupLocation ]; then echo "Invalid directory --backuplocation: $backupLocation"; exit 1; fi
        if [ ! -d $backupLocation$loadDate ]; then echo "Invalid directory --load: $backupLocation$loadDate"; exit 1; fi

        # Loop through per dbroot
        printf "[~] Columnstore Data..."; i=1;
        while [ $i -le $DBRootCount ]; do
            if [ $pm == "pm$i" ]; then rm -rf /var/lib/columnstore/data$i/; rsync -av $backupLocation$loadDate/data$i/ /var/lib/columnstore/data$i/; chown mysql:mysql -R /var/lib/columnstore/data$i/; fi
            ((i++))
        done
        printf "[+] Columnstore Data... Done\n" 
        
        # Put configs in place
        printf "[~] Columnstore Configs... "
        rsync -av $backupLocation$loadDate/configs/storagemanager.cnf /etc/columnstore/storagemanager.cnf 
        rsync -av $backupLocation$loadDate/configs/cmapi_server.conf /etc/columnstore/cmapi_server.conf 
        rsync -av $backupLocation$loadDate/configs/mysql/$pm/ /etc/my.cnf.d/ 
        printf "[+] Columnstore Configs... Done\n" 
       
    elif [ $backupDestination == "Remote" ]; then
        printf "[~] Copy MySQL Data..."
        tmp="localscpcopy-$loadDate"
        rm -rf $backupLocation$tmp/mysql/
        mkdir -p $backupLocation$tmp/mysql/
        rsync -av $scp:$backupLocation/$loadDate/mysql/ $backupLocation$tmp/mysql/
        printf "[+] Copy MySQL Data... Done\n"

        # Loop through per dbroot
        printf "[~] Columnstore Data..."; i=1;
        while [ $i -le $DBRootCount ]; do
            if [ $pm == "pm$i" ]; then rm -rf /var/lib/columnstore/data$i/; rsync -av $scp:$backupLocation$loadDate/data$i/ /var/lib/columnstore/data$i/ ; chown mysql:mysql -R /var/lib/columnstore/data$i/;fi
            ((i++))
        done
        printf "[+] Columnstore Data... Done\n" 

        # Put configs in place
        printf "[~] Columnstore Configs... "
        rsync -av $backupLocation$loadDate/configs/storagemanager.cnf /etc/columnstore/storagemanager.cnf 
        rsync -av $backupLocation$loadDate/configs/cmapi_server.conf /etc/columnstore/cmapi_server.conf 
        rsync -av $backupLocation$loadDate/configs/mysql/$pm/ /etc/my.cnf.d/ 
        printf "[+] Columnstore Configs... Done\n"
        loadDate=$tmp
    else 
        echo "Invalid Script Variable --backupdestination: $backupDestination"
        exit 1
    fi

    # Standard mysql restore
    mariabackup --prepare --target-dir=$backupLocation$loadDate/mysql/
    rm -rf /var/lib/mysql
    mariabackup --copy-back --target-dir=$backupLocation$loadDate/mysql/
    chown -R mysql:mysql /var/lib/mysql/
    chown -R mysql:mysql /var/log/mariadb/

elif [ $storage == "S3" ]; then

    # Check aws-cli installed
    if ! command -v aws > /dev/null; then
        which aws
        echo "Hints:  
        curl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"
        unzip awscliv2.zip
        sudo ./aws/install
        aws configure"

        printf "\n\n Please make sure aws cli is installed configured and executable\n\n"
        exit 1
    fi

    # Validate addtional relevant arguments
    if [ -z "$backupBucket" ]; then echo "Invalid --backupbucket: $backupBucket - is empty"; exit 1; fi
    # TODO check s3 bucket exists : aws s3 ls s3://$backupBucket/$loadDate

    printf "[~] Columnstore Configurations and metadata\n"
    #aws s3 cp s3://$backupBucket/$loadDate/configs/Columnstore.xml /etc/columnstore/Columnstore.xml
    aws $additionalS3apiFlags s3 cp s3://$backupBucket/$loadDate/configs/storagemanager.cnf /etc/columnstore/storagemanager.cnf 
    aws $additionalS3apiFlags s3 cp s3://$backupBucket/$loadDate/configs/cmapi_server.conf /etc/columnstore/cmapi_server.conf 
    aws $additionalS3apiFlags s3 cp s3://$backupBucket/$loadDate/configs/mysql/$pm/ /etc/my.cnf.d/  --recursive
    aws $additionalS3apiFlags s3 cp s3://$backupBucket/$loadDate/storagemanager/ /var/lib/columnstore/storagemanager/ --recursive 
    if [ $pm == "pm1" ]; then aws $additionalS3apiFlags s3 cp s3://$backupBucket/$loadDate/data1/ /var/lib/columnstore/data1/  --recursive ; fi
    printf "[+] Columnstore Configurations and metadata ... done \n\n"

    bucket=$( grep -m 1 "bucket =" /etc/columnstore/storagemanager.cnf)
    sed -i "s/$bucket/bucket = $backupBucket/g" /etc/columnstore/storagemanager.cnf
    # prefix=$( grep -m 1 "prefix =" /etc/columnstore/storagemanager.cnf)
    # sed -i "s/$prefix/prefix = $loadDate/g" /etc/columnstore/storagemanager.cnf
    chown -R mysql:mysql /var/lib/columnstore/
    chown -R root:root /etc/my.cnf.d/

    # Check if S3 connection works
    if testS3Connection; then echo "[+] S3 Connection passes" ; else printf "\n[X] S3 Connection issues - retest/configure\n"; exit 1; fi

    # Copy the MariaDB data, prepare and put in place
    printf "\n[~] Preparing and Copying back MariaDB data\n"
    rm -rf $backupLocation$loadDate/mysql/   
    mkdir -p $backupLocation$loadDate/mysql/
    aws $additionalS3apiFlags s3 cp s3://$backupBucket/$loadDate/mysql/ $backupLocation$loadDate/mysql  --recursive  
    mariabackup --prepare --target-dir=$backupLocation$loadDate/mysql/
    rm -rf /var/lib/mysql
    mariabackup --copy-back --target-dir=$backupLocation$loadDate/mysql/
    chown -R mysql:mysql /var/lib/mysql/
    chown -R mysql:mysql /var/log/mariadb/
    printf "[+] Preparing and Copying back MariaDB data ... done\n"

fi

printf "\n\n[+] $finalMessage\n"
printf " - Last you need to manually configure mariadb replication between nodes\n"
printf " - Check /etc/columnstore/Columnstore.xml customizations via mcsGetConfig -a\n"
printf " - systemctl start mariadb \n"
printf " - mcsStart \n\n\n"




