# # #this is deploysql.sh###
#!/bin/bash
Lang= "en_US. UTF-8 "
Date= ' date + '%y%m%d_%h%m%s '
User=root
Password=yourpassword
cd/root/sqltest/
SVN up
If [$? = 0];then
get_env= ' cat/root/sqltest/upgrade.sql |grep ^ #ENV =|sed ' s/#ENV =//g;s/.$//'
Get_host= ' cat/root/sqltest/upgrade.sql |grep ^ #HOST =|sed ' s/#HOST =//g;s/.$//'
Host=${get_host}
If [-N "${get_env"}] && [-N "${get_host}"];then
Echo-e "Getting ${get_env} Ip:${get_host} The table that needs to be backed up ... \ n"
Backup_tables= ' cat/root/sqltest/upgrade.sql |grep ^ #backup_tables =|sed ' s/#backup_tables =//g;s/.$//'
Old_ifs= "$IFS"
Ifs= ","
tables= ($backup _tables)
ifs= "$OLD _ifs"
If [-N "$tables"];then
ECHO-E "The tables you need to back up are: ${tables[*]}\n"
For table in ${tables[@]}
Do
Echo-e "mysqldump-uroot-pdbpasswd ${table%.*} ${table#*.} > ${table%.*}_${table#*.} _${date}.sql\n "
Done
If [$?-eq 0]
Then
ECHO-E "Backup succeeded! \ n "
ECHO-E "Performing upgrade sql...\n"
Mysql-u$user-p$password-h $host <<eof
Source/root/sqltest/upgrade.sql;
Eof
Else
ECHO-E "Backup failed! \ n "
Fi
Else
ECHO-E "WARNING: You did not enter any tables that need to be backed up!" but the upgrade SQL still executes ... \ n "
ECHO-E "Performing upgrade sql...\n"
Mysql-u$user-p$password-h $host <<eof
Source/root/sqltest/upgrade.sql;
Eof
Fi
Else
Echo-e "ENV or HOST not found!\n"
Fi
Else
ECHO-E "SVN update failed!\n"
Fi
#this is testenv.sql#
############################### #声明主机 (Operation maintenance) ##############################################
#ENV = quasi-production environment
#HOST =192.168.1.1.
############################### #END ###################################################
################################ #声明需要备份的表, it is recommended to make a backup of the table that has the operation upgraded, can be empty, that is, do not back up the direct upgrade (development maintenance) ################################## ####
#请在下一行 "backup_tables=" after entering the table to be backed up, format: db.table multiple with "," delimited
#backup_tables =mysql.user,mysql.host
################################# #END ##################################################
############################### #提供升级操作SQL (development and maintenance) ############################################
Use MySQL;
Show tables;
############################### #END #####################################################
Jenkins calls the shell to perform database upgrades and backups