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
25 begin; require 'sqlite3'; rescue Exception; end
27 puts 'RBot Registry Backup/Restore/Migrate'
28 puts '[%s]' % ['Ruby: ' + RUBY_VERSION,
29 'DBM: ' + (DBM::VERSION rescue '-'),
30 'BDB: ' + (BDB::VERSION rescue '-'),
31 'TokyoCabinet: ' + (TokyoCabinet::VERSION rescue '-'),
32 'Daybreak: ' + (Daybreak::VERSION rescue '-'),
33 'SQLite: ' + (SQLite3::VERSION rescue '-'),
39 TYPES = [:bdb, :tc, :dbm, :daybreak, :sqlite]
41 :profile => '~/.rbot',
43 :dbfile => './%s.rbot' % DateTime.now.strftime('export_%Y-%m-%d_%H%M%S'),
46 opt_parser = OptionParser.new do |opt|
47 opt.banner = 'Usage: rbotdb COMMAND [OPTIONS]'
49 opt.separator 'Commands:'
50 opt.separator ' export: store rbot registry platform-independently in a file.'
51 opt.separator ' import: restore rbot registry from such a file.'
53 opt.separator 'Options:'
55 opt.on('-t', '--type TYPE', TYPES, 'format to export/import. Values: %s.' % [TYPES.join(', ')]) do |type|
59 opt.on('-p', '--profile [PROFILE]', 'rbot profile directory. Defaults to: %s.' % options[:profile]) do |profile|
60 options[:profile] = profile
63 opt.on('-r', '--registry [REGISTRY]', 'registry-path to read/write, Optional, defaults to: <PROFILE>/registry_<TYPE>.') do |profile|
64 options[:registry] = profile
67 opt.on('-f', '--file [DBFILE]', 'cross-platform file to export to/import from. Defaults to: %s.' % options[:dbfile]) do |dbfile|
68 options[:dbfile] = dbfile
75 def initialize(profile, type, registry)
76 @profile = File.expand_path profile
79 puts 'Using type=%s profile=%s registry=%s' % [@type, @profile, @registry.inspect]
82 # returns a hash with the complete registry data
86 puts 'Found registry types: bdb=%d tc=%d dbm=%d daybreak=%d sqlite=%d' % [
87 listings[:bdb].length, listings[:tc].length,
88 listings[:dbm].length, listings[:daybreak].length, listings[:sqlite].length
90 if listings[@type].empty?
91 puts 'No suitable registry found!'
94 puts 'Using registry type: %s' % @type
99 print "~Reading... (this might take a moment)\r"
102 listing.each do |file|
104 data[file.key] = case @type
116 count += data[file.key].length
118 puts 'ERROR: <%s> %s' % [$!.class, $!]
120 puts 'Keep in mind that, even minor version differences of'
121 puts 'Barkeley DB or Tokyocabinet make files unreadable. Use this'
122 puts 'script on the exact same platform rbot was running!'
126 puts 'Read %d registry files, with %d entries.' % [data.length, count]
132 db = BDB::Hash.open(file.abs, nil, 'r')
133 db.each do |key, value|
142 db = TokyoCabinet::BDB.new
143 db.open(file.abs, TokyoCabinet::BDB::OREADER)
144 db.each do |key, value|
152 db = DBM.open(file.abs.gsub(/\.[^\.]+$/,''), 0666, DBM::READER)
158 def read_daybreak(file)
160 db = Daybreak::DB.new(file.abs)
161 db.each do |key, value|
168 def read_sqlite(file)
170 db = SQLite3::Database.new(file.abs)
171 res = db.execute('SELECT key, value FROM data')
180 # searches in profile directory for existing registry formats
183 :bdb => list(get_registry, '*.db'),
184 :tc => list(get_registry('_tc'), '*.tdb'),
185 :dbm => list(get_registry('_dbm'), '*.*'),
186 :daybreak => list(get_registry('_daybreak'), '*.db'),
187 :sqlite => list(get_registry('_sqlite'), '*.db'),
191 def get_registry(suffix='')
193 File.expand_path(@registry)
195 File.join(@profile, 'registry'+suffix)
200 def initialize(folder, name)
203 @key = name.gsub(/\.[^\.]+$/,'')
205 attr_reader :folder, :name, :key
207 File.expand_path(File.join(@folder, @name))
214 def list(folder, ext='*.db')
215 return [] if not File.directory? folder
217 Dir.glob(File.join('**', ext)).map do |name|
218 RegistryFile.new(folder, name) if File.exists?(name)
225 def initialize(profile, type, registry)
226 @profile = File.expand_path profile
227 @registry = registry ? File.expand_path(registry) : nil
229 puts 'Using type=%s profile=%s' % [@type, @profile]
233 puts 'Using registry type: %s' % @type
234 folder = create_folder
235 print "~Importing... (this might take a moment)\r"
236 data.each do |file, hash|
237 file = File.join(folder, file)
241 write_dbm(file, hash)
245 write_daybreak(file, hash)
247 write_sqlite(file, hash)
250 puts 'Import successful! '
253 def write_dbm(file, data)
254 db = DBM.open(file, 0666, DBM::WRCREAT)
255 data.each_pair do |key, value|
261 def write_tc(file, data)
262 db = TokyoCabinet::BDB.new
263 db.open(file + '.tdb',
264 TokyoCabinet::BDB::OREADER |
265 TokyoCabinet::BDB::OCREAT |
266 TokyoCabinet::BDB::OWRITER)
267 data.each_pair do |key, value|
274 def write_daybreak(file, data)
275 db = Daybreak::DB.new(file + '.db')
276 data.each_pair do |key, value|
282 def write_sqlite(file, data)
283 db = SQLite3::Database.new(file + '.db')
284 db.execute('CREATE TABLE data (key string, value blob)')
285 data.each_pair do |key, value|
286 db.execute('INSERT INTO data VALUES (?, ?)',
296 folder = File.join(@profile, 'registry_%s' % [@type.to_s])
298 Dir.mkdir(folder) unless File.directory?(folder)
299 if File.directory?(folder) and Dir.glob(File.join(folder, '**')).select{|f|File.file? f}.length>0
300 puts 'ERROR: Unable to import!'
301 puts 'Import folder exists and is not empty: ' + folder
307 # used to create subregistry folders
308 def create_subdir(path)
309 dirs = File.dirname(path).split('/')
310 dirs.length.times { |i|
311 dir = dirs[0,i+1].join("/")+"/"
312 unless File.exist?(dir)
320 if options[:type].nil?
321 puts 'Missing Argument: -t [type]'
327 if File.exists? options[:dbfile]
328 puts 'Export file already exists.'
332 reg = ExportRegistry.new(options[:profile], options[:type], options[:registry])
337 File.open(options[:dbfile], 'w') do |f|
338 f.write(Marshal.dump(data))
340 puts 'Written registry to ' + options[:dbfile]
344 unless File.exists? options[:dbfile]
345 puts 'Import file does not exist.'
349 reg = ImportRegistry.new(options[:profile], options[:type], options[:registry])
350 data = Marshal.load File.read(options[:dbfile])
352 puts 'Read %d registry files from import file.' % data.length