#!/bin/bash if [ ! -d /opt/sqlbak/ ]; then mkdir /opt/sqlbak/ fi function get_installation_id { if [ -f /dev/sqlbak.pranas.txt ]; then installation_id=$(cat /dev/sqlbak.pranas.txt) if [[ ! "$installation_id" =~ ^\{?[A-F0-9a-f]{8}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{12}\}?$ ]]; then installation_id=$(get_uuid) echo "$installation_id" >> /dev/sqlbak.pranas.txt fi else installation_id=$(get_uuid) echo "$installation_id" >> /dev/sqlbak.pranas.txt fi if [ -z "${installation_id}" ]; then installation_id=null else installation_id="\"$installation_id\"" fi echo $installation_id } function get_uuid { if [ ! $(which uuidgen) ]; then s=$(head /dev/urandom | tr -dc A-Fa-f0-9) uuid="${s:0:8}-${s:9:4}-${s:14:4}-${s:19:4}-${s:24:12}" else uuid=$(uuidgen) fi echo $uuid } function trace_event { event_id=$(get_uuid) date=$(date +%s%3N) installation_id=$(get_installation_id) request="{\"EventId\": \"$event_id\", \"ApplicationTypeId\": 2, \"TimeStamp\": \"/Date($date)/\", \"MajorVersion\": 1, \"MinorVersion\": 10, \"PatchVersion\": 9, \"InstallationId\": $installation_id, \"SessionId\": \"$session_id\", \"EventSource\": \"Inst\", \"EventType\": \"$1\", \"EventData\": \"$2\"}" curl -s -o /opt/sqlbak/sqlbak_out -d "${request}" -H "Content-Type: application/json" -X POST $trace_url } function eie { local exit_code=$1 shift [[ $exit_code ]] && ((exit_code != 0)) && { if [ ! -f /opt/sqlbak/sqlbak_out ]; then error_message="File for errors was not found. Error message have been lost" else sed -r '/^\s*$/d' /opt/sqlbak/sqlbak_out error_message=$(tail -n 1 /opt/sqlbak/sqlbak_out) rm -rf /opt/sqlbak/sqlbak_out fi trace_event "Error" "exit_code:$exit_code, error:$error_message" print_text "\nThe app installation has finished with the error: $error_message" exit "$exit_code" } } function print_text { echo -e "\n\e[1m${1}\e[0m\n" } secret_key=$1 server_name=$2 session_id=$(get_uuid) trace_url="https://api.sqlbak.com/Client/Service/ClientTrace.svc/rest/TraceApplicationEvent" [[ $server_name == "" ]] && server_name_param="" || server_name_param="-n $server_name" if [ -f /etc/os-release ]; then . /etc/os-release || eie $? VER=$VERSION_ID OS=$ID elif type lsb_release > /dev/null 2>&1; then VER=$(lsb_release -sr) OS=$(lsb_release -si) elif [ -f /etc/lsb-release ]; then . /etc/lsb-release || eie $? VER=$DISTRIB_RELEASE OS=$DISTRIB_ID elif [ -f /etc/debian_version ]; then VER=$(cat /etc/debian_version) OS="debian" elif [ -f /etc/SuSe-release ]; then VER="OLD" OS="suse" elif [ -f /etc/redhat-release ]; then VER="OLD" OS="centos" else VER=$(uname -r) OS=$(uname -n) fi trace_event "Start" "server:$OS, version:$VER" case $OS in "ubuntu" | "debian" | "linuxmint" | "kali") apt-get install -y gnupg2 apt-transport-https print_text "Get a public gpg.key" curl -sSL https://sqlbak.com/deb/1/10/9/key/deb.gpg.key | apt-key add - 2> /opt/sqlbak/sqlbak_out || eie $? rm -rf /etc/apt/sources.list.d/sqlbak.list print_text "Add a link to SqlBak app’s package to the repository list" echo "deb [arch=amd64] https://sqlbak.com/deb/1/10/9 stable main" >> /etc/apt/sources.list.d/sqlbak.list 2> /opt/sqlbak/sqlbak_out || eie $? print_text "Update the package repository list" apt-get update print_text "Install the app" apt-get install -y sqlbak 2> /opt/sqlbak/sqlbak_out || eie $? ;; "rhel" | "centos" | "oracle" | "ol" | "amzn" | "photon") rm -rf /etc/yum.repos.d/sqlbak.repo print_text "Add a link to SqlBak app’s package to the repository list..." sh -c "echo ' [sqlbakrepo] name=Sqlbak Repository baseurl=https://sqlbak.com/rpm/1/10/9/ enabled=1 gpgcheck=0' >> /etc/yum.repos.d/sqlbak.repo" 2> /opt/sqlbak/sqlbak_out || eie $? print_text "Install the app..." yum install -y sqlbak 2> /opt/sqlbak/sqlbak_out || eie $? ;; "sles" | "suse" | "opensuse-leap") rm -rf /etc/zypp/repos.d/sqlbak.repo print_text "Add a link to SqlBak app’s package to the repository list..." sh -c 'echo " [sqlbak] name=sqlbak baseurl=https://sqlbak.com/rpm/1/10/9/ enabled=1 type=rpm-m gpgcheck=0" >> /etc/zypp/repos.d/sqlbak.repo' 2> /opt/sqlbak/sqlbak_out || eie $? print_text "Update the package repository list..." zypper update print_text "Install the app..." zypper install sqlbak 2> /opt/sqlbak/sqlbak_out || eie $? ;; "fedora") rm -rf /etc/yum.repos.d/sqlbak.repo print_text "Add a link to SqlBak app’s package to the repository list..." sh -c 'echo " [sqlbakrepo] name=Sqlbak Repository baseurl=https://sqlbak.com/rpm/1/10/9/ enabled=1 gpgcheck=0" >> /etc/yum.repos.d/sqlbak.repo' 2> /opt/sqlbak/sqlbak_out || eie $? print_text "Update the package repository list..." dnf update print_text "Install the app..." dnf install -y sqlbak 2> /opt/sqlbak/sqlbak_out || eie $? ;; "raspbian") apt-get install -y gnupg2 apt-transport-https print_text "Get a public gpg.key" curl -sSL https://sqlbak.com/raspberry_deb/1/10/9/key/deb.gpg.key | apt-key add - 2> /opt/sqlbak/sqlbak_out || eie $? rm -rf /etc/apt/sources.list.d/sqlbak.list print_text "Add a link to SqlBak app’s package to the repository list" echo "deb https://sqlbak.com/raspberry_deb/1/10/9 stable main" >> /etc/apt/sources.list.d/sqlbak.list 2> /opt/sqlbak/sqlbak_out || eie $? print_text "Update the package repository list" apt-get update print_text "Install the app" apt-get install -y sqlbak 2> /opt/sqlbak/sqlbak_out || eie $? ;; *) print_text "It seems your distro $OS is not supported yet. Please, ask SqlBak.com support" esac trace_event "Finish" "Success" print_text "Register the application on sqlbak.com ..." sqlbak -r -k $secret_key $server_name_param 2> /opt/sqlbak/sqlbak_out || eie $?