Skip to content

Commit ce2e717

Browse files
author
JT
committed
[BC] Add options to specify source/destination dbNames, and also source collection names to watch
1 parent 442d69e commit ce2e717

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
lines changed

cmd/root.go

+7
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,26 @@ func init() {
5858

5959
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.go-sync-mongo.yaml)")
6060
RootCmd.PersistentFlags().String("src", "", "mongodb://host1:27017")
61+
RootCmd.PersistentFlags().String("src-db", "", "source database name")
62+
RootCmd.PersistentFlags().String("src-collections", "", "source collections separated by ,")
6163
RootCmd.PersistentFlags().String("src-username", "", "source database username")
6264
RootCmd.PersistentFlags().String("src-password", "", "source database password")
6365
RootCmd.PersistentFlags().Bool("src-ssl", false, "source ssl enabled (true)")
6466
RootCmd.PersistentFlags().String("dst", "", "mongodb://host1:27017,host2:27017")
67+
RootCmd.PersistentFlags().String("dst-db", "", "target database name")
68+
RootCmd.PersistentFlags().String("dst-collections", "", "dst collections separated by ,")
6569
RootCmd.PersistentFlags().String("dst-username", "", "destination database username")
6670
RootCmd.PersistentFlags().String("dst-password", "", "destiantion database password")
6771
RootCmd.PersistentFlags().Bool("dst-ssl", false, "destination ssl enabled (true)")
6872

6973
viper.BindPFlag("src", RootCmd.PersistentFlags().Lookup("src"))
74+
viper.BindPFlag("src-db", RootCmd.PersistentFlags().Lookup("src-db"))
75+
viper.BindPFlag("src-collections", RootCmd.PersistentFlags().Lookup("src-collections"))
7076
viper.BindPFlag("src-username", RootCmd.PersistentFlags().Lookup("src-username"))
7177
viper.BindPFlag("src-password", RootCmd.PersistentFlags().Lookup("src-password"))
7278
viper.BindPFlag("src-ssl", RootCmd.PersistentFlags().Lookup("src-ssl"))
7379
viper.BindPFlag("dst", RootCmd.PersistentFlags().Lookup("dst"))
80+
viper.BindPFlag("dst-db", RootCmd.PersistentFlags().Lookup("dst-db"))
7481
viper.BindPFlag("dst-username", RootCmd.PersistentFlags().Lookup("dst-username"))
7582
viper.BindPFlag("dst-password", RootCmd.PersistentFlags().Lookup("dst-password"))
7683
viper.BindPFlag("dst-ssl", RootCmd.PersistentFlags().Lookup("dst-ssl"))

cmd/sync.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package cmd
22

33
import (
44
"fmt"
5+
s "strings"
56

6-
db "github.com/checkr/go-sync-mongo/db"
7+
db "../db"
78
"github.com/spf13/cobra"
89
"github.com/spf13/viper"
910
mgo "gopkg.in/mgo.v2"
@@ -21,6 +22,8 @@ var syncCmd = &cobra.Command{
2122
Username: viper.GetString("src-username"),
2223
Password: viper.GetString("src-password"),
2324
},
25+
Database: viper.GetString("src-db"),
26+
Collections: s.Split(viper.GetString("src-collections"), ","),
2427
}
2528
src, err := db.NewConnection(srcConfig)
2629
if err != nil {
@@ -34,6 +37,8 @@ var syncCmd = &cobra.Command{
3437
Username: viper.GetString("dst-username"),
3538
Password: viper.GetString("dst-password"),
3639
},
40+
Database: viper.GetString("dst-db"),
41+
Collections: make([]string, 0),
3742
}
3843
dst, err := db.NewConnection(dstConfig)
3944
if err != nil {

db/config.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package db
33
import mgo "gopkg.in/mgo.v2"
44

55
type Config struct {
6-
URI string
7-
SSL bool
8-
Creds mgo.Credential
6+
URI string
7+
SSL bool
8+
Creds mgo.Credential
9+
Database string
10+
Collections []string
911
}
1012

1113
func (p *Config) Load() error {

db/db.go

+42-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"net"
8+
s "strings"
89
"sync"
910
"time"
1011

@@ -88,7 +89,7 @@ func (c *Connection) Databases() ([]string, error) {
8889
var slice []string
8990

9091
for _, dbname := range dbnames {
91-
if dbname != "local" && dbname != "admin" {
92+
if dbname == c.config.Database {
9293
slice = append(slice, dbname)
9394
}
9495
}
@@ -104,7 +105,7 @@ func (c *Connection) databaseRegExs() ([]bson.RegEx, error) {
104105
var slice []bson.RegEx
105106

106107
for _, dbname := range dbnames {
107-
if dbname != "local" && dbname != "admin" {
108+
if dbname == c.config.Database {
108109
slice = append(slice, bson.RegEx{Pattern: dbname + ".*"})
109110
}
110111
}
@@ -179,6 +180,7 @@ func (c *Connection) SyncOplog(dst *Connection) error {
179180
// apply the operation
180181
opsToApply := []Oplog{oplogEntry}
181182
err := dst.Session.Run(bson.M{"applyOps": opsToApply}, &applyOpsResponse)
183+
182184
if err != nil {
183185
return fmt.Errorf("error applying ops: %v", err)
184186
}
@@ -198,7 +200,8 @@ func (c *Connection) SyncOplog(dst *Connection) error {
198200
}
199201
}
200202

201-
fmt.Println("Tailing...")
203+
fmt.Println("Tailing.....")
204+
202205
iter = oplog.Find(tail_query).Tail(1 * time.Second)
203206
for {
204207
for iter.Next(&oplogEntry) {
@@ -207,11 +210,46 @@ func (c *Connection) SyncOplog(dst *Connection) error {
207210
log.Printf("skipping no-op for namespace `%v`", oplogEntry.Namespace)
208211
continue
209212
}
210-
opCount++
213+
214+
if !s.Contains(oplogEntry.Namespace, c.config.Database+".") {
215+
log.Printf("skipping namespace `%v`", oplogEntry.Namespace)
216+
continue
217+
}
218+
219+
// check collection against config
220+
collection := s.Split(oplogEntry.Namespace, ".")[1]
221+
222+
isCollectionMatch := false
223+
for _, permittedCollection := range c.config.Collections {
224+
if collection == permittedCollection {
225+
isCollectionMatch = true
226+
}
227+
}
228+
229+
if !isCollectionMatch {
230+
log.Printf("skipping collection `%v`", oplogEntry.Namespace)
231+
continue
232+
}
233+
234+
oplogEntry.Namespace = dst.config.Database + "." + collection
235+
236+
if false {
237+
fmt.Println("\n")
238+
fmt.Println("****************************** %v", oplogEntry.HistoryID)
239+
fmt.Println("****************************** %v", oplogEntry.Namespace)
240+
fmt.Println("****************************** %v", oplogEntry.Object)
241+
fmt.Println("****************************** %v", oplogEntry.Operation)
242+
fmt.Println("****************************** %v", oplogEntry.Query)
243+
fmt.Println("****************************** %v", oplogEntry.Timestamp)
244+
fmt.Println("****************************** %v", oplogEntry.Version)
245+
fmt.Println("%v", oplogEntry.Namespace)
246+
}
211247

212248
// apply the operation
249+
opCount++
213250
opsToApply := []Oplog{oplogEntry}
214251
err := dst.Session.Run(bson.M{"applyOps": opsToApply}, &applyOpsResponse)
252+
215253
if err != nil {
216254
return fmt.Errorf("error applying ops: %v", err)
217255
}

go-sync-mongo

12.9 MB
Binary file not shown.

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package main
1616

17-
import "github.com/checkr/go-sync-mongo/cmd"
17+
import "./cmd"
1818

1919
func main() {
2020
cmd.Execute()

0 commit comments

Comments
 (0)