migrate.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import subprocess
  2. import redis
  3. import time
  4. import os
  5. # Container names
  6. SOURCE_CONTAINER = "redis-old-mailcow"
  7. DEST_CONTAINER = "valkey-mailcow"
  8. VALKEYPASS = os.getenv("VALKEYPASS")
  9. def migrate_redis():
  10. src_redis = redis.StrictRedis(host=SOURCE_CONTAINER, port=6379, db=0, password=VALKEYPASS, decode_responses=False)
  11. dest_redis = redis.StrictRedis(host=DEST_CONTAINER, port=6379, db=0, password=VALKEYPASS, decode_responses=False)
  12. cursor = 0
  13. batch_size = 100
  14. migrated_count = 0
  15. print("Starting migration...")
  16. while True:
  17. cursor, keys = src_redis.scan(cursor=cursor, match="*", count=batch_size)
  18. keys_to_migrate = [key for key in keys if not key.startswith(b"PHPREDIS_SESSION:")]
  19. for key in keys_to_migrate:
  20. key_type = src_redis.type(key)
  21. print(f"Import {key} of type {key_type}")
  22. if key_type == b"string":
  23. value = src_redis.get(key)
  24. dest_redis.set(key, value)
  25. elif key_type == b"hash":
  26. value = src_redis.hgetall(key)
  27. dest_redis.hset(key, mapping=value)
  28. elif key_type == b"list":
  29. value = src_redis.lrange(key, 0, -1)
  30. for v in value:
  31. dest_redis.rpush(key, v)
  32. elif key_type == b"set":
  33. value = src_redis.smembers(key)
  34. for v in value:
  35. dest_redis.sadd(key, v)
  36. elif key_type == b"zset":
  37. value = src_redis.zrange(key, 0, -1, withscores=True)
  38. for v, score in value:
  39. dest_redis.zadd(key, {v: score})
  40. # Preserve TTL if exists
  41. ttl = src_redis.ttl(key)
  42. if ttl > 0:
  43. dest_redis.expire(key, ttl)
  44. migrated_count += 1
  45. if cursor == 0:
  46. break # No more keys to scan
  47. print(f"Migration completed! {migrated_count} keys migrated.")
  48. print("Forcing Valkey to save data...")
  49. try:
  50. dest_redis.save() # Immediate RDB save (blocking)
  51. dest_redis.bgrewriteaof() # Rewrites the AOF file in the background
  52. print("Data successfully saved to disk.")
  53. except Exception as e:
  54. print(f"Failed to save data: {e}")
  55. # Main script execution
  56. if __name__ == "__main__":
  57. try:
  58. migrate_redis()
  59. finally:
  60. pass