HEX
Server: nginx/1.29.3
System: Linux 11979.bigscoots-wpo.com 6.8.0-88-generic #89-Ubuntu SMP PREEMPT_DYNAMIC Sat Oct 11 01:02:46 UTC 2025 x86_64
User: nginx (1068)
PHP: 7.4.33
Disabled: exec,system,passthru,shell_exec,proc_open,proc_close,popen,show_source,cmd# Do not modify this line # 1684243876
Upload Files
File: //proc/1284358/cwd/bigscoots/wpo_backups_ovz_restore.sh
#!/bin/bash
# options
# h = human readable

source /bigscoots/includes/common.sh

KOPIA_STATUS=$(bash /bigscoots/wpo/manage/bsbackup_manager.sh check 2>/dev/null)

if echo "$KOPIA_STATUS" | grep -q '"success": true'; then
    exit
fi

date=$(date "+%Y-%m-%dT%H_%M_%S")
HOMEDIR=/home/nginx/domains/
BKSVR=backup3.bigscoots.com
BSPATH=/root/.bigscoots
PATH=/usr/lib64/ccache:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
BKLIMIT=30
serverip=$($(which ifconfig) | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | head -1)

if grep bksvr "$BSPATH"/backupinfo >/dev/null 2>&1 ; then
  BKSVR=$(grep bksvr "$BSPATH"/backupinfo | sed 's/bksvr=//g')
fi

if [ -f /proc/vz/veinfo ] && ! grep -qs '/backup ' /proc/mounts ; then
  remote=y
  if grep -q bkuser= "${BSPATH}"/backupinfo; then
    BKUSER=$(grep bkuser= "${BSPATH}"/backupinfo | sed 's/=/ /g' | awk '{print $2}')
  else
  BKUSER=wpo$(awk '{print $1}' /proc/vz/veinfo)
  fi
elif ! grep -qs '/backup ' /proc/mounts && ! grep destination=remote "$BSPATH"/backupinfo >/dev/null 2>&1 ; then
  send_slack_alert "#wpo-backups" ":warning:" "Backup drive not mounted in $HOSTNAME" "N/A" "Make sure to set destination=remote in "${BSPATH}"/backupinfo if supposed to be remote backups."
  remote=y
  BKUSER=wpo"${HOSTNAME//./}"
elif ! grep -qs '/backup ' /proc/mounts && grep destination=remote "$BSPATH"/backupinfo >/dev/null 2>&1 ; then
  remote=y
  if [[ -n $(grep bkuser= "${BSPATH}"/backupinfo | sed 's/=/ /g' | awk '{print $2}') ]]; then
    BKUSER=$(grep bkuser= "${BSPATH}"/backupinfo | sed 's/=/ /g' | awk '{print $2}')
  else
    BKUSER=wpo"${HOSTNAME//./}"
  fi
fi


if [ -f /proc/vz/veinfo ] && ! grep -qs '/backup ' /proc/mounts || grep -q destination=remote "$BSPATH"/backupinfo
then

  if [[ ! $(pwd) == *public ]]
  then
     echo "Run this script within the public directory of the WP install, quiting..."
     exit 1
  else
    DOMAIN=$(echo "$(pwd)"| sed "s=/home/nginx/domains/==g ; s=/public==g")
  fi

  if ssh -i "$HOME"/.ssh/wpo_backups "$BKUSER"@"$BKSVR" 'uptime' >/dev/null ; [ $? -eq 255 ]
  then
    echo "Connection to backup server has failed."
    exit 1
  fi

# h - used for listing available backups in format for WPO panel
# /bigscoots/wpo_backups_ovz_restore.sh h [daily/manual]

  case $1 in
  h)

  if [ -z "$2" ]; then

   echo "/bigscoots/wpo_backups_ovz_restore.sh h [daily/manual] required"
   exit

  fi

  # /bigscoots/wpo_backups_ovz_restore.sh h daily
  # This will list out backups in WPO panel format for daily backups

  if [ "$2" = "daily" ]; then

  ssh -i "$HOME"/.ssh/wpo_backups "$BKUSER"@"$BKSVR" "ls -1d */$DOMAIN | sed 's/back-//g ; s/T/ /g ; s/_/:/g ; s/incomplete:/incomplete_/g ; s/\/$DOMAIN//g' | grep -v 'current\|manual' | sed 's/$/;/g'"

  # /bigscoots/wpo_backups_ovz_restore.sh h manual
  # This will list out backups in WPO panel format for manual backups

  elif [ "$2" = "manual" ]; then

  ssh -i "$HOME"/.ssh/wpo_backups "$BKUSER"@"$BKSVR" "ls -1d */$DOMAIN | sed 's/incomplete_back-//g ; s/back-//g ; s/T/ /g ; s/_/:/g ; s/\/$DOMAIN//g' | grep 'manual' | sed 's/manual-//g' | sed 's/$/;/g'"

  fi

  # /bigscoots/wpo_backups_ovz_restore.sh hh 
  # this will list out all backups for the current wordpress install, must be in the public directory.

  ;;
  hh)

  ssh -i "$HOME"/.ssh/wpo_backups "$BKUSER"@"$BKSVR" "ls -1d */$DOMAIN | sed 's/\/$DOMAIN//g' |grep -v current"

  echo
  echo "Example rsync command - You should run a backup before proceeding"
  echo "# rsync -ahv -e \"ssh -i $HOME/.ssh/wpo_backups\" --delete $BKUSER@$BKSVR:$(ssh -i $HOME/.ssh/wpo_backups $BKUSER@$BKSVR 'echo $HOME')/$(ssh -i $HOME/.ssh/wpo_backups $BKUSER@$BKSVR "ls -1d */$DOMAIN | sed 's/\/$DOMAIN//g' |grep -v 'current\|manual-\|incomplete_'" | tail -1)/$DOMAIN/public/ $(pwd)/"
  echo

  # /bigscoots/wpo_backups_ovz_restore.sh restore ${BACKUP}
  # This will restore the current site from the date specified, must be in the public directory.
  # Backup name example: back-2019-06-11T02_18_01

  ;;
  restore)

  init_json_response

  # Retrieve the DB name from wp-config.php
  dbname=$(wpcli config get DB_NAME 2>&1)
  if [ $? -eq 0 ]; then
      add_json_message "DB_NAME retrieved: $dbname"
  else
      add_json_error "Failed to retrieve DB_NAME: $dbname"
      send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Failed to retrieve DB_NAME. Output: $dbname"
      set_json_success false
      print_json_response
      exit 1
  fi

  # Check if the backup location exists on the backup server
  backup_check=$(ssh -i "$HOME/.ssh/wpo_backups" "$BKUSER@$BKSVR" "[ -d ~/$2/$DOMAIN/public ]" 2>&1)
  if [ $? -ne 0 ]; then
      # Directory does not exist
      add_json_error "Backup location does not exist: $BKUSER@$BKSVR /$2/$DOMAIN/public"
      send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Backup location not found. Output: $backup_check"
      set_json_success false
      print_json_response
      exit 1
  else
      # Directory exists, proceed with rsync
      rsync_output=$(rsync -ah -e "ssh -i $HOME/.ssh/wpo_backups" --delete "$BKUSER@$BKSVR":~/"$2"/"$DOMAIN"/public/ "$(pwd)"/ 2>&1)
      if [ $? -eq 0 ]; then
          add_json_message "Files restored from backup: $BKUSER@$BKSVR $2 $DOMAIN"
      else
          add_json_error "Failed to restore files: $rsync_output"
          send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Failed to restore files from backup. Output: $rsync_output"
          set_json_success false
          print_json_response
          exit 1
      fi
  fi

  # Check for malicious @include statements
  grep_output=$(grep -q '@include "' ./*.php 2>&1)
  if [ $? -eq 0 ]; then
      add_json_message "Found malicious '@include' statements in PHP files."

      # Remove the malicious includes
      sed_output=$(sed -i '/@include "/d' ./*.php 2>&1)
      add_json_message "Removed malicious '@include' statements from PHP files."
      send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Found and removed malicious '@include' statements. Please check the site for further issues."
  else
      add_json_message "No malicious '@include' statements found in PHP files."
  fi

  # Backup the current database
  backup_output=$(mysqldump --routines --events --single-transaction "$dbname" | gzip > ../"$dbname".sql.gz 2>&1)
  if [ -f "../$dbname.sql.gz" ]; then
      add_json_message "Current database backed up: $dbname"
  else
      add_json_error "Failed to backup the current database: $backup_output"
      send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Database backup failed. Output: $backup_output"
      set_json_success false
      print_json_response
      exit 1
  fi

  # Drop the current database
  drop_output=$(mysqladmin -s drop -f "$dbname" 2>&1)
  if [ $? -eq 0 ]; then
      add_json_message "Dropped current database: $dbname"
  else
      add_json_error "Failed to drop current database: $drop_output"
  fi

  # Recreate the database
  create_output=$(mysqladmin create "$dbname" 2>&1)
  if [ $? -eq 0 ]; then
      add_json_message "Database recreated: $dbname"
  else
      add_json_error "Failed to recreate database: $create_output"
  fi

  # Restore from the backup file
  if [ -f "$dbname.sql.gz" ]; then
      restore_output=$(zcat "$dbname.sql.gz" | mysql -f --max_allowed_packet=1G "$dbname" 2>&1)
      if [ $? -eq 0 ]; then
          add_json_message "Database restored from compressed backup: $dbname"
      else
          add_json_error "Failed to restore database from compressed backup: $restore_output"
          set_json_success false
          print_json_response
          exit 1
      fi
  elif [ -f "$dbname.sql" ]; then
      restore_output=$(mysql -f --max_allowed_packet=1G "$dbname" < "$dbname.sql" 2>&1)
      if [ $? -eq 0 ]; then
          add_json_message "Database restored from uncompressed backup: $dbname"
      else
          add_json_error "Failed to restore database from uncompressed backup: $restore_output"
          set_json_success false
          print_json_response
          exit 1
      fi
  else
      file_list=$(ls -1)
      add_json_error "Backup file not found for database restoration: $dbname"
      send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Backup had no database to restore. Files in directory: $file_list"
      set_json_success false
      print_json_response
      exit 1
  fi

  # "Setting proper permissions..."
  bash -c 'source /bigscoots/includes/common.sh ; correct_permissions_ownership > /dev/null 2>&1 & disown'

  bash /bigscoots/wpo/manage/clear_cache.sh "${DOMAIN}" op_cache > /dev/null 2>&1

  set_json_success true
  print_json_response

  ;;
  restore2alt)

  {

  if [ -z "$3" ]
  then
    bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${DOMAIN}  - There was no domain specified where to restore the backup to from the source domain."
    exit 1
  else
    ALTDOMAIN="$3"
    #echo "ALTDOMAIN set to $ALTDOMAIN"
  fi

  if [ -d /home/nginx/domains/"$ALTDOMAIN"/public ]
  then
    path=$(pwd)
    DOMAIN=$(basename "${path%/public}")
    #echo "Setting DOMAIN to $DOMAIN"
    cd /home/nginx/domains/"$ALTDOMAIN"/public
    #echo "cd into $(pwd)"

    # Get DB of gafestivaloftreesorg.bigscoots-staging.com
    if dbname=$(wpcli config get DB_NAME 2>/dev/null)
    then
      #echo "$dbname is db for $ALTDOMAIN"
      ALTDOMAINSITEURL=$(wpcli option get siteurl --path=/home/nginx/domains/"${ALTDOMAIN}"/public/ 2>/dev/null | sed -r 's/https?:\/\///g')
      #echo "$ALTDOMAINSITEURL is siteurl for $ALTDOMAIN"

      # "Restoring files..."

      #echo "We should be rsycning now"
      if ssh -i "$HOME"/.ssh/wpo_backups "$BKUSER"@"$BKSVR" "[ ! -d ~/$2/$DOMAIN/public ]"
      then
          echo "The backup location didnt exist: $BKUSER@$BKSVR /$2/$DOMAIN/public"
          exit 1
      else
          rsync -ah --exclude wp-config.php -e "ssh -i $HOME/.ssh/wpo_backups" --delete "$BKUSER"@"$BKSVR":~/"$2"/"$DOMAIN"/public/ /home/nginx/domains/"${ALTDOMAIN}"/public/
      fi
      #echo "We should done rsycning now"

      sed -i '/@include "/d' /home/nginx/domains/"${ALTDOMAIN}"/public/*.php

      #echo "Backing up the current database..."
      mysqldump "$dbname" | gzip > /home/nginx/domains/"${ALTDOMAIN}"/backup/"$dbname".sql.gz

      if dbnamesource=$(wpcli config get DB_NAME --path=/home/nginx/domains/"${DOMAIN}"/public/ 2>/dev/null)
      then
        #echo "$dbnamesource is db name of source domain ${DOMAIN}"
        if wdpprefix=$(wpcli config get table_prefix --path=/home/nginx/domains/"${DOMAIN}"/public/ --quiet 2>/dev/null)
        then
          #echo "$wdpprefix is table prefix for source ${DOMAIN}"
          wpcli config set table_prefix ${wdpprefix} --path=/home/nginx/domains/"${ALTDOMAIN}"/public/ --quiet 2>/dev/null
        else
          exit 1
          bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${DOMAIN}  -  ${ALTDOMAIN} - Failed to get the source sites database preifx"
        fi

        # echo "I am in $(pwd) about to check for the database"
        # "Dropping current database..."
        if [ -f "$dbnamesource".sql.gz ]
        then
          mysqladmin -s drop -f "$dbname"

          # "Restoring backup database..."
          mysqladmin create "$dbname"

          if [ -f "$dbnamesource".sql.gz ]
          then
              gunzip -f "$dbnamesource".sql.gz
          fi

          mysql -f --max_allowed_packet=1G "$dbname" < "$dbnamesource".sql
          rm -f "$dbnamesource".sql
        else
          bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${2}  - Backup had no database to restore."
        fi
      else
        bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${ALTDOMAIN}  - Unable to get the source domains database name so we cannot restore it to the alt domain."
        exit 1
      fi

      DOMAINSITEURL=$(wpcli option get siteurl --path=/home/nginx/domains/"${DOMAIN}"/public/ 2>/dev/null | sed -r 's/https?:\/\///g')
      # echo "$DOMAINSITEURL is the siteurl of ${DOMAIN}"
      # echo "Comparing source site: $DOMAINSITEURL with destination siteurl $ALTDOMAINSITEURL"

      if  [ "$DOMAINSITEURL" == "$ALTDOMAINSITEURL" ]
      then
        bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${ALTDOMAIN}  - Not running Search Replace failed because source site matched alt domain"
      else
        # echo "We should be running search/replace now"
        # echo "search-replace "$DOMAINSITEURL" "$ALTDOMAINSITEURL""
        wpcli search-replace "$DOMAINSITEURL" "$ALTDOMAINSITEURL" --skip-columns=guid,user_email --all-tables --path=/home/nginx/domains/"${ALTDOMAIN}"/public/
      fi

      # "Setting proper permissions..."

      bash -c 'source /bigscoots/includes/common.sh ; correct_permissions_ownership > /dev/null 2>&1 & disown'

      bash /bigscoots/wpo/manage/clear_cache.sh "${ALTDOMAIN}" op_cache > /dev/null 2>&1


      # "Restore has been completed!"

    else
      bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${2}  - Failed to get the database name via WP CLI"
      exit 1
    fi
  else
    bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${ALTDOMAIN}  - /home/nginx/domains/${ALTDOMAIN}/public doesnt exist"
  fi

      } > /dev/null 2>&1
  ;;
  enter)
  ssh -i "$HOME"/.ssh/wpo_backups -t "$BKUSER"@"$BKSVR" "echo Available Backup paths for ${DOMAIN} are listed below: ; echo ; find */${DOMAIN}/public -maxdepth 0 -type d ; echo ; echo ; echo You are now logged into the remote backup server. ; echo ; bash --login"
  
  ;;
  *)
  echo don\'t know
  ;;
  esac

else

  if [[ ! $(pwd) == *public ]]
  then

     echo "Run this script within the public directory of the WP install, quiting..."
     exit

  else

    DOMAIN=$(echo "$(pwd)"| sed "s=/home/nginx/domains/==g ; s=/public==g")

  fi
  
  if ! grep -qs '/backup ' /proc/mounts && ! grep destination=remote "$BSPATH"/backupinfo >/dev/null 2>&1 ; then
  # if ! grep -qs '/backup ' /proc/mounts ; then
     bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip} - Backup drive not mounted!"
     exit
  fi

  case $1 in
  h)

  if [ $2 = "daily" ]; then

    ls -1d /backup/*/"$DOMAIN" | sed 's/\/backup\///g' | sed "s/back-//g ; s/T/ /g ; s/_/:/g ; s/incomplete:/incomplete_/g ; s/\/$DOMAIN//g" | grep -v 'current\|manual' | sed 's/$/;/g'

  elif [ $2 = "manual" ]; then

    ls -1d /backup/*/"$DOMAIN" | sed 's/\/backup\///g' | sed "s/incomplete_back-//g ; s/back-//g ; s/T/ /g ; s/_/:/g ; s/\/$DOMAIN//g" | grep manual | sed 's/manual-//g' | sed 's/$/;/g'

  fi

  ;;
  hh)

  ls -1d /backup/*/$DOMAIN | sed 's/\/$DOMAIN//g' |grep -v 'current\|manual-\|incomplete_'

  echo
  echo "Example rsync command - You should run a backup before proceeding"
  echo "# rsync -ahv --delete /backup/current/$DOMAIN/public/ $(pwd)/"
  echo

  ;;
restore)

    init_json_response

    # Retrieve the DB name from wp-config.php
    dbname=$(wpcli config get DB_NAME 2>&1)
    if [ $? -eq 0 ]; then
        add_json_message "DB_NAME retrieved: $dbname"
    else
        add_json_error "Failed to retrieve DB_NAME: $dbname"
        send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Failed to retrieve DB_NAME. Output: $dbname"
        set_json_success false
        print_json_response
        exit 1
    fi

    # Check if the local backup directory exists
    if [ -d "/backup/$2/$DOMAIN/public" ]; then
        # Directory exists, proceed with rsync
        rsync_output=$(rsync -ah --delete /backup/"$2"/"$DOMAIN"/public/ "$(pwd)"/ 2>&1)
        if [ $? -eq 0 ]; then
            add_json_message "Files restored from local backup: /backup/$2/$DOMAIN/public"
        else
            add_json_error "Failed to restore files from local backup: $rsync_output"
            send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Failed to restore files from local backup. Output: $rsync_output"
            set_json_success false
            print_json_response
            exit 1
        fi
    else
        # Directory does not exist
        add_json_error "Backup location does not exist: /backup/$2/$DOMAIN/public"
        send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Backup location not found: /backup/$2/$DOMAIN/public"
        set_json_success false
        print_json_response
        exit 1
    fi

    # Check for malicious @include statements
    grep_output=$(grep -q '@include "' ./*.php 2>&1)
    if [ $? -eq 0 ]; then
        add_json_message "Found malicious '@include' statements in PHP files."
        
        # Remove the malicious includes
        sed_output=$(sed -i '/@include "/d' ./*.php 2>&1)
        add_json_message "Removed malicious '@include' statements from PHP files."
        send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Found and removed malicious '@include' statements. Please check the site for further issues."
    else
        add_json_message "No malicious '@include' statements found in PHP files."
    fi

    # Backup the current database
    backup_output=$(mysqldump "$dbname" | gzip > ../"$dbname".sql.gz 2>&1)
    if [ -f "../$dbname.sql.gz" ]; then
        add_json_message "Current database backed up: $dbname"
    else
        add_json_error "Failed to backup the current database: $backup_output"
        send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Database backup failed. Output: $backup_output"
        set_json_success false
        print_json_response
        exit 1
    fi

    # Drop the current database
    drop_output=$(mysqladmin -s drop -f "$dbname" 2>&1)
    if [ $? -eq 0 ]; then
        add_json_message "Dropped current database: $dbname"
    else
        add_json_error "Failed to drop current database: $drop_output"
    fi

    # Recreate the database
    create_output=$(mysqladmin create "$dbname" 2>&1)
    if [ $? -eq 0 ]; then
        add_json_message "Database recreated: $dbname"
    else
        add_json_error "Failed to recreate database: $create_output"
    fi

    # Restore from the backup file
    if [ -f "$dbname.sql.gz" ]; then
        restore_output=$(zcat "$dbname.sql.gz" | mysql -f --max_allowed_packet=1G "$dbname" 2>&1)
        if [ $? -eq 0 ]; then
            add_json_message "Database restored from compressed backup: $dbname"
        else
            add_json_error "Failed to restore database from compressed backup: $restore_output"
            set_json_success false
            print_json_response
            exit 1
        fi
    elif [ -f "$dbname.sql" ]; then
        restore_output=$(mysql -f --max_allowed_packet=1G "$dbname" < "$dbname.sql" 2>&1)
        if [ $? -eq 0 ]; then
            add_json_message "Database restored from uncompressed backup: $dbname"
        else
            add_json_error "Failed to restore database from uncompressed backup: $restore_output"
            set_json_success false
            print_json_response
            exit 1
        fi
    else
        file_list=$(ls -1)
        add_json_error "Backup file not found for database restoration: $dbname"
        send_slack_alert "#wpo-backups" ":warning: $(hostname) - ${serverip} - ${PWD} - ${2} - Backup had no database to restore. Files in directory: $file_list"
        set_json_success false
        print_json_response
        exit 1
    fi

    # "Setting proper permissions..."
    bash -c 'source /bigscoots/includes/common.sh ; correct_permissions_ownership > /dev/null 2>&1 & disown'

    bash /bigscoots/wpo/manage/clear_cache.sh "${DOMAIN}" op_cache > /dev/null 2>&1

    set_json_success true
    print_json_response

  ;;
  restore2alt)

  {

  if [ -z "$3" ]
  then
    bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${DOMAIN}  - There was no domain specified where to restore the backup to from the source domain."
    exit 1
  else
    ALTDOMAIN="$3"
  fi

  if [ -d /home/nginx/domains/"$ALTDOMAIN"/public ]
  then
    path=$(pwd)
    DOMAIN=$(basename "${path%/public}")
    cd /home/nginx/domains/"$ALTDOMAIN"/public

    if dbname=$(wpcli config get DB_NAME 2>/dev/null)
    then
      ALTDOMAINSITEURL=$(wpcli option get siteurl --path=/home/nginx/domains/"${ALTDOMAIN}"/public/ 2>/dev/null | sed -r 's/https?:\/\///g')

      # "Restoring files..."

      if [ -d "/backup/$2/$DOMAIN/public" ] 
      then
        rsync -ah --delete --exclude wp-config.php /backup/"$2"/"$DOMAIN"/public/ /home/nginx/domains/"${ALTDOMAIN}"/public/
      else
        echo "The backup location didnt exist: /backup/$2/$DOMAIN/public"
        exit 1
      fi

      sed -i '/@include "/d' /home/nginx/domains/"${ALTDOMAIN}"/public/*.php

      # "Backing up the current database..."
      mkdir -p /home/nginx/domains/"${ALTDOMAIN}"/backup
      mysqldump "$dbname" | gzip > /home/nginx/domains/"${ALTDOMAIN}"/backup/"$dbname".sql.gz

      if dbnamesource=$(wpcli config get DB_NAME --path=/home/nginx/domains/"${DOMAIN}"/public/ 2>/dev/null)
      then
        if wdpprefix=$(wpcli config get table_prefix --path=/home/nginx/domains/"${DOMAIN}"/public/ --quiet 2>/dev/null)
        then
          wpcli config set table_prefix ${wdpprefix} --path=/home/nginx/domains/"${ALTDOMAIN}"/public/ --quiet 2>/dev/null
        else
          exit 1
          bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${DOMAIN}  -  ${ALTDOMAIN} - Failed to get the source sites database preifx"
        fi

        # "Dropping current database..."
        if [ -f "$dbnamesource".sql.gz ]
        then
          mysqladmin -s drop -f "$dbname"

          # "Restoring backup database..."
          mysqladmin create "$dbname"

          if [ -f "$dbnamesource".sql.gz ]
          then
            gunzip -f "$dbnamesource".sql.gz
          fi

          mysql -f --max_allowed_packet=1G "$dbname" < "$dbnamesource".sql
          rm -f "$dbnamesource".sql
        else
          bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${2}  - Backup had no database to restore."
        fi
      else
        bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${ALTDOMAIN}  - Unable to get the source domains database name so we cannot restore it to the alt domain."
        exit 1
      fi

      DOMAINSITEURL=$(wpcli option get siteurl --path=/home/nginx/domains/"${ALTDOMAIN}"/public/ 2>/dev/null | sed -r 's/https?:\/\///g')

      if  [ "$DOMAINSITEURL" == "$ALTDOMAINSITEURL" ]
      then
        bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${ALTDOMAIN}  - Not running Search Replace failed because source site matched alt domain"
      else
        wpcli search-replace "$DOMAINSITEURL" "$ALTDOMAINSITEURL" --skip-columns=guid,user_email --all-tables --path=/home/nginx/domains/"${ALTDOMAIN}"/public/ --quiet 2>/dev/null
      fi

      # "Setting proper permissions..."

      bash -c 'source /bigscoots/includes/common.sh ; correct_permissions_ownership > /dev/null 2>&1 & disown'

      bash /bigscoots/wpo/manage/clear_cache.sh "${ALTDOMAIN}" op_cache > /dev/null 2>&1


      # "Restore has been completed!"

    else
      bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${2}  - Failed to get the database name via WP CLI"
      exit 1
    fi
  else
    bash /bigscoots/general/slack.sh "#wpo-backups" ":warning: $(hostname) -  ${serverip}  -  ${PWD}  -  ${ALTDOMAIN}  - /home/nginx/domains/${ALTDOMAIN}/public doesnt exist"
  fi

      }
  ;;
  enter)
  echo "The backups are available locally in /backup so just use /bigscoots/wpo_backups_ovz_restore.sh hh"
  
  ;;
  *)
  echo don\'t know
  ;;
  esac

fi