forked from sonic-net/sonic-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentralize_database
executable file
·62 lines (53 loc) · 2.15 KB
/
centralize_database
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
#!/usr/bin/python
from __future__ import print_function
import sys
from swsscommon.swsscommon import SonicDBConfig
import redis
import argparse
def centralize_to_target_db(target_dbname):
target_dbport = SonicDBConfig.getDbPort(target_dbname)
target_dbhost = SonicDBConfig.getDbHostname(target_dbname)
dblists = SonicDBConfig.getDbList()
for dbname in dblists:
dbport = SonicDBConfig.getDbPort(dbname)
dbhost = SonicDBConfig.getDbHostname(dbname)
# if the db is on the same instance, no need to move
if dbport == target_dbport and dbhost == target_dbhost:
continue
dbsocket = SonicDBConfig.getDbSock(dbname)
dbid = SonicDBConfig.getDbId(dbname)
r = redis.Redis(host=dbhost, unix_socket_path=dbsocket, db=dbid)
script = """
local cursor = 0;
repeat
local dat = redis.call('SCAN', cursor, 'COUNT', 7000);
cursor = dat[1];
redis.call('MIGRATE', KEYS[1], KEYS[2], '', KEYS[3], 5000, 'COPY', 'REPLACE', 'KEYS', unpack(dat[2]));
until cursor == '0';
"""
r.eval(script, 3, target_dbhost, target_dbport, dbid)
#SAVE rdb file
r = redis.Redis(host=target_dbhost, port=target_dbport)
r.save()
def main():
parser = argparse.ArgumentParser(description='centralize all db data into one db instances',
formatter_class=argparse.RawTextHelpFormatter,
epilog=
"""
Example : centralize_database APPL_DB
""")
parser.add_argument('target_db', type=str, help='move all db data into the instance where target db locates')
args = parser.parse_args()
if args.target_db:
try:
centralize_to_target_db(args.target_db)
print(SonicDBConfig.getDbInst(args.target_db))
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print(message, file=sys.stderr)
sys.exit(1)
else:
parser.print_help()
if __name__ == "__main__":
main()