dongyun4010 2018-04-11 00:59
浏览 150
已采纳

备份DBmssql时出现Golang错误:BACKUP DATABASE异常终止

I am trying to do a full backup for an MSSQL database using go.

However, I tested the connection to the MSSQL server and it works like charm!

By executing also a query into the code SELECT @@VERSION

It gives me the version and that has been connected to mssql using the credentials in the code below.

By running a query BACKUP DATABASE name_db TO DISK = '/tmp/database.bak' it fails with the following error

Error with Backing up the DBmssql: BACKUP DATABASE is terminating abnormally

I am sure that I am missing something, any ideas/thoughts would be much appreciated.

BTW, I am on Ubuntu 16.04 and the mssql is installed there, I am using go version 1.9.

This is my code:

package main                                                                    
  import (                                                                      
     _ "github.com/denisenkom/go-mssqldb"                                       
     "database/sql"                                                             
     "context"                                                                  
     "log"                                                                      
     "fmt" )                                                                    


var server = "server_url"                                     
var port = 1433                                                                 
var user = "user"                                                          
var password = "pass"                                                  

var db *sql.DB                                                                  

func main() {                                                                   
        var err error                                                                                                                                           
        // Create connection string                                               
        connString := fmt.Sprintf("server=%s;user id=%s;password=%s;
        port=%d", server, user, password, port)                                   
        // Create connection pool                                                 
        db, err = sql.Open("sqlserver", connString)                             
        if err != nil {                                                         
                log.Fatal("Error creating connection pool: " 
                + err.Error())     
        }                                                                       
     log.Printf("Connected!
")                                                 
     defer db.Close()                                                           

        Backup()                                                                
        //Restore()                                                             
 }                                                                              

func Backup(){                                                                  
        ctx := context.Background()                                                                                                                     
        err := db.PingContext(ctx)
        if err != nil {
                log.Fatal("Error pinging database :( " + err.Error())
        }
        var result string
        err = db.QueryRowContext(ctx, "BACKUP DATABASE tempdb 
        TO DISK= '/tmp/Backup.bak' WITH NOFORMAT, NOINIT,
        SKIP, NOREWIND, NOUNLOAD, STATS = 10;").Scan(&result)
        //err = db.QueryRowContext(ctx, "select @@version").Scan(&result)
        if err != nil {
                log.Fatal("Error with Backing up the DB", err.Error())
        }
        fmt.Printf("%s
",result)

}   

I have found the solution which is very silly.. You can find it in the 1st reply.

  • 写回答

1条回答 默认 最新

  • doutui8842 2018-04-12 13:51
    关注

    That was obvious though, the code is clean and working as expected.

    The only issue was from MSSQL, where it have a specific value set for the timeout queries.

    That's why I succeeded executing other SQL queries like: SELECT @@VERSION from the example above in the Q.

    Though, I set the value of the timeout for remote queries to 0 this SQL query:

    EXEC SP_CONFIGURE 'remote query timeout', 0
    reconfigure
    EXEC sp_configure

    The 0 in remote query timeout means that the timeout is unlimited.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题