2015-12-25 13:23:22 +00:00
|
|
|
package run_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
2016-01-28 00:39:54 +00:00
|
|
|
"path/filepath"
|
2015-12-25 13:23:22 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-02-10 17:26:18 +00:00
|
|
|
"github.com/influxdata/influxdb/cmd/influxd/backup"
|
|
|
|
"github.com/influxdata/influxdb/cmd/influxd/restore"
|
|
|
|
"github.com/influxdata/influxdb/cmd/influxd/run"
|
2015-12-25 13:23:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_BackupAndRestore(t *testing.T) {
|
|
|
|
config := NewConfig()
|
|
|
|
config.Data.Engine = "tsm1"
|
|
|
|
config.Data.Dir, _ = ioutil.TempDir("", "data_backup")
|
|
|
|
config.Meta.Dir, _ = ioutil.TempDir("", "meta_backup")
|
2016-03-08 19:59:33 +00:00
|
|
|
config.BindAddress = freePort()
|
2015-12-25 13:23:22 +00:00
|
|
|
|
|
|
|
backupDir, _ := ioutil.TempDir("", "backup")
|
|
|
|
defer os.RemoveAll(backupDir)
|
|
|
|
|
|
|
|
db := "mydb"
|
|
|
|
rp := "forever"
|
|
|
|
expected := `{"results":[{"series":[{"name":"myseries","columns":["time","host","value"],"values":[["1970-01-01T00:00:00.001Z","A",23]]}]}]}`
|
|
|
|
|
|
|
|
// set the cache snapshot size low so that a single point will cause TSM file creation
|
|
|
|
config.Data.CacheSnapshotMemorySize = 1
|
|
|
|
|
|
|
|
func() {
|
2016-03-08 19:59:33 +00:00
|
|
|
s := OpenServer(config)
|
2015-12-25 13:23:22 +00:00
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
if err := s.CreateDatabaseAndRetentionPolicy(db, newRetentionPolicyInfo(rp, 1, 0)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-20 19:53:53 +00:00
|
|
|
if err := s.MetaClient.SetDefaultRetentionPolicy(db, rp); err != nil {
|
2015-12-25 13:23:22 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-03-08 19:59:33 +00:00
|
|
|
if _, err := s.Write(db, rp, "myseries,host=A value=23 1000000", nil); err != nil {
|
|
|
|
t.Fatalf("failed to write: %s", err)
|
|
|
|
}
|
2015-12-25 13:23:22 +00:00
|
|
|
|
|
|
|
// wait for the snapshot to write
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
res, err := s.Query(`select * from "mydb"."forever"."myseries"`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error querying: %s", err.Error())
|
|
|
|
}
|
|
|
|
if res != expected {
|
|
|
|
t.Fatalf("query results wrong:\n\texp: %s\n\tgot: %s", expected, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now backup
|
|
|
|
cmd := backup.NewCommand()
|
2016-03-08 19:59:33 +00:00
|
|
|
hostAddress, _ := run.DefaultHost(run.DefaultHostname, config.BindAddress)
|
2016-01-31 08:33:13 +00:00
|
|
|
if err := cmd.Run("-host", hostAddress, "-database", "mydb", backupDir); err != nil {
|
2016-03-08 19:59:33 +00:00
|
|
|
t.Fatalf("error backing up: %s, hostAddress: %s", err.Error(), hostAddress)
|
2015-12-25 13:23:22 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if _, err := os.Stat(config.Meta.Dir); err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("meta dir should be deleted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(config.Data.Dir); err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("meta dir should be deleted")
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore
|
|
|
|
cmd := restore.NewCommand()
|
|
|
|
|
|
|
|
if err := cmd.Run("-metadir", config.Meta.Dir, "-datadir", config.Data.Dir, "-database", "mydb", backupDir); err != nil {
|
|
|
|
t.Fatalf("error restoring: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2016-01-28 00:39:54 +00:00
|
|
|
// Make sure node.json was restored
|
|
|
|
nodePath := filepath.Join(config.Meta.Dir, "node.json")
|
|
|
|
if _, err := os.Stat(nodePath); err != nil || os.IsNotExist(err) {
|
|
|
|
t.Fatalf("node.json should exist")
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:23:22 +00:00
|
|
|
// now open it up and verify we're good
|
2016-03-08 19:59:33 +00:00
|
|
|
s := OpenServer(config)
|
2015-12-25 13:23:22 +00:00
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
res, err := s.Query(`select * from "mydb"."forever"."myseries"`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error querying: %s", err.Error())
|
|
|
|
}
|
|
|
|
if res != expected {
|
|
|
|
t.Fatalf("query results wrong:\n\texp: %s\n\tgot: %s", expected, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func freePort() string {
|
|
|
|
l, _ := net.Listen("tcp", "")
|
|
|
|
defer l.Close()
|
|
|
|
return l.Addr().String()
|
|
|
|
}
|