Browse Source

增加数据备份功能

pull/2/head
娄文智 4 years ago
parent
commit
281eb12715
9 changed files with 94 additions and 5 deletions
  1. +1
    -0
      .gitignore
  2. +4
    -2
      conf/app.go
  3. +2
    -0
      conf/db.go
  4. +37
    -0
      db/corn.go
  5. +32
    -0
      db/db.go
  6. +9
    -0
      db/db_test.go
  7. +1
    -0
      go.mod
  8. +3
    -0
      go.sum
  9. +5
    -3
      main.go

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
.idea/

+ 4
- 2
conf/app.go View File

@ -1,8 +1,10 @@
package conf
var ExampleFile = "http://localhost:22000/public/uploadxlsx/example.xlsx"
var ExampleFile = "http://localhost:8092/public/uploadxlsx/example.xlsx"
//var ExampleFile = "http://localhost:22000/public/uploadxlsx/example.xlsx"
var Baseurl = "http://localhost:22000"
//var Baseurl = "http://localhost:22000"
var Baseurl = "http://localhost:8092"
type AppConf struct {
DisablePathCorrection bool


+ 2
- 0
conf/db.go View File

@ -8,6 +8,7 @@ type DbConf struct {
User string
Pwd string
DbName string
BackUp string
}
//线上服务器配置
@ -17,6 +18,7 @@ var MasterDbConfig DbConf = DbConf{
User: "root",
Pwd: "Leit2020",
DbName: "lappserver",
BackUp: "C:\\lappServer\\dblog",
}
//测试服务器配置


+ 37
- 0
db/corn.go View File

@ -0,0 +1,37 @@
// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
package db
import (
cron "github.com/robfig/cron/v3"
)
/******************************************************************************
*
* @Function Name :
*-----------------------------------------------------------------------------
*
* @Description : 定时任务
*
* @Function Parameters:
*
* @Return Value :
*
* @Author : Lou Wenzhi
*
* @Date : 2021/3/8 18:00
*
******************************************************************************/
func CornTime() {
crontab := cron.New()
task := func() {
BackUp()
}
// 添加定时任务, * * * * * 是 crontab,表示每分钟执行一次
//crontab.AddFunc("*/1 * * * *", task)
crontab.AddFunc("@midnight", task)
// 启动定时器
crontab.Start()
// 定时任务是另起协程执行的,这里使用 select 简答阻塞.实际开发中需要
// 根据实际情况进行控制
select {}
}

+ 32
- 0
db/db.go View File

@ -8,7 +8,10 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"lapp_-wy/conf"
"lapp_-wy/utils"
"lapp_-wy/web/middleware/glog"
"log"
"path/filepath"
"sync"
"time"
)
@ -73,3 +76,32 @@ func MgoDb() *mongo.Client {
return mgoDb
}
}
//数据备份
func BackUp() {
var (
engine *xorm.Engine
err error
)
c := conf.MasterDbConfig
driveSource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8",
c.User, c.Pwd, c.Host, c.Port, c.DbName)
if engine, err = xorm.NewEngine(conf.DriverName, driveSource); err != nil {
return
}
if err = engine.Ping(); err != nil {
glog.ErrorExtln("数据库连接失败:", "err:", err.Error())
return
} else {
glog.ErrorExtln("数据库连接成功:", "driveSource", driveSource)
}
filename := utils.TimeFormat(time.Now(),"yyyy-MM-dd") + ".sql"
dirpath := filepath.Join(c.BackUp,filename)
err = engine.DumpAllToFile(dirpath, conf.DriverName)
if err != nil {
return
}
}

+ 9
- 0
db/db_test.go View File

@ -11,6 +11,7 @@ import (
"log"
"path/filepath"
"testing"
"time"
)
func TestDb(t *testing.T) {
@ -28,6 +29,14 @@ func TestDb(t *testing.T) {
t.Error(err)
return
}
filename := utils.TimeFormat(time.Now(),"yyyy-MM-dd") + ".sql"
dirpath := filepath.Join(c.BackUp,filename)
fmt.Println(dirpath)
err = engine.DumpAllToFile(dirpath, conf.DriverName)
if err != nil{
log.Println("数据库导出失败:", err.Error())
return
}
//开启sql,debug
engine.ShowSQL(true)
if err = engine.Ping(); err != nil {


+ 1
- 0
go.mod View File

@ -33,6 +33,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/moul/http2curl v1.0.0 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect


+ 3
- 0
go.sum View File

@ -352,6 +352,9 @@ github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=


+ 5
- 3
main.go View File

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/kardianos/service"
"github.com/kataras/iris"
"lapp_-wy/db"
"lapp_-wy/inits/parse"
"lapp_-wy/utils"
"lapp_-wy/web/middleware/glog"
@ -102,9 +103,10 @@ func imain() {
app.RegisterView(iris.HTML(savePath, ".html"))
// 设置静态资源
app.StaticWeb("/public", savePath)
//数据备份
go db.CornTime()
//启动监听端口
app.Run(iris.Addr(":22000"), iris.WithConfiguration(parse.C))
//app.Run(iris.Addr(":8092"), iris.WithConfiguration(parse.C))
//app.Run(iris.Addr(":22000"), iris.WithConfiguration(parse.C))
app.Run(iris.Addr(":8092"), iris.WithConfiguration(parse.C))
}

Loading…
Cancel
Save