Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple script to migrate all Redis keys from one server (REDIS_ORIGIN_URL) to another (REDIS_DESTINATION_URL)
# NOTE: Don't get your two Redis URLs mixed up or you'll lose data!
require "redis"
redis1 = Redis.new(url: ENV.fetch("REDIS_ORIGIN_URL"))
redis2 = Redis.new(url: ENV.fetch("REDIS_DESTINATION_URL"))
# NOTE: Uncomment this if you want to clear out all Sidekiq stats from the ORIGIN(!) server
# redis1.pipelined do |pipeline|
# redis1.keys("stat:processed:*").each do |key|
# puts "Deleting #{key}"
# pipeline.del(key)
# end
# end
#
# redis1.pipelined do |pipeline|
# redis1.keys("stat:processed:*").each do |key|
# puts "Deleting #{key}"
# pipeline.del(key)
# end
# end
# Clear all redis2 keys
redis2.flushdb
redis1.keys.each do |key|
puts "Setting #{key}"
puts "Type: #{redis1.type(key)}"
case redis1.type(key)
when "string"
redis2.set(key, redis1.get(key))
when "list"
redis2.del(key)
redis1.lrange(key, 0, -1).each do |value|
redis2.rpush(key, value)
end
when "set"
redis2.del(key)
redis1.smembers(key).each do |value|
redis2.sadd(key, value)
end
when "zset"
redis2.del(key)
redis1.zrange(key, 0, -1, with_scores: true).each do |value, score|
redis2.zadd(key, score, value)
end
when "hash"
redis2.del(key)
redis1.hgetall(key).each do |field, value|
redis2.hset(key, field, value)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment