-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredisync.py
More file actions
161 lines (137 loc) · 6.97 KB
/
redisync.py
File metadata and controls
161 lines (137 loc) · 6.97 KB
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
import redis
import logging
import configparser
import argparse
from logging.handlers import SysLogHandler
def generate_config_template():
template = """
# Source credentials
[source]
source_hosts = 192.168.1.2, 192.168.1.3
source_password = sourceP@ssw0rd
# Target credentials
[target]
target_hosts = 192.168.1.4, 192.168.1.5
target_password = targetP@ssw0rd
# Output configuration
[output]
output_destination = syslog
"""
with open('/etc/redisync.conf', 'w') as file:
file.write(template.strip())
print("Configuration template '/etc/redisync.conf' generated.")
def output_keyspace_info(keyspace_info, label):
keyspace_str = ', '.join([f"{db}:keys={info['keys']},expires={info.get('expires', 0)},avg_ttl={info.get('avg_ttl', 0)}" for db, info in keyspace_info.items()])
if keyspace_str:
logger.info(f"{label} Keys: {keyspace_str}")
else:
logger.info(f"{label} Keys: No data available")
def is_master(redis_instance):
try:
info = redis_instance.info('replication')
return info['role'] == 'master'
except redis.ConnectionError as e:
error_message = str(e).split(".")[0]
logger.error(f"Connection error: {error_message}")
return None
except Exception as e:
logger.error(f"Error checking if instance is master: {e}")
return None
def migrate_redis_data(source_hosts, target_hosts, source_password=None, target_password=None):
source_master = None
target_master = None
# Connect to source and target Redis instances and find the master nodes
for source_host in source_hosts:
try:
source_redis = redis.Redis(host=source_host, port=6379, password=source_password)
if is_master(source_redis):
source_master = source_redis
logger.info(f"Connected to source master: {source_host}")
break
except Exception as e:
logger.error(f"Error connecting to source host {source_host}: {e}")
for target_host in target_hosts:
try:
target_redis = redis.Redis(host=target_host, port=6379, password=target_password)
if is_master(target_redis):
target_master = target_redis
logger.info(f"Connected to target master: {target_host}")
break
except Exception as e:
logger.error(f"Error connecting to target host {target_host}: {e}")
if not source_master or not target_master:
logger.error("Failed to find master nodes in either source or target clusters. Stopping migration.")
return
# Log the initial keyspace information
source_keyspace_info = source_master.info('keyspace') if source_master else {}
target_keyspace_info = target_master.info('keyspace') if target_master else {}
output_keyspace_info(source_keyspace_info, "Source")
output_keyspace_info(target_keyspace_info, "Target")
# Perform migration
for db in source_keyspace_info:
db_index = int(db[2:])
logger.info(f"Starting migration db{db_index} from source {source_hosts[0]} to target {target_hosts[0]}")
source_master.execute_command('SELECT', db_index)
target_master.execute_command('SELECT', db_index)
cursor = '0'
while cursor != 0:
cursor, keys = source_master.scan(cursor=cursor, count=1000)
for key in keys:
try:
ttl = source_master.pttl(key)
ttl = ttl if ttl > 0 else 0
value = source_master.dump(key)
if value:
target_master.restore(key, ttl, value, replace=True)
except Exception as e:
logger.error(f"Error migrating key {key}: {e}")
logger.info(f"Finished migrating database {db} from {source_hosts[0]} to {target_hosts[0]}.")
# Log final keyspace info
final_source_keyspace = source_master.info('keyspace') if source_master else {}
final_target_keyspace = target_master.info('keyspace') if target_master else {}
logger.info("Migration finished. Here is the final status:")
output_keyspace_info(final_source_keyspace, "Source")
output_keyspace_info(final_target_keyspace, "Target")
# Set up command-line argument parsing
parser = argparse.ArgumentParser(description="Redisync: Redis Data Migration Tool")
parser.add_argument('--config', default='/etc/redisync.conf', metavar='', help='Path to configuration file, "Default file: /etc/redisync.conf"')
parser.add_argument('--source', metavar='', help='Comma-separated list of source host addresses "It could be one Redis standalone instance"')
parser.add_argument('--source-password', metavar='', help='Password for source Redis instances')
parser.add_argument('--target', metavar='target', help='Comma-separated list of target host addresses "It could be one Redis standalone instance"')
parser.add_argument('--target-password', metavar='', help='Password for target Redis instances')
parser.add_argument('--output', metavar='', help='Output destination: syslog, stdout, or file')
parser.add_argument('--generate-config-file', action='store_true', help='Generate a template of the default configuration file')
args = parser.parse_args()
# Handle --generate-config before attempting to read the config file
if args.generate_config_file:
generate_config_template()
exit(0)
# Read configuration file
config = configparser.ConfigParser()
config.read(args.config)
# Retrieve configuration values and overwrite with command-line arguments if provided
source_hosts = args.source.split(',') if args.source else config.get('source', 'source_hosts').split(',')
source_password = args.source_password if args.source_password else config.get('source', 'source_password')
target_hosts = args.target.split(',') if args.target else config.get('target', 'target_hosts').split(',')
target_password = args.target_password if args.target_password else config.get('target', 'target_password')
output_destination = args.output if args.output else config.get('output', 'output_destination')
# Configure logging
logger = logging.getLogger('redisync')
logger.setLevel(logging.INFO)
if output_destination == 'syslog':
syslog_handler = SysLogHandler(address='/dev/log')
syslog_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
syslog_handler.setFormatter(syslog_formatter)
logger.addHandler(syslog_handler)
elif output_destination == 'stdout':
stdout_handler = logging.StreamHandler()
stdout_formatter = logging.Formatter('%(asctime)s - (redisync) - %(levelname)s - %(message)s')
stdout_handler.setFormatter(stdout_formatter)
logger.addHandler(stdout_handler)
elif output_destination == 'file':
file_handler = logging.FileHandler('snew-test-redisync.log')
file_formatter = logging.Formatter('%(asctime)s - (redisync) - %(levelname)s - %(message)s')
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
# Start the migration
migrate_redis_data(source_hosts, target_hosts, source_password, target_password)