influxdata / influxdb

Scalable datastore for metrics, events, and real-time analytics
https://influxdata.com
Apache License 2.0
28.51k stars 3.53k forks source link

Any Golang Alternative/API for backup CLI command #18162

Open sid-08 opened 4 years ago

sid-08 commented 4 years ago

Hi!

I am trying to programatically take backup of InfluxDB to another server using golang.

I am able to do that via command line function of golang as follows:

cmd := exec.Command(configInfluxDB.InfluxdExeutablePath, "backup", "-portable", "-database", configInfluxDB.Database, "-host", configInfluxDB.HostName+":8088", fileStorePath)

However, I am unable to find any golang api function for the same purpose.

I would like to know similar function which could take the parameters path , database , host and file path and then take the backup

I tried using the backend functions defined in https://github.com/influxdata/influxdb/blob/master/cmd/influx/backup.go as follows:

func newBackupService() (influxdb.BackupService, error) {
    return &http.BackupService{
        Addr:  "http://localhost:8086",
        Token: "admin:admin",
    }, nil
}

func ConnectToDB(fullFileName string) (*InfluxReader, error) { 
    // Read InfluxDB instance's properties from an external file.
    configInfluxDB, err := loadInfluxProperty(fullFileName)

    fmt.Println("hello")

    HTTPAddr := fmt.Sprintf("http://%s:%s",configInfluxDB.HostName, configInfluxDB.PortNumber)
    c, _ := client.NewHTTPClient(client.HTTPConfig{
        Addr: HTTPAddr,
        Username: configInfluxDB.UserName,
        Password: configInfluxDB.Password,
    })
    _, _, err = c.Ping(0)
    if err != nil {
        log.Fatal("Error creating InfluxDB Client: ", err.Error())
    }
    defer c.Close()

    var slash = ""
    if string(os.TempDir()[0]) == "C" {
        slash = "\\"
    } else {
        slash = "/"
        }
    fileStorePath := os.TempDir() + slash + configInfluxDB.Database
        fmt.Println(fileStorePath)

        ctx := context.Background()

        err = os.MkdirAll(fileStorePath, 0777)
    if err != nil && !os.IsExist(err) {
        fmt.Println("e1")
        return nil,err
    }

    backupService, err := newBackupService()
    if err != nil {
        fmt.Println("e2")
        return nil,err
    }

    id, backupFilenames, err := backupService.CreateBackup(ctx)
    if err != nil {
        fmt.Println("e3")
        return nil,err
    }

    fmt.Printf("Backup ID %d contains %d files\n", id, len(backupFilenames))
    fmt.Printf("Backup complete")

        var files []string
    err = filepath.Walk(fileStorePath, visit(&files))
        if err != nil {
        fmt.Println("Could not store file names:", err)
        return nil, err
    }
    return &InfluxReader{ConfigInfluxDB: configInfluxDB,File: files,Path: fileStorePath}, err
}

On running the code, the output is:

hello
(.....some path......)
e3
Failed to establish connection with InfluxDB:2020/05/19 13:47:25 app.Run: 404 page not found

Kindly help Thanks

russorat commented 4 years ago

@testsid thanks for the issue. I think you might be mixing our 1.x apis with our 2.x apis. Check out https://github.com/influxdata/influxdb/tree/master-1.x/cmd/influxd instead. Does that help?