#!/bin/bash

# 数据表备份

export mysql_exec_file_prefix=/data/lingyun/mysql-8.0.38-linux-glibc2.12-x86_64/bin
export mysql_port=33663
export SQL_DUMP_FILE=/root/wdd/all_tables_5.7.0.sql
export SQL_FULL_BACK_UP_FILE=/root/wdd/all_tables_5.7.0_20241210_fullback.sql
export SQL_DDL_FILE="/mnt/d/NextCloudData/MasterLoad/Work/UAV Cloud/22.希腊项目-阿里云-德国/999-数据库脚本/5.7.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!"

  $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 ""

}

backup_all_structure
backup_all_dump

