-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
180 lines (152 loc) · 5.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/olivere/elastic/v7"
)
const (
batchSize = 10000
retryCount = 3
retryDelay = 5 * time.Second
)
type ElasticConfig struct {
URL string
Index string
User string
Pass string
}
func main() {
srcUser := flag.String("U", "", "Source username")
srcPass := flag.String("P", "", "Source password")
srcHost := flag.String("H", "", "Source host")
srcPort := flag.String("R", "", "Source port")
srcSSL := flag.Bool("S", false, "Use SSL/HTTPS for source")
srcIndex := flag.String("I", "", "Source index name")
srcInsecure := flag.Bool("insecure", false, "Skip SSL certificate verification for source")
fromDoc := flag.Int("F", 1, "Start copying from this document number (1-indexed)")
toDoc := flag.Int("T", 0, "Stop copying at this document number (0 for no limit)")
destUser := flag.String("u", "", "Target username")
destPass := flag.String("p", "", "Target password")
destHost := flag.String("h", "", "Target host")
destPort := flag.String("r", "", "Target port")
destSSL := flag.Bool("s", false, "Use SSL/HTTPS for target")
destIndex := flag.String("i", "", "Target index name")
flag.Parse()
if *srcHost == "" || *destHost == "" || *srcIndex == "" || *destIndex == "" {
fmt.Println("Missing required parameters. Please provide host and index information for both source and destination.")
flag.Usage()
return
}
srcScheme := "http"
if *srcSSL {
srcScheme = "https"
}
destScheme := "http"
if *destSSL {
destScheme = "https"
}
srcURL := fmt.Sprintf("%s://%s:%s@%s:%s", srcScheme, *srcUser, *srcPass, *srcHost, *srcPort)
destURL := fmt.Sprintf("%s://%s:%s@%s:%s", destScheme, *destUser, *destPass, *destHost, *destPort)
sourceConfig := ElasticConfig{URL: srcURL, Index: *srcIndex, User: *srcUser, Pass: *srcPass}
destinationConfig := ElasticConfig{URL: destURL, Index: *destIndex, User: *destUser, Pass: *destPass}
sourceClient, err := CreateClient(sourceConfig, *srcInsecure, true)
if err != nil {
log.Fatalf("Error creating source client: %v", err)
}
destClient, err := CreateClient(destinationConfig, false, true)
if err != nil {
log.Fatalf("Error creating destination client: %v", err)
}
ensureIndex(context.Background(), destClient, *destIndex)
actualTotalDocs, err := getTotalDocumentCount(sourceClient, *srcIndex)
if err != nil {
log.Fatalf("Error getting total document count: %v", err)
}
if *toDoc == 0 || *toDoc > actualTotalDocs {
*toDoc = actualTotalDocs
}
if err := copyData(context.Background(), sourceClient, destClient, sourceConfig, *destIndex, *fromDoc, *toDoc); err != nil {
log.Fatalf("Error copying data: %v", err)
}
fmt.Println("\nData migration completed successfully.")
}
func CreateClient(cfg ElasticConfig, insecure bool, forceHttp1 bool) (*elastic.Client, error) {
options := []elastic.ClientOptionFunc{
elastic.SetURL(cfg.URL),
elastic.SetBasicAuth(cfg.User, cfg.Pass),
elastic.SetSniff(false),
}
if insecure || forceHttp1 {
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
TLSNextProto: map[string]func(string, *tls.Conn) http.RoundTripper{},
},
}
options = append(options, elastic.SetHttpClient(httpClient))
}
return elastic.NewClient(options...)
}
func ensureIndex(ctx context.Context, client *elastic.Client, index string) {
exists, err := client.IndexExists(index).Do(ctx)
if err != nil {
log.Fatalf("Error checking if index exists: %v", err)
}
if !exists {
_, err = client.CreateIndex(index).Do(ctx)
if err != nil {
log.Fatalf("Error creating index: %v", err)
}
}
}
func getTotalDocumentCount(client *elastic.Client, index string) (int, error) {
countService := client.Count(index)
count, err := countService.Do(context.Background())
if err != nil {
return 0, err
}
return int(count), nil
}
func copyData(ctx context.Context, sourceClient, destClient *elastic.Client, srcConfig ElasticConfig, destIndex string, fromDoc int, toDoc int) error {
totalDocs := toDoc - fromDoc + 1
query := elastic.NewMatchAllQuery()
scroll := sourceClient.Scroll(srcConfig.Index).Query(query).Size(batchSize)
copiedDocs := 0
for {
results, err := scroll.Do(ctx)
if err != nil {
return fmt.Errorf("error retrieving results: %v", err)
}
if len(results.Hits.Hits) == 0 {
break
}
bulkRequest := destClient.Bulk()
for _, hit := range results.Hits.Hits {
copiedDocs++
if copiedDocs < fromDoc {
continue
}
if toDoc != 0 && copiedDocs > toDoc {
break
}
req := elastic.NewBulkIndexRequest().Index(destIndex).Id(hit.Id).Doc(hit.Source)
bulkRequest = bulkRequest.Add(req)
}
if bulkRequest.NumberOfActions() > 0 {
if _, err := bulkRequest.Do(ctx); err != nil {
return fmt.Errorf("error bulk indexing: %v", err)
}
}
if toDoc != 0 && copiedDocs >= toDoc {
break
}
fmt.Fprintf(os.Stdout, "\rProgress: Copied %d/%d documents (%.2f%%)", copiedDocs-fromDoc+1, totalDocs, float64(copiedDocs-fromDoc+1)*100/float64(totalDocs))
}
return nil
}