Browse Source

实现基本的备份功能

master
VIVIMAN 3 years ago
parent
commit
04c622d62e
  1. 204
      golang_learn/data_func/go_to_windows/main.go
  2. 59
      golang_learn/data_func/go_to_zip/demo4/main.go
  3. 2
      golang_learn/go.mod
  4. 6
      golang_learn/go.sum

204
golang_learn/data_func/go_to_windows/main.go

@ -4,12 +4,14 @@
package main
import (
"archive/zip"
"bufio"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/lxn/win"
"github.com/mzky/zip"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"io"
@ -23,6 +25,7 @@ import (
)
var version = "V2021 1.0.1"
var passwordname = "Qw^e1@10&++Hzb...?8"
func (mw *myApp) aboutactionTriggered() {
walk.MsgBox(mw.mw, "功能说明", strings.Join([]string{
@ -174,11 +177,11 @@ func getWindows() error {
Text: "关于",
OnTriggered: app.aboutactionTriggered,
},
Action{
/*Action{
// Image: "./image//yaoqiu.png",
Text: "要求",
OnTriggered: app.aboutactionTriggered,
},
},*/
},
},
},
@ -601,11 +604,46 @@ func setServerSize() {
// dbBak 数据备份功能
func dbBak() {
showMsg("【待完善】数据备份!")
db, _ := sql.Open("mysql", "root:admin@tcp(127.0.0.1:35017)/hy_qggwy?charset=utf8")
showMsg("连接数据库成功!")
// 创建临时文件夹
dir := time.Now().Format("2006102150405")
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
showMsg("创建临时目录时发生异常!")
}
QuerySQL(db, dir, "select * from a01 where STATUS=1", "a01", []string{"A0155", "JSNLSJ", "TBSJ", "XGSJ"})
QuerySQL(db, dir, "select * from a02 where a0000 in (select a0000 from a01 where status=1)", "a02", []string{})
QuerySQL(db, dir, "select * from a05 where a0000 in (select a0000 from a01 where status=1)", "a05", []string{})
QuerySQL(db, dir, "select * from a06 where a0000 in (select a0000 from a01 where status=1)", "a06", []string{})
QuerySQL(db, dir, "select * from a08 where a0000 in (select a0000 from a01 where status=1)", "a08", []string{})
QuerySQL(db, dir, "select * from a14 where a0000 in (select a0000 from a01 where status=1)", "a14", []string{})
QuerySQL(db, dir, "select * from a15 where a0000 in (select a0000 from a01 where status=1)", "a15", []string{})
QuerySQL(db, dir, "select * from a36 where a0000 in (select a0000 from a01 where status=1)", "a36", []string{})
QuerySQL(db, dir, "select * from a99z1 where a0000 in (select a0000 from a01 where status=1)", "a99z1", []string{})
QuerySQL(db, dir, "select * from a30 where a0000 in (select a0000 from a01 where status=1)", "a30", []string{})
QuerySQL(db, dir, "select * from a33 where a0000 in (select a0000 from a01 where status=1)", "a33", []string{})
QuerySQL(db, dir, "select * from a57 where a0000 in (select a0000 from a01 where status=1)", "a57", []string{})
// 处理照片信息
copyFileByA57(db, dir)
dirName := fmt.Sprintf("./%s", dir)
fileName := fmt.Sprintf("%s.gz", dir)
// 进行压缩操作 并 删除压缩前目录
Zip(dirName, fileName)
if err := os.RemoveAll(dir); err != nil {
showMsg("删除历史目录时发生异常!")
}
showMsg("已完成数据备份!")
showMsg(fmt.Sprintf(`⭐数据备份:%s`, fileName))
}
// reDbBak 备份数据还原
func reDbBak() {
// 执行还原操作
// runSQL("mysqlPath", "sqlPath")
showMsg("【待完善】备份数据还原!")
}
@ -767,3 +805,161 @@ func (mw *myApp) RunAppSetingDialog(owner walk.Form, confirmCall func(*AppSeting
},
}.Run(owner)
}
// runSQL 执行sql脚本
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.Print("【不影响执行】执行时发生异常!异常为:", err)
}
log.Print("预置脚本数据成功!")
}
// QuerySQL 获取表数据
func QuerySQL(db *sql.DB, dir, sqlStr, tableName string, colNil []string) {
rows, _ := db.Query(sqlStr)
showMsg(fmt.Sprintf(`开始备份数据:%s;`, tableName))
defer rows.Close()
cloumns, _ := rows.Columns()
values := make([]sql.RawBytes, len(cloumns))
scanArgs := make([]interface{}, len(values))
for i := range values {
scanArgs[i] = &values[i]
}
aa := ""
for rows.Next() {
_ = rows.Scan(scanArgs...)
key, value := "(", "("
aa = aa + "insert into " + tableName + " "
for i, col := range values {
kk := cloumns[i]
if inStr(colNil, kk) {
continue
}
if i == len(values)-1 {
key = key + kk
if value == "(" {
if col == nil {
value = value + "NULL"
} else {
value = value + "'" + string(col) + "'"
}
} else {
if col == nil {
value = value + ",NULL"
} else {
value = value + ",'" + string(col) + "'"
}
}
} else {
key = key + kk + ","
if value == "(" {
if col == nil {
value = value + "NULL"
} else {
value = value + "'" + string(col) + "'"
}
} else {
if col == nil {
value = value + ",NULL"
} else {
value = value + ",'" + string(col) + "'"
}
}
}
}
key = key + ")"
value = value + ");\n"
aa = aa + key + " values " + value
}
var path string
path = fmt.Sprintf("%s/%s.sql", dir, tableName)
file3, _ := os.Create(path)
_, _ = file3.WriteString(aa)
file3.Close()
}
// inStr 判断字段关系
func inStr(colNil []string, kk string) bool {
for _, s := range colNil {
if s == kk {
return true
}
}
return false
}
// copyFileByA57 复制需要备份的照片文件
func copyFileByA57(db *sql.DB, mysqlPath string) {
codeValues, _ := db.Query("select concat('tomcat8/HZBPHOTOS/', PHOTOPATH) filePath, PHOTONAME fileName from " +
"a57 where a0000 in (select a0000 from a01 where status=1);")
//返回所有列
filePath, fileName := "", ""
for codeValues.Next() {
_ = codeValues.Scan(&filePath, &fileName)
if filePath != "" {
from := path + "/" + filePath + fileName
to := mysqlPath + "/" + filePath
_ = os.MkdirAll(to, os.ModePerm)
if _, err := CopyFile(from, to+fileName); err != nil {
showMsg(fmt.Sprintf(`发现存在照片无法复制:%s`, filePath+fileName))
}
}
}
}
// CopyFile 复制文件操作
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)
}
// Zip 打包成zip文件
func Zip(srcDir string, zipFileName string) {
// 预防:旧文件无法覆盖
_ = os.RemoveAll(zipFileName)
// 创建:zip文件
zipfile, _ := os.Create(zipFileName)
defer zipfile.Close()
// 打开:zip文件
archive := zip.NewWriter(zipfile)
defer archive.Close()
// 遍历路径信息
_ = filepath.Walk(srcDir, func(path string, info os.FileInfo, _ error) error {
// 如果是源路径,提前进行下一个遍历
if path == srcDir {
return nil
}
// 获取:文件头信息
header, _ := zip.FileInfoHeader(info)
header.Name = strings.TrimPrefix(path, srcDir+`/`)
// 判断:文件是不是文件夹
if info.IsDir() {
header.Name += `/`
} else {
// 设置:zip的文件压缩算法
header.Method = zip.Deflate
}
// 创建:压缩包头部信息
writer, _ := archive.Encrypt(header.Name, passwordname, zip.AES256Encryption)
if !info.IsDir() {
file, _ := os.Open(path)
defer file.Close()
_, _ = io.Copy(writer, file)
}
return nil
})
}

59
golang_learn/data_func/go_to_zip/demo4/main.go

@ -0,0 +1,59 @@
package main
import (
"github.com/mzky/zip"
"io"
"os"
"path/filepath"
"strings"
)
func main() {
Zip("./", "aaa.gz")
}
// Zip 打包成zip文件
func Zip(srcDir string, zipFileName string) {
// 预防:旧文件无法覆盖
_ = os.RemoveAll(zipFileName)
// 创建:zip文件
zipfile, _ := os.Create(zipFileName)
defer zipfile.Close()
// 打开:zip文件
archive := zip.NewWriter(zipfile)
defer archive.Close()
// 遍历路径信息
_ = filepath.Walk(srcDir, func(path string, info os.FileInfo, _ error) error {
// 如果是源路径,提前进行下一个遍历
if path == srcDir {
return nil
}
// 获取:文件头信息
header, _ := zip.FileInfoHeader(info)
header.Name = strings.TrimPrefix(path, srcDir+`/`)
// 判断:文件是不是文件夹
if info.IsDir() {
header.Name += `/`
} else {
// 设置:zip的文件压缩算法
header.Method = zip.Deflate
}
// 创建:压缩包头部信息
// writer, _ := archive.CreateHeader(header)
writer, _ := archive.Encrypt(header.Name, "1234", zip.AES256Encryption)
if !info.IsDir() {
file, _ := os.Open(path)
defer file.Close()
_, _ = io.Copy(writer, file)
}
return nil
})
}

2
golang_learn/go.mod

@ -17,13 +17,13 @@ require (
github.com/google/martian v2.1.0+incompatible
github.com/google/uuid v1.1.2
github.com/joho/godotenv v1.3.0
github.com/josephspurrier/goversioninfo v1.4.0 // indirect
github.com/kardianos/service v1.2.0
github.com/lxn/walk v0.0.0-20210112085537-c389da54e794
github.com/lxn/win v0.0.0-20210218163916-a377121e959e
github.com/mattn/go-adodb v0.0.1
github.com/mattn/go-oci8 v0.1.1
github.com/mattn/go-sqlite3 v1.14.8
github.com/mzky/zip v0.0.0-20210817031838-1b692015aed2
github.com/russross/blackfriday v1.6.0
github.com/shirou/gopsutil v2.20.4+incompatible
github.com/spf13/viper v1.8.1

6
golang_learn/go.sum

@ -47,8 +47,6 @@ github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9/go.mod
github.com/PuerkitoBio/goquery v1.7.1 h1:oE+T06D+1T7LNrn91B4aERsRIeCLJ/oPSa6xB9FPnz4=
github.com/PuerkitoBio/goquery v1.7.1/go.mod h1:XY0pP4kfraEmmV1O7Uf6XyjoslwsneBbgeDjLYuN8xY=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/akavel/rsrc v0.10.2 h1:Zxm8V5eI1hW4gGaYsJQUhxpjkENuG91ki8B4zCrvEsw=
github.com/akavel/rsrc v0.10.2/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/andlabs/ui v0.0.0-20200610043537-70a69d6ae31e h1:wSQCJiig/QkoUnpvelSPbLiZNWvh2yMqQTQvIQqSUkU=
github.com/andlabs/ui v0.0.0-20200610043537-70a69d6ae31e/go.mod h1:5G2EjwzgZUPnnReoKvPWVneT8APYbyKkihDVAHUi0II=
github.com/andybalholm/cascadia v1.2.0 h1:vuRCkM5Ozh/BfmsaTm26kbjm0mIOM3yS5Ek/F5h18aE=
@ -205,8 +203,6 @@ github.com/jackmordaunt/icns v0.0.0-20181231085925-4f16af745526/go.mod h1:UQkeMH
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca/go.mod h1:eJTEwMjXb7kZ633hO3Ln9mBUCOjX2+FlTljvpl9SYdE=
github.com/josephspurrier/goversioninfo v1.4.0 h1:Puhl12NSHUSALHSuzYwPYQkqa2E1+7SrtAPJorKK0C8=
github.com/josephspurrier/goversioninfo v1.4.0/go.mod h1:JWzv5rKQr+MmW+LvM412ToT/IkYDZjaclF2pKDss8IY=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
@ -252,6 +248,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/mzky/zip v0.0.0-20210817031838-1b692015aed2 h1:g6FwzFqWNJsmf/E7DY7OPOoSIi0BU9Sz7/JY8aHIiHA=
github.com/mzky/zip v0.0.0-20210817031838-1b692015aed2/go.mod h1:nbeXRTTEZNdpEvY28aGERF/6Du3a3BfcPr1ETZ3anwI=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=

Loading…
Cancel
Save