Last active
August 2, 2019 19:48
-
-
Save rbalman/dbd005ef50ae4b8cea213af058abf771 to your computer and use it in GitHub Desktop.
Dumping and restoring the memcache key values using telnet & dalli gem
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
#!/usr/bin/env ruby | |
# List all keys stored in memcache. | |
# Credit to Graham King at http://www.darkcoding.net/software/memcached-list-all-keys/ for the original article on how to get the data from memcache in the first place. | |
require 'net/telnet' | |
require 'csv' | |
headings = %w(id expires bytes key) | |
rows = [] | |
host = ENV['MEMCACHE_DB_HOST'] || "localhost" | |
port = ENV['MEMCACHE_DB_PORT'] || 11211 | |
dump_file = ENV['MEMCACHE_DB_DUMP_FILE'] || "memcache_dump.csv" | |
connection = Net::Telnet::new("Host" => host, "Port" => port, "Timeout" => 3) | |
matches = connection.cmd("String" => "stats items", "Match" => /^END/).scan(/STAT items:(\d+):number (\d+)/) | |
slabs = matches.inject([]) { |items, item| items << Hash[*['id','items'].zip(item).flatten]; items } | |
longest_key_len = 0 | |
CSV.open(dump_file, "w") do |csv| | |
csv << headings | |
slabs.each do |slab| | |
connection.cmd("String" => "stats cachedump #{slab['id']} #{slab['items']}", "Match" => /^END/) do |c| | |
matches = c.scan(/^ITEM (.+?) \[(\d+) b; (\d+) s\]$/).each do |key_data| | |
cache_key, bytes, expires_time = key_data | |
csv << [slab['id'], Time.at(expires_time.to_i), bytes, cache_key] | |
rows << [slab['id'], Time.at(expires_time.to_i), bytes, cache_key] | |
longest_key_len = [longest_key_len,cache_key.length].max | |
end | |
end | |
end | |
end | |
row_format = %Q(|%8s | %28s | %12s | %-#{longest_key_len}s |) | |
puts row_format%headings | |
rows.each{|row| puts row_format%row} | |
puts "\n############# successfully dumped in #{dump_file} ##############" | |
connection.close |
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
#!/usr/bin/env ruby | |
# List all keys stored in memcache. | |
# Credit to Graham King at http://www.darkcoding.net/software/memcached-list-all-keys/ for the original article on how to get the data from memcache in the first place. | |
require 'dalli' | |
require 'net/telnet' | |
destination = ENV['MEMCACHE_DESTINATION_HOST'] || "localhost" | |
source = ENV['MEMCACHE_SOURCE_HOST'] || "localhost" | |
port = ENV['MEMCACHE_DB_PORT'] || 11211 | |
expires_in = ENV['MEMCACHE_DB_EXPIRE_TIME'] || 604800 # 1.week in seconds | |
options = {:expires_in => expires_in } | |
dc_source = Dalli::Client.new("#{source}:#{port}") | |
dc_destination = Dalli::Client.new("#{destination}:#{port}", options) | |
connection = Net::Telnet::new("Host" => source, "Port" => port, "Timeout" => 3) | |
matches = connection.cmd("String" => "stats items", "Match" => /^END/).scan(/STAT items:(\d+):number (\d+)/) | |
slabs = matches.inject([]) { |items, item| items << Hash[*['id','items'].zip(item).flatten]; items } | |
slabs.each do |slab| | |
connection.cmd("String" => "stats cachedump #{slab['id']} #{slab['items']}", "Match" => /^END/) do |c| | |
matches = c.scan(/^ITEM (.+?) \[(\d+) b; (\d+) s\]$/).each do |key_data| | |
cache_key, bytes, expires_time = key_data | |
value = dc_source.get cache_key | |
p key_data | |
dc_destination.set cache_key, value | |
end | |
end | |
end | |
puts "\nsuccessfully copied from #{source} to #{destination}" | |
connection.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment