From 281eb12715ae8e330390537e269833722b27932c Mon Sep 17 00:00:00 2001 From: louwenzhi Date: Fri, 12 Mar 2021 13:44:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=A4=87?= =?UTF-8?q?=E4=BB=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + conf/app.go | 6 ++++-- conf/db.go | 2 ++ db/corn.go | 37 +++++++++++++++++++++++++++++++++++++ db/db.go | 32 ++++++++++++++++++++++++++++++++ db/db_test.go | 9 +++++++++ go.mod | 1 + go.sum | 3 +++ main.go | 8 +++++--- 9 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 db/corn.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/conf/app.go b/conf/app.go index 4fedb14..4fca2f6 100644 --- a/conf/app.go +++ b/conf/app.go @@ -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 diff --git a/conf/db.go b/conf/db.go index 2800ce9..569f3c7 100644 --- a/conf/db.go +++ b/conf/db.go @@ -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", } //测试服务器配置 diff --git a/db/corn.go b/db/corn.go new file mode 100644 index 0000000..cc3982d --- /dev/null +++ b/db/corn.go @@ -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 {} +} diff --git a/db/db.go b/db/db.go index 7878c1f..2a8c1b2 100644 --- a/db/db.go +++ b/db/db.go @@ -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 + } + +} diff --git a/db/db_test.go b/db/db_test.go index b505eec..0e7a15a 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -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 { diff --git a/go.mod b/go.mod index 23927f8..c1996d8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 93f8338..b3a356c 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 422c6ed..ff3337e 100644 --- a/main.go +++ b/main.go @@ -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)) }