Last active
February 21, 2024 17:18
-
-
Save natematykiewicz/f19f09474288e6182b550a858c5cb250 to your computer and use it in GitHub Desktop.
Migrate Sidekiq Redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A script to migrate Sidekiq's redis to a new server. | |
# This obviously can work for any redis, but I only handled | |
# data types that Sidekiq uses. | |
require 'redis' | |
old_redis = Redis.new url: 'redis://old-redis:6379' | |
new_redis = Redis.new url: 'redis://new-redis:6379' | |
unknowns = [] | |
old_redis.keys('*').each do |key| | |
puts key | |
case old_redis.type(key) | |
when 'string' | |
new_redis.set(key, old_redis.get(key)) | |
when 'hash' | |
new_redis.mapped_hmset(key, old_redis.hgetall(key)) | |
when 'zset' | |
new_members = old_redis.zrange(key, 0, -1, with_scores: true).map { |m, s| [s, m] } | |
new_redis.zadd(key, new_members) | |
when 'set' | |
new_redis.sadd(key, *old_redis.smembers(key)) | |
else | |
unknowns << key | |
end | |
ttl = old_redis.ttl(key) | |
new_redis.expire(key, ttl) if ttl > 0 | |
end | |
puts "Unknown Keys: #{unknowns.inspect}" if unknowns.any? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment