#!/usr/bin/env ruby # msqladmin.rb written by itacchi (DATE Ken) 2000- # That's sample program for Ruby/mSQL. # msqladmin.rb is a `msqladmin' clone. require 'getopts' require 'msql' class OptionError < StandardError; end class MsqlAdmin def initialize(host) @client = Msql::connect(host) @message = "" end attr_reader :message def copy(srcDB, destDB) @client.copy_db srcDB, destDB @message = "Database \"#{srcDB}\" copied to \"#{destDB}\".\n" end def create(databaseName) @client.create_db databaseName @message = "Database \"#{databaseName}\" created.\n" end def drop(databaseName) @client.drop_db databaseName @message = "Database \"#{databaseName}\" dropped\n" end def move(srcDB, destDB) @client.move_db srcDB, destDB @message = "Database \"#{srcDB}\" moved to \"#{destDB}\".\n" end def reload @client.reload_acls end def shutdown @client.shutdown end def stats @client.get_server_stats end # can't get a value from some constants, so that this method is incomplete... def version general = Msql.get_config 'general' system = Msql.get_config 'system' @message = "" @message << "\nVersion Details :-\n\n" @message << "\tmsqladmin version \t#{Msql::SERVER_VERSION}\n" @message << "\tmSQL server version \t#{@client.get_server_info}\n" @message << "\tmSQL protocol version \t#{@client.get_protocol_info}\n" @message << "\tmSQL connection \t#{@client.get_host_info}\n" @message << "\tTarget platform \t#{Msql::TARGET}\n\n" @message << "Configuration Details :-\n\n" @message << "\tDefault config file\t#{Msql::INST_DIR}/msql.conf\n" @message << "\tTCP socket \t#{general['TCP_Port']}\n" @message << "\tUNIX socket \t#{general['UNIX_Port']}\n" @message << "\tmSQL user \t#{general['mSQL_User']}\n" @message << "\tAdmin user \t#{general['Admin_User']}\n" @message << "\tInstall directory \t#{general['Inst_Dir']}\n" @message << "\tPID file location \t#{general['Pid_File']}\n" @message << "\tMemory Sync Timer \t#{system['Msync_Timer']}\n" @message << "\tHostname Lookup \t#{system['Host_Lookup']}\n" @message << "\n\n" end end # print usage def usage print "\n\nusage : #{$0} [-h host] [-f conf] [-q] \n\n" print "where command =" print "\t drop DatabaseName\n" print "\t\t create DatabaseName\n" print "\t\t copy FromDB ToDB\n" print "\t\t move FromDB ToDB\n" print "\t\t shutdown\n" print "\t\t reload\n" print "\t\t version\n" print "\t\t stats\n" print "\n -q\tQuiet mode. No verification of commands.\n\n" end # --- main process # parse option opts = getopts("q", "h:", "f:") # parse command and some parameters command = ARGV.shift param1 = ARGV.shift param2 = ARGV.shift host = $OPT_h conf = $OPT_f quiet = $OPT_q begin # illegal option or no command raise OptionError unless opts and command # too many args... raise OptionError if ARGV.size != 0 # When option -f exists, load specified config file. if conf Msql::load_config_file(conf) end # create admin instance admin = MsqlAdmin.new host # execute specified command case command when "create" raise OptionError if not param1 or param2 admin.create param1 when "drop" raise OptionError if not param1 or param2 unless quiet print "\n\nDropping the database is potentially a very bad " print "thing to do.\nAny data stored in the database will be" print " destroyed.\n\nDo you really want to drop the " print "\"#{param1}\" " print "database? [Y/N] " STDOUT.flush buf = STDIN.gets buf.chop! if (buf != 'y' and buf != 'Y') print "\n\nOK, aborting database drop!\n\n" break end end admin.drop param1 when "copy" raise OptionError unless param1 and param2 admin.copy param1, param2 when "move" raise OptionError unless param1 and param2 admin.move param1, param2 when "shutdown" raise OptionError if param1 admin.shutdown when "reload" raise OptionError if param1 admin.reload when "version" raise OptionError if param1 admin.version when "stats" raise OptionError if param1 print "\nServer Statistics\n" print "-----------------\n\n" admin.stats print "\n\n" else raise OptionError end print admin.message rescue OptionError usage end