Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions internal/hotbackup/hotbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/percona/mongodb-backup/mdbstructs"
)

var (
ErrNotLocalhost = errors.New("session must be direct session to localhost")
ErrNotLocalhost = errors.New("session must be direct session to localhost")
ErrNotDirectConn = errors.New("session is not direct")
)

func isLocalhostSession(session *mgo.Session) (bool, error) {
Expand All @@ -22,8 +22,10 @@ func isLocalhostSession(session *mgo.Session) (bool, error) {
}

// check the server host == os.Hostname
status := mdbstructs.ReplsetStatus{}
err = session.Run(bson.D{{"serverStatus", "1"}}, &status)
status := struct {
Host string `bson:"host"`
}{}
err = session.Run(bson.D{{"serverStatus", 1}}, &status)
if err != nil {
return false, err
}
Expand All @@ -35,7 +37,7 @@ func isLocalhostSession(session *mgo.Session) (bool, error) {
// check connection is direct
servers := session.LiveServers()
if len(servers) != 1 {
return false, errors.New("session is not direct")
return false, ErrNotDirectConn
}
split = strings.SplitN(servers[0], ":", 2)
for _, match := range []string{"127.0.0.1", "localhost", hostname} {
Expand All @@ -51,8 +53,9 @@ type HotBackup struct {
removed bool
}

// New creates a Percona Server for MongoDB Hot Backup. The provided
// MongoDB session must be a direct connection to localhost/127.0.0.1
// New creates a Percona Server for MongoDB Hot Backup and outputs it
// to the specified backup directory. The provided MongoDB session must
// be a direct connection to localhost/127.0.0.1
//
// https://www.percona.com/doc/percona-server-for-mongodb/LATEST/hot-backup.html
//
Expand Down
4 changes: 4 additions & 0 deletions internal/hotbackup/hotbackup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func TestHotBackupRemove(t *testing.T) {
t.Fatal("Backup dir should not exist after .Remove()")
} else if !hb.removed {
t.Fatal("'removed' field should be true after .Remove()")
} else if hb.Remove() != nil {
t.Fatal("Failed to run .Remove()")
}
}

Expand All @@ -91,5 +93,7 @@ func TestHotBackupClose(t *testing.T) {
t.Fatal("'removed' field should be true after .Close()")
} else if hb.dir != "" {
t.Fatal("'dir' field should be empty after .Close()")
} else if _, err := os.Stat(tempDir); err == nil {
t.Fatal("Backup dir should not exist after .Close()")
}
}
1 change: 0 additions & 1 deletion mdbstructs/replset_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ type ReplsetStatusMember struct {
}

type ReplsetStatus struct {
Host string `bson:"host" json:"host"`
Set string `bson:"set" json:"set"`
Date time.Time `bson:"date" json:"date"`
MyState ReplsetMemberState `bson:"myState" json:"myState"`
Expand Down