#!/bin/bash

# 数据表备份

export mysql_exec_file_prefix=/root/mysql-8.0.27-linux-glibc2.17-x86_64-minimal/bin
export mysql_port=33306
export SQL_DUMP_FILE=/root/update_6.0.0/all_tables_5.7.0.sql
export SQL_FULL_BACK_UP_FILE=/root/update_6.0.0/all_tables_5.7.0_20240919_fullback.sql
export SQL_DDL_FILE="/root/update_6.0.0/厦门5.7.0~6.0.0"

backup_all_structure() {
  echo ""
  echo ""
  echo "start to backup all-table-structure!"
  $mysql_exec_file_prefix/mysql -uroot -pQzfXQhd3bQ -h127.0.0.1 -P$mysql_port -e 'show databases;' | grep -Ev 'Database|information_schema|mysql|sys|performance_schema' | xargs $mysql_exec_file_prefix/mysqldump -uroot -pQzfXQhd3bQ -h127.0.0.1 -P$mysql_port --single-transaction --source-data=2 --hex-blob --triggers --routines --events --no-data --set-gtid-purged=OFF --databases >"${SQL_DUMP_FILE}"
  echo ""
}

backup_all_dump() {
  echo ""
  echo ""
  echo ""
  echo "start to backup all-table-database-data!"
  export SQL_FULL_BACK_UP_FILE=/root/wdd/all_tables_5.4.0_20240904_fullback.sql
  $mysql_exec_file_prefix/mysql -uroot -pQzfXQhd3bQ -h127.0.0.1 -P$mysql_port -e 'show databases;' | grep -Ev 'Database|information_schema|mysql|sys|performance_schema' | xargs $mysql_exec_file_prefix/mysqldump -uroot -pQzfXQhd3bQ -h127.0.0.1 -P$mysql_port --single-transaction --source-data=2 --hex-blob --triggers --routines --events --set-gtid-purged=OFF --databases >"${SQL_FULL_BACK_UP_FILE}"
  echo ""
}

import_ddl_sql(){
  echo ""
  echo ""
  echo "start to import ddl sql !"

  for sql_file in $(ls "$SQL_DDL_FILE" | sort -n -k1.1,1.2); do
      echo "current file is ${sql_file}"

      sudo $mysql_exec_file_prefix/mysql -uroot -pQzfXQhd3bQ -h127.0.0.1 -P$mysql_port <"$SQL_DDL_FILE/${sql_file}"
      echo "------------------"
      echo ""
  done

  echo ""

}

import_ddl_sql

/root/mysql-8.0.27-linux-glibc2.17-x86_64-minimal/bin/mysql -uroot -pQzfXQhd3bQ -h127.0.0.1 -P33306 < ccc.sql