yes-no 发表于 2018-8-29 07:32:03

shell自动创建mysql数据表

  线上用的是阿里云RDS数据库
  某些应用的日志信息需要写入到数据表里面,表需要提前创建好,否则数据就丢失了。
  总共需要创建4张表
  shell脚本如下:
  #!/bin/bash
  base="xxa.rds.aliyuncs.com"
  express="xxb.mysql.rds.aliyuncs.com"
  log="xxc.rds.aliyuncs.com"
  #上一个月
  before_mon=`date -d "-1 month" +"%m"`
  #下一个月
  next_mon=`date -d "+1 month" +"%m"`
  #上一个月,去掉月前面的0
  before_mv_mon=`date -d "-1 month" +"%-m"`
  #下一个月,去掉月前面的0
  next_mv_mon=`date -d "+1 month" +"%-m"`
  #前一个月的年份
  year=`date +%Y`
  #下一个月的年份
  year_next=`date -d "+1 month" +"%Y"`
  /usr/local/mysql/bin/mysql -u xxuser -pxxpass -h $base -e "use kd_shop;CREATE TABLE tbl_scan_to_e3_bak_"$year_next"_$next_mv_mon like tbl_scan_to_e3_bak_"$year"_$before_mv_mon;"
  sleep 1
  /usr/local/mysql/bin/mysql -u xxuser -pxxpass -h $base -e "use kd_shop;CREATE TABLE tbl_scan_to_qf_bak_"$year_next"_$next_mv_mon like tbl_scan_to_qf_bak_"$year"_$before_mv_mon;"
  sleep 1
  /usr/local/mysql/bin/mysql -u xxuser -pxxpass -h $base -e "use sms;CREATE TABLE tbl_sms_history_"$year_next"$next_mon like tbl_sms_history_"$year"$before_mon;"
  sleep 1
  /usr/local/mysql/bin/mysql -u xxuser -pxxpass -h $log -e "use dts;CREATE TABLE dts_message_history_"$year_next"$next_mon like dts_message_history_"$year"$before_mon;"
  设置权限
  chmod 755 create_tables.sh
  设置任务计划,每天跑一遍,防止未创建
  01 23 * * * /opt/create_tables.sh

页: [1]
查看完整版本: shell自动创建mysql数据表