要什么变量替换,直接复制粘贴,程序跑起来就走。


#!/bin/bash

# 将hellodb的数据拷贝到hellodb_bak库里面。


mysql -uroot -pAbcd@1234 -h 10.0.20.26 -P3306 2>/dev/null -e "flush tables with read lock;"


mysql -uroot -pAbcd@1234 -h 10.0.20.26 -P3306 2>/dev/null -e "create database hellodb_bak;"


tbname=$(mysql -uroot -pAbcd@1234 -h 10.0.20.26 -P3306 2>/dev/null -e "SELECT table_name from information_schema.tables where TABLE_SCHEMA='hellodb'")


for i in $tbname; do

    mysql -uroot -pAbcd@1234 -h 10.0.20.26 -P3306 hellodb_bak 2>/dev/null -e "create table $i like hellodb.$i"

    mysql -uroot -pAbcd@1234 -h 10.0.20.26 -P3306 hellodb_bak 2>/dev/null -e "insert into $i select * from hellodb.$i"

done


mysql -uroot -pAbcd@1234 -h 10.0.20.26 -P3306 2>/dev/null -e "unlock tables;"