Skip to content

Commit 7022d19

Browse files
author
Jamie Curnow
committed
Better output of progress
1 parent 4edf1dd commit 7022d19

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Any pre-existing keys on DESTINATION that are not on SOURCE will remain untouche
1717
## Usage
1818

1919
```bash
20-
Usage: redis-migrator --source-host SOURCE-HOST --destination-host DESTINATION-HOST
21-
[--source-port SOURCE-PORT] [--source-db SOURCE-DB] [--source-user SOURCE-USER]
22-
[--source-pass SOURCE-PASS] [--destination-port DESTINATION-PORT]
20+
Usage: redis-migrator --source-host SOURCE-HOST [--source-port SOURCE-PORT]
21+
[--source-db SOURCE-DB] [--source-user SOURCE-USER] [--source-pass SOURCE-PASS]
22+
--destination-host DESTINATION-HOST [--destination-port DESTINATION-PORT]
2323
[--destination-db DESTINATION-DB] [--destination-user DESTINATION-USER]
2424
[--destination-pass DESTINATION-PASS] [--source-filter SOURCE-FILTER]
2525
[--destination-prefix DESTINATION-PREFIX] [--verbose]
@@ -51,6 +51,7 @@ Options:
5151
destination key prefix to prepend
5252
--verbose, -v Print a lot more info
5353
--help, -h display this help and exit
54+
--version display version and exit
5455
```
5556

5657
## Install
@@ -73,3 +74,44 @@ git clone https://github.com/jc21/redis-migrator && cd redis-migrator
7374
go build -ldflags="-X main.version=1.0.1" -o bin/redis-migrator cmd/redis-migrator/main.go
7475
./bin/redis-migrator -h
7576
```
77+
78+
79+
## Real world example
80+
81+
Copying data from one db to another on the same instance. This is a production instance
82+
where the keys were expiring mid-way through the migration, hence why the total
83+
number of keys migrated doesn't match the initial count.
84+
85+
```bash
86+
time redis-migrator --source-host 127.0.0.1 --source-db 0 --destination-host 127.0.0.1 --destination-db 1
87+
SOURCE:
88+
Server: 127.0.0.1:6379
89+
DB Index: 0
90+
Auth: None
91+
DESTINATION:
92+
Server: 127.0.0.1:6379
93+
DB Index: 1
94+
Auth: None
95+
2021-02-22 07:08:37 INFO => Source has 622028 keys
96+
2021-02-22 07:08:37 INFO => Destination has 0 keys
97+
2021-02-22 07:08:37 INFO => Found 622028 keys on SOURCE with Key filter: *
98+
2021-02-22 07:08:37 INFO => Migration running, each dot is ~1,000 keys
99+
.......... .......... .......... .......... .......... 8% (50012 / 622028)
100+
.......... .......... .......... .......... .......... 16% (100028 / 622028)
101+
.......... .......... .......... .......... .......... 24% (150037 / 622028)
102+
.......... .......... .......... .......... .......... 32% (200046 / 622028)
103+
.......... .......... .......... .......... .......... 40% (250058 / 622028)
104+
.......... .......... .......... .......... .......... 48% (300073 / 622028)
105+
.......... .......... .......... .......... .......... 56% (350084 / 622028)
106+
.......... .......... .......... .......... .......... 64% (400090 / 622028)
107+
.......... .......... .......... .......... .......... 72% (450102 / 622028)
108+
.......... .......... .......... .......... .......... 80% (500113 / 622028)
109+
.......... .......... .......... .......... .......... 88% (550127 / 622028)
110+
.......... .......... .......... .......... .......... 96% (600147 / 622028)
111+
.......... .......... ..
112+
2021-02-22 07:16:33 INFO => Migration completed with 622004 keys, 0 skipped :)
113+
114+
real 7m55.358s
115+
user 0m52.892s
116+
sys 4m32.788s
117+
```

pkg/migrator/migrate.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ func DoMigration(sourceClient, destinationClient *redis.Client, keyFilter, keyPr
2626
os.Exit(1)
2727
}
2828

29-
logger.Info("Migration running, each dot is 1,000 keys, each row is 50,000 keys")
29+
logger.Info("Migration running, each dot is ~1,000 keys")
3030

3131
var cursor uint64
3232
var n int
3333

3434
counter := 0
3535
skipped := 0
3636
dots := 0
37+
var percentage int64
3738

3839
for {
3940
var keys []string
@@ -76,11 +77,16 @@ func DoMigration(sourceClient, destinationClient *redis.Client, keyFilter, keyPr
7677
fmt.Print(".")
7778
dots++
7879
n += len(keys)
79-
if cursor == 0 {
80-
break
80+
if dots%10 == 0 {
81+
fmt.Print(" ")
8182
}
8283
if dots%50 == 0 {
83-
fmt.Print("\n")
84+
// Print a percentage
85+
percentage = int64(math.Round((float64(counter) / float64(size)) * 100))
86+
fmt.Printf("%d%% (%d / %d)\n", percentage, counter, size)
87+
}
88+
if cursor == 0 {
89+
break
8490
}
8591
}
8692

0 commit comments

Comments
 (0)