mysql切表原理:
1、创建临时表
2、修改源table名称为tablename_$date
3、修改临时表名称为源table名称
#!/bin/sh
host=$1
port=$2
host=${host:="localhost"} #host没赋值,那么就赋值为localhost
port=${port:="3306"}
#schema是数据库名字
schema=$3
echo $host, $port, $schema
#sleep 15
echo "=====bre2.0 archive tables ...."
date "+%F %T"
dater=`date -d "-1 day" "+%Y%m%d"`
exe_sql="create table Mapping_event_tmp like Mapping_event; rename table Mapping_event to Mapping_event_$dater, Mapping_event_tmp to Mapping_event;"
#如果只传了schema进来, 则只修改该schema下的所有表
if [ "$schema" ]
then
echo "===="$schema
echo $exe_sql
echo $exe_sql | mysql -h $host -P $port -uusername -ppassword $schema
#如果schema没传则修改该服务器下面的所有表
else
mysql -h $host -P $port -uusername -ppassword -e "show databases like 'DMP_GDMP%';" -N | while read schema
do
echo "===="$schema
echo $exe_sql
echo $exe_sql | mysql -h $host -P $port -uusername -ppassword $schema
done
fi
date "+%F %T"
host=$1
port=$2
host=${host:="localhost"} #host没赋值,那么就赋值为localhost
port=${port:="3306"}
#schema是数据库名字
schema=$3
echo $host, $port, $schema
#sleep 15
echo "=====bre2.0 archive tables ...."
date "+%F %T"
dater=`date -d "-1 day" "+%Y%m%d"`
exe_sql="create table Mapping_event_tmp like Mapping_event; rename table Mapping_event to Mapping_event_$dater, Mapping_event_tmp to Mapping_event;"
#如果只传了schema进来, 则只修改该schema下的所有表
if [ "$schema" ]
then
echo "===="$schema
echo $exe_sql
echo $exe_sql | mysql -h $host -P $port -uusername -ppassword $schema
#如果schema没传则修改该服务器下面的所有表
else
mysql -h $host -P $port -uusername -ppassword -e "show databases like 'DMP_GDMP%';" -N | while read schema
do
echo "===="$schema
echo $exe_sql
echo $exe_sql | mysql -h $host -P $port -uusername -ppassword $schema
done
fi
date "+%F %T"
转载:http://blog.csdn.net/crazyhacking/article/details/12649797