|
|
@ -1,13 +1,20 @@ |
|
|
|
package main |
|
|
|
|
|
|
|
/* |
|
|
|
思路: 脚本按照顺序拼接,旧文件按照 utf8 格式清空 |
|
|
|
思路: |
|
|
|
实现 a (a a0101,b,c,d a0184) 现在只针对 a0184 生效,并且可以实现导出照片信息 |
|
|
|
|
|
|
|
*/ |
|
|
|
import ( |
|
|
|
"archive/zip" |
|
|
|
"database/sql" |
|
|
|
"golang.org/x/sys/windows/registry" |
|
|
|
"io" |
|
|
|
"log" |
|
|
|
"os" |
|
|
|
"os/exec" |
|
|
|
"path/filepath" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql" |
|
|
@ -15,25 +22,119 @@ import ( |
|
|
|
|
|
|
|
//存在所有对象信息 - 集合
|
|
|
|
func main() { |
|
|
|
sqlPath := "D:/hy_qggwy_kk.sql" |
|
|
|
|
|
|
|
db, _ := sql.Open("mysql", "root:admin@tcp(127.0.0.1:35017)/hy_qggwy?charset=utf8") |
|
|
|
log.Print("操作数据库连接:", db) |
|
|
|
|
|
|
|
QuerySQL(db, "select * from a01 where a0184 in (select d from kk)", "a01", []string{"A0155", "JSNLSJ", "TBSJ", "XGSJ"}) |
|
|
|
QuerySQL(db, "select * from a02 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a02", []string{}) |
|
|
|
QuerySQL(db, "select * from a05 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a05", []string{}) |
|
|
|
QuerySQL(db, "select * from a06 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a06", []string{}) |
|
|
|
QuerySQL(db, "select * from a08 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a08", []string{}) |
|
|
|
QuerySQL(db, "select * from a14 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a14", []string{}) |
|
|
|
QuerySQL(db, "select * from a15 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a15", []string{}) |
|
|
|
QuerySQL(db, "select * from a36 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a36", []string{}) |
|
|
|
QuerySQL(db, "select * from a99z1 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a99z1", []string{}) |
|
|
|
QuerySQL(db, "select * from a30 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a30", []string{}) |
|
|
|
QuerySQL(db, "select * from a33 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a33", []string{}) |
|
|
|
QuerySQL(db, "select * from a57 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a57", []string{}) |
|
|
|
// 创建临时文件夹
|
|
|
|
dir := time.Now().Format("2006102150405") |
|
|
|
if err := os.Mkdir("./"+dir, os.ModePerm); err != nil { |
|
|
|
log.Fatal("创建目录时发生异常:", db) |
|
|
|
} |
|
|
|
|
|
|
|
// 获取MySQL的安装目录
|
|
|
|
// getMySQLPathByRegistry()
|
|
|
|
mysqlPath := getMySQLPath(db) |
|
|
|
|
|
|
|
// 插入预置脚本
|
|
|
|
runSQL(mysqlPath, sqlPath) |
|
|
|
|
|
|
|
QuerySQL(db, dir, "select * from a01 where a0184 in (select d from kk)", "a01", []string{"A0155", "JSNLSJ", "TBSJ", "XGSJ"}) |
|
|
|
QuerySQL(db, dir, "select * from a02 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a02", []string{}) |
|
|
|
QuerySQL(db, dir, "select * from a05 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a05", []string{}) |
|
|
|
QuerySQL(db, dir, "select * from a06 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a06", []string{}) |
|
|
|
QuerySQL(db, dir, "select * from a08 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a08", []string{}) |
|
|
|
QuerySQL(db, dir, "select * from a14 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a14", []string{}) |
|
|
|
QuerySQL(db, dir, "select * from a15 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a15", []string{}) |
|
|
|
QuerySQL(db, dir, "select * from a36 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a36", []string{}) |
|
|
|
QuerySQL(db, dir, "select * from a99z1 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a99z1", []string{}) |
|
|
|
QuerySQL(db, dir, "select * from a30 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a30", []string{}) |
|
|
|
QuerySQL(db, dir, "select * from a33 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a33", []string{}) |
|
|
|
QuerySQL(db, dir, "select * from a57 where a0000 in (select a0000 from a01 where a0184 in (select d from kk))", "a57", []string{}) |
|
|
|
|
|
|
|
// 处理照片信息
|
|
|
|
copyFileByA57(db, dir, mysqlPath) |
|
|
|
|
|
|
|
// 进行压缩操作 并 删除压缩前目录
|
|
|
|
Zip("./"+dir, dir+".gz") |
|
|
|
if err := os.RemoveAll("./" + dir); err != nil { |
|
|
|
log.Fatal("删除历史目录时发生异常:", db) |
|
|
|
} |
|
|
|
log.Println("执行结束!") |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
copyFileByA57 复制需要备份的照片文件 |
|
|
|
*/ |
|
|
|
func copyFileByA57(db *sql.DB, dir, mysqlPath string) { |
|
|
|
dirNew := strings.Replace(mysqlPath, "mysql/bin/", "", -1) |
|
|
|
codeValues, _ := db.Query("select concat('tomcat8/HZBPHOTOS/', PHOTOPATH) filePath, PHOTONAME fileName from " + |
|
|
|
"a57 where a0000 in (select a0000 from a01 where a0184 in (select d from kk));") |
|
|
|
//返回所有列
|
|
|
|
filePath, fileName := "", "" |
|
|
|
for codeValues.Next() { |
|
|
|
codeValues.Scan(&filePath, &fileName) |
|
|
|
if filePath != "" { |
|
|
|
from := dirNew + filePath + fileName |
|
|
|
to := "./" + dir + "/" + filePath |
|
|
|
_ = os.MkdirAll(to, os.ModePerm) |
|
|
|
if _, err := CopyFile(from, to+fileName); err != nil { |
|
|
|
log.Println("发现存在照片无法复制:", from, to+fileName) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
func CopyFile(src, des string) (w int64, err error) { |
|
|
|
srcFile, _ := os.Open(src) |
|
|
|
defer srcFile.Close() |
|
|
|
desFile, _ := os.Create(des) |
|
|
|
defer desFile.Close() |
|
|
|
|
|
|
|
return io.Copy(desFile, srcFile) |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
getMySQLPath 使用注册表获取MySQL安装路径 |
|
|
|
*/ |
|
|
|
func getMySQLPathByRegistry() string { |
|
|
|
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `计算机\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\GWY20_Mysql`, registry.ALL_ACCESS) |
|
|
|
if err != nil { |
|
|
|
log.Fatal("读取注册表信息时异常:", err) |
|
|
|
} |
|
|
|
defer k.Close() |
|
|
|
s, _, err := k.GetStringValue("ImagePath/**/") |
|
|
|
if err != nil { |
|
|
|
log.Fatal("获取注册表关于MySQL服务的地址异常:", err) |
|
|
|
} |
|
|
|
log.Fatal("获取注册表关于MySQL服务的地址:", s) |
|
|
|
return s |
|
|
|
} |
|
|
|
func getMySQLPath(db *sql.DB) string { |
|
|
|
codeValues, _ := db.Query("select @@datadir as dataPath from dual;") |
|
|
|
//返回所有列
|
|
|
|
dataPath := "" |
|
|
|
for codeValues.Next() { |
|
|
|
codeValues.Scan(&dataPath) |
|
|
|
} |
|
|
|
if dataPath == "" { |
|
|
|
log.Fatal("获取MySQL的地址异常") |
|
|
|
} |
|
|
|
dataPath = strings.Replace(dataPath, "\\", "/", -1) |
|
|
|
return strings.Replace(dataPath, "data", "bin", -1) |
|
|
|
} |
|
|
|
|
|
|
|
func runSQL(mysqlPath, sqlPath string) { |
|
|
|
log.Print("调用执行对应的SQL脚本:", sqlPath) |
|
|
|
c := exec.Command("cmd", "/C", mysqlPath+"mysql -uroot -padmin hy_qggwy < "+sqlPath) |
|
|
|
if err := c.Run(); err != nil { |
|
|
|
log.Fatal("执行时发生异常!异常为:", err) |
|
|
|
} |
|
|
|
log.Print("预置脚本数据成功!") |
|
|
|
} |
|
|
|
|
|
|
|
// 获取表数据
|
|
|
|
func QuerySQL(db *sql.DB, sqlStr, tableName string, colNil []string) { |
|
|
|
func QuerySQL(db *sql.DB, dir, sqlStr, tableName string, colNil []string) { |
|
|
|
rows, err := db.Query(sqlStr) |
|
|
|
log.Print("开始备份数据:", tableName, ";处理数据:", rows, ";异常情况:", err) |
|
|
|
defer rows.Close() |
|
|
@ -90,7 +191,7 @@ func QuerySQL(db *sql.DB, sqlStr, tableName string, colNil []string) { |
|
|
|
aa = aa + key + " values " + value |
|
|
|
} |
|
|
|
var path string |
|
|
|
path = "./" + tableName + "_" + time.Now().Format("2006102150405") + ".sql" |
|
|
|
path = "./" + dir + "/" + tableName + "_" + time.Now().Format("2006102150405") + ".sql" |
|
|
|
file3, _ := os.Create(path) |
|
|
|
file3.WriteString(aa) |
|
|
|
file3.Close() |
|
|
@ -104,3 +205,91 @@ func inStr(colNil []string, kk string) bool { |
|
|
|
} |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
// 打包成zip文件
|
|
|
|
func Zip(src_dir string, zip_file_name string) { |
|
|
|
|
|
|
|
// 预防:旧文件无法覆盖
|
|
|
|
os.RemoveAll(zip_file_name) |
|
|
|
|
|
|
|
// 创建:zip文件
|
|
|
|
zipfile, _ := os.Create(zip_file_name) |
|
|
|
defer zipfile.Close() |
|
|
|
|
|
|
|
// 打开:zip文件
|
|
|
|
archive := zip.NewWriter(zipfile) |
|
|
|
defer archive.Close() |
|
|
|
|
|
|
|
// 遍历路径信息
|
|
|
|
filepath.Walk(src_dir, func(path string, info os.FileInfo, _ error) error { |
|
|
|
|
|
|
|
// 如果是源路径,提前进行下一个遍历
|
|
|
|
if path == src_dir { |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// 获取:文件头信息
|
|
|
|
header, _ := zip.FileInfoHeader(info) |
|
|
|
header.Name = strings.TrimPrefix(path, src_dir+`/`) |
|
|
|
|
|
|
|
// 判断:文件是不是文件夹
|
|
|
|
if info.IsDir() { |
|
|
|
header.Name += `/` |
|
|
|
} else { |
|
|
|
// 设置:zip的文件压缩算法
|
|
|
|
header.Method = zip.Deflate |
|
|
|
} |
|
|
|
|
|
|
|
// 创建:压缩包头部信息
|
|
|
|
writer, _ := archive.CreateHeader(header) |
|
|
|
if !info.IsDir() { |
|
|
|
file, _ := os.Open(path) |
|
|
|
defer file.Close() |
|
|
|
io.Copy(writer, file) |
|
|
|
} |
|
|
|
return nil |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// 打包成zip文件
|
|
|
|
func UnZip(src_file string) { |
|
|
|
//打开并读取压缩文件中的内容
|
|
|
|
fr, err := zip.OpenReader(src_file) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
defer fr.Close() |
|
|
|
//r.reader.file 是一个集合,里面包括了压缩包里面的所有文件
|
|
|
|
for _, file := range fr.Reader.File { |
|
|
|
//判断文件该目录文件是否为文件夹
|
|
|
|
if file.FileInfo().IsDir() { |
|
|
|
err := os.MkdirAll(file.Name, 0644) |
|
|
|
if err != nil { |
|
|
|
log.Println("判断文件该目录文件是否为文件夹:", err) |
|
|
|
} |
|
|
|
continue |
|
|
|
} |
|
|
|
//为文件时,打开文件
|
|
|
|
r, err := file.Open() |
|
|
|
|
|
|
|
//文件为空的时候,打印错误
|
|
|
|
if err != nil { |
|
|
|
log.Println("文件为空的时候,打印错误") |
|
|
|
continue |
|
|
|
} |
|
|
|
//这里在控制台输出文件的文件名及路径
|
|
|
|
log.Println("进行解压文件:", file.Name) |
|
|
|
|
|
|
|
//在对应的目录中创建相同的文件
|
|
|
|
NewFile, err := os.Create(file.Name) |
|
|
|
if err != nil { |
|
|
|
log.Println("在对应的目录中创建相同的文件:", err) |
|
|
|
continue |
|
|
|
} |
|
|
|
//将内容复制
|
|
|
|
io.Copy(NewFile, r) |
|
|
|
//关闭文件
|
|
|
|
NewFile.Close() |
|
|
|
r.Close() |
|
|
|
} |
|
|
|
} |
|
|
|