45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
import redis
|
|
|
|
# 本地 Redis
|
|
src = redis.Redis(host='localhost', port=6379, password='Zzh08165511', db=0, decode_responses=False)
|
|
|
|
# 远程 Redis
|
|
dst = redis.Redis(host='175.24.177.96', port=6379, password='Zzh08165511', db=0, decode_responses=False)
|
|
|
|
src_count = src.dbsize()
|
|
dst_count = dst.dbsize()
|
|
print(f"本地: {src_count} keys")
|
|
print(f"远程: {dst_count} keys")
|
|
print(f"需同步: {src_count - dst_count} keys")
|
|
print("")
|
|
|
|
# 获取所有本地 key
|
|
keys = src.keys()
|
|
total = len(keys)
|
|
synced = 0
|
|
updated = 0
|
|
|
|
for key in keys:
|
|
try:
|
|
data = src.dump(key)
|
|
if data:
|
|
ttl = src.ttl(key)
|
|
if ttl < 0:
|
|
ttl = 0
|
|
# 检查远程是否已有此 key
|
|
exists = dst.exists(key)
|
|
dst.restore(key, ttl * 1000, data, replace=True)
|
|
if exists:
|
|
updated += 1
|
|
else:
|
|
synced += 1
|
|
if (synced + updated) % 100 == 0:
|
|
print(f"进度: {synced + updated}/{total}")
|
|
except Exception as e:
|
|
print(f"同步失败 {key}: {e}")
|
|
|
|
print(f"\n同步完成!")
|
|
print(f"新增: {synced} keys")
|
|
print(f"更新: {updated} keys")
|
|
print(f"远程现有: {dst.dbsize()} keys")
|