Skip to content

Instantly share code, notes, and snippets.

@pietern
Created July 18, 2010 11:15
Show Gist options
  • Save pietern/480331 to your computer and use it in GitHub Desktop.
Save pietern/480331 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Script to remove commands from an AOF. Writes trimmed down AOF
# to standard out. Progress report is written to standard error.
#
# Usage: redis-trim-aof.rb <appendonly.aof> [regexp ...]
# Example: redis-trim.aof.rb myaof.aof ^user:\\d+ > newaof.aof
#
# Note: make sure to provide enough escape characters for special
# characters inside the regexp (because a regular shell also does
# escaping of characters).
#
require 'stringio'
class Command
attr_accessor :argc, :argv
def initialize(io)
self.argc = io.gets.strip[/^\*(\d+)$/,1].to_i
self.argv = []
argc.times {
bytes = io.gets.strip[/^\$(\d+)$/,1].to_i
self.argv << io.read(bytes)
io.read(2) # consume \r\n
}
end
def match?(rgxs)
rgxs.any? { |r| r.match(argv[1]) }
end
def to_s
out = "*#{argv.size}\r\n"
argv.each { |arg|
out << "$#{arg.size}\r\n"
out << arg
out << "\r\n"
}
out
end
end
f = File.open(ARGV[0],'r')
fs = File.size(ARGV[0])
skip = ARGV[1..-1].map { |arg| Regexp.new(arg) }
while !f.eof?
cmd = Command.new(f)
if !cmd.match?(skip)
STDOUT.print cmd.to_s
end
pct = f.pos.to_f / fs
STDERR.print "\r%6.2f%%" % (pct * 100)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment