Skip to content

Instantly share code, notes, and snippets.

@natematykiewicz
Last active February 21, 2024 17:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save natematykiewicz/f19f09474288e6182b550a858c5cb250 to your computer and use it in GitHub Desktop.
Save natematykiewicz/f19f09474288e6182b550a858c5cb250 to your computer and use it in GitHub Desktop.
Migrate Sidekiq Redis
# 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