Skip to content

Instantly share code, notes, and snippets.

@dekart
Created May 28, 2013 09:02
Show Gist options
  • Save dekart/5661483 to your computer and use it in GitHub Desktop.
Save dekart/5661483 to your computer and use it in GitHub Desktop.
Redis backup & restore script
def backup(path, redis)
keys = redis.keys
puts "#{ keys.size } keys to dump"
File.open(path, 'w+') do |f|
keys.each_with_index do |key, i|
next unless dump = redis.dump(key)
f.puts JSON.dump([key, dump.force_encoding('utf-8')]).force_encoding('utf-8')
puts "#{i} / #{ keys.size }" if i % 100 == 0
end
end
puts "Done!"
end
backup(Rails.root.join('tmp/redis.json'), Redis.current)
def load(path, redis)
File.open(path, 'r') do |f|
i = 0
f.each_line do |line|
i += 1
key, dump = JSON.load(line.force_encoding('utf-8'))
redis.restore(key, 0, dump)
puts i if i % 100 == 0
end
end
end
Redis.new(:db => 6).flushdb # Cleaning up the database
load(Rails.root.join('tmp/redis.json'), Redis.new(:db => 6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment