Created
May 28, 2013 09:02
-
-
Save dekart/5661483 to your computer and use it in GitHub Desktop.
Redis backup & restore script
This file contains 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
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) |
This file contains 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
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