5 # :title: RBot Registry Export, Import and Migration Script.
7 # You can use this script to,
8 # - export the rbot registry in a format that is platform/engine independent
9 # - import these backups in supported formats (dbm, daybreak)
10 # - migrate old rbot registries bdb (ruby 1.8) and tokyocabinet.
12 # For more information, just execute the script without any arguments!
14 # Author:: apoc (Matthias Hecker) <apoc@geekosphere.org>
15 # Copyright:: (C) 2014 Matthias Hecker
18 begin; require 'rubygems'; rescue Exception; end
20 # load registry formats:
21 begin; require 'bdb'; rescue Exception; end
22 begin; require 'tokyocabinet'; rescue Exception; end
23 begin; require 'dbm'; rescue Exception; end
24 begin; require 'daybreak'; rescue Exception; end
26 puts 'RBot Registry Backup/Restore/Migrate'
27 puts '[%s]' % ['Ruby: ' + RUBY_VERSION,
28 'DBM: ' + (DBM::VERSION rescue '-'),
29 'BDB: ' + (BDB::VERSION rescue '-'),
30 'TokyoCabinet: ' + (TokyoCabinet::VERSION rescue '-'),
31 'Daybreak: ' + (Daybreak::VERSION rescue '-'),
37 TYPES = [:bdb, :tc, :dbm, :daybreak, :auto]
39 :profile => '~/.rbot',
41 :dbfile => './%s.rbot' % DateTime.now.strftime('export_%Y-%m-%d_%H%M%S'),
44 opt_parser = OptionParser.new do |opt|
45 opt.banner = 'Usage: rbotdb COMMAND [OPTIONS]'
47 opt.separator 'Commands:'
48 opt.separator ' export: store rbot registry platform-independently in a file.'
49 opt.separator ' import: restore rbot registry from such a file.'
51 opt.separator 'Options:'
53 opt.on('-p', '--profile [PROFILE]', 'rbot profile directory. Defaults to: %s.' % options[:profile]) do |profile|
54 options[:profile] = profile
57 opt.on('-r', '--registry [REGISTRY]', 'registry-path to read/write, Optional, defaults to: <PROFILE>/registry_<TYPE>.') do |profile|
58 options[:registry] = profile
61 opt.on('-f', '--file [DBFILE]', 'cross-platform file to export to/import from. Defaults to: %s.' % options[:dbfile]) do |dbfile|
62 options[:dbfile] = dbfile
65 opt.on('-t', '--type TYPE', TYPES, 'format to export/import. Values: %s. Defaults to %s.' % [TYPES.join(', '), options[:type]]) do |type|
73 def initialize(profile, type, registry)
74 @profile = File.expand_path profile
77 puts 'Using type=%s profile=%s registry=%s' % [@type, @profile, @registry.inspect]
80 # returns a hash with the complete registry data
83 puts 'Found registry types: bdb=%d tc=%d dbm=%d daybreak=%d' % [
84 listings[:bdb].length, listings[:tc].length,
85 listings[:dbm].length, listings[:daybreak].length
88 @type = :bdb if listings[:bdb].length > 0
89 @type = :tc if listings[:tc].length > 0
90 @type = :dbm if listings[:dbm].length > 0
91 @type = :daybreak if listings[:daybreak].length > 0
93 if @type == :auto or listings[@type].empty?
94 puts 'No suitable registry found!'
97 puts 'Using registry type: %s' % @type
102 print "~Reading... (this might take a moment)\r"
105 listing.each do |file|
107 data[file.key] = case @type
117 count += data[file.key].length
119 puts 'ERROR: <%s> %s' % [$!.class, $!]
121 puts 'Keep in mind that, even minor version differences of'
122 puts 'Barkeley DB or Tokyocabinet make files unreadable. Use this'
123 puts 'script on the exact same platform rbot was running!'
127 puts 'Read %d registry files, with %d entries.' % [data.length, count]
133 db = BDB::Hash.open(file.abs, nil, 'r')
134 db.each do |key, value|
143 db = TokyoCabinet::BDB.new
144 db.open(file.abs, TokyoCabinet::BDB::OREADER)
145 db.each do |key, value|
153 db = DBM.open(file.abs.gsub(/\.[^\.]+$/,''), 0666, DBM::READER)
159 def read_daybreak(file)
161 db = Daybreak::DB.new(file.abs)
162 db.each do |key, value|
169 # searches in profile directory for existing registry formats
172 :bdb => list(get_registry, '*.db'),
173 :tc => list(get_registry('_tc'), '*.tdb'),
174 :dbm => list(get_registry('_dbm'), '*.*'),
175 :daybreak => list(get_registry('_daybreak'), '*.db'),
179 def get_registry(suffix='')
181 File.expand_path(@registry)
183 File.join(@profile, 'registry'+suffix)
188 def initialize(folder, name)
191 @key = name.gsub(/\.[^\.]+$/,'')
193 attr_reader :folder, :name, :key
195 File.expand_path(File.join(@folder, @name))
202 def list(folder, ext='*.db')
203 return [] if not File.directory? folder
205 Dir.glob(File.join('**', ext)).map do |name|
206 RegistryFile.new(folder, name) if File.exists?(name)
213 def initialize(profile, type, registry)
214 @profile = File.expand_path profile
215 @registry = registry ? File.expand_path(registry) : nil
216 @type = (type == :auto) ? :dbm : type
217 puts 'Using type=%s profile=%s' % [@type, @profile]
221 puts 'Using registry type: %s' % @type
222 folder = create_folder
223 print "~Importing... (this might take a moment)\r"
224 data.each do |file, hash|
225 file = File.join(folder, file)
229 write_dbm(file, hash)
231 write_daybreak(file, hash)
234 puts 'Import successful! '
237 def write_dbm(file, data)
238 db = DBM.open(file, 0666, DBM::WRCREAT)
239 data.each_pair do |key, value|
245 def write_daybreak(file, data)
246 db = Daybreak::DB.new(file + '.db')
247 data.each_pair do |key, value|
257 folder = File.join(@profile, 'registry_%s' % [@type.to_s])
259 Dir.mkdir(folder) unless File.directory?(folder)
260 if File.directory?(folder) and Dir.glob(File.join(folder, '**')).select{|f|File.file? f}.length>0
261 puts 'ERROR: Unable to import!'
262 puts 'Import folder exists and is not empty: ' + folder
268 # used to create subregistry folders
269 def create_subdir(path)
270 dirs = File.dirname(path).split('/')
271 dirs.length.times { |i|
272 dir = dirs[0,i+1].join("/")+"/"
273 unless File.exist?(dir)
284 if File.exists? options[:dbfile]
285 puts 'Export file already exists.'
289 reg = ExportRegistry.new(options[:profile], options[:type], options[:registry])
294 File.open(options[:dbfile], 'w') do |f|
295 f.write(Marshal.dump(data))
297 puts 'Written registry to ' + options[:dbfile]
301 unless File.exists? options[:dbfile]
302 puts 'Import file does not exist.'
306 reg = ImportRegistry.new(options[:profile], options[:type], options[:registry])
307 data = Marshal.load File.read(options[:dbfile])
309 puts 'Read %d registry files from import file.' % data.length