create_dbs.pl
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:16k
- #!/usr/bin/perl
- use Getopt::Long;
- use strict;
- $| = 1;
- my $database_user = '';
- my $database_user_password = '';
- my $mysql_flag = 0;
- my $mysql_new_flag = 0;
- my $mysql_command = '';
- my $mysql_noshow = 0;
- my $mysqlshow_command = '';
- my $session_flag = 0;
- my $user_flag = 0;
- my $domain_flag = 0;
- my $admin_privileges_flag = 0;
- my $mail_settings_flag = 0;
- my $customer_flag = 0;
- my $user_settings_flag = 0;
- my $SQL_BASE = './';
- if ( $ARGV[ 0 ] eq '' ) {
- die <<"USAGE";
- create_dbs.pl [options]
- --user=username - username to be used
- --password=password - password to be used
- --mysql - mysql database
- --mysql_command=path - path to mysql command
- --mysql_noshow - disable the mysqlshow command, will
- use alternate method for checking
- database/table creation
- --mysqlshow_command=path - path to mysqlshow command
- --mysql_new - turns on support for mysql versions
- greater than 3.22.27 otherwise it
- will fail
- --session_db - create a session database
- --user_db - create a user database
- --domain_db - create a domain database
- --admin_privileges_db - create a admin privileges database
- --mail_settings_db - create a mail settings database
- --customer_db - create a customer database
- --user_settings_db - create a user settings database
- --sql_base=path - base directory to the sql
- USAGE
- }
- my $foo;
- my $result = GetOptions(
- 'user=s', $database_user,
- 'password=s', $database_user_password,
- 'mysql', $mysql_flag,
- 'mysql_new', $mysql_new_flag,
- 'mysql_command=s', $mysql_command,
- 'mysql_noshow', $mysql_noshow,
- 'mysqlshow_command=s', $mysqlshow_command,
- 'session_db', $session_flag,
- 'user_db', $user_flag,
- 'domain_db', $domain_flag,
- 'admin_privileges_db', $admin_privileges_flag,
- 'customer_db', $customer_flag,
- 'sql_base=s', $SQL_BASE,
- 'mail_settings_db', $mail_settings_flag,
- 'user_settings_db', $user_settings_flag,
- );
- print "BASE : " . $SQL_BASE . "n";
- if ( $mysql_command eq '' ) {
- $mysql_command = find_command('mysql');
- }
- if ( $mysqlshow_command eq '' ) {
- $mysqlshow_command = find_command('mysqlshow');
- }
- print "Option confirmation : n";
- display_option( 'Database Username ', $database_user );
- display_option( 'Database Password ', $database_user_password );
- if ( $mysql_flag ) {
- display_option( 'Mysql ', $mysql_flag );
- display_option( 'Mysql Command ', $mysql_command );
- display_option( 'Mysqlshow Command ', $mysqlshow_command );
- }
- display_option( 'Sessions ', $session_flag );
- display_option( 'Users ', $user_flag );
- display_option( 'User Settings ', $user_settings_flag );
- display_option( 'Domains ', $domain_flag );
- display_option( 'Admin Privileges ', $admin_privileges_flag );
- display_option( 'Mail Settings ', $mail_settings_flag );
- display_option( 'Customer ', $customer_flag );
- print "n";
- if ( $mysql_flag ) {
- if ( $mysql_command eq '' ) {
- die "The mysql command is required : n" .
- "--mysql-command=path_to_mysql is a overriden";
- }
- if ( $mysqlshow_command eq '' ) {
- die "The mysqlshow command is required :n" .
- "--mysqlshow-command=path_to_mysqlshow is a overriden";
- }
- if ( $session_flag ) {
- create_mysql_session_dbs();
- }
- if ( $user_flag ) {
- create_mysql_user_dbs();
- }
- if ( $user_settings_flag ) {
- create_mysql_user_settings_dbs();
- }
- if ( $domain_flag ) {
- create_mysql_domain_dbs();
- }
- if ( $admin_privileges_flag ) {
- create_mysql_admin_privileges_dbs();
- }
- if ( $mail_settings_flag ) {
- create_mysql_mail_settings_dbs();
- }
- if ( $customer_flag ) {
- create_mysql_customer_dbs();
- }
- }
- exit();
- sub print_log {
- my $mesg = shift;
- open( LOG_CREATE, ">> create_log.log" );
- print LOG_CREATE . "*" x 40 . "n";
- print LOG_CREATE . $mesg . "n";
- close( LOG_CREATE );
- open( LOG_CREATE, ">> create_log.stderr" );
- print LOG_CREATE . "*" x 40 . "n";
- print LOG_CREATE . $mesg . "n";
- close( LOG_CREATE );
- }
- sub display_option {
- my $option_name = shift;
- my $option_value = shift;
- print $option_name . ' - ' . $option_value . "n";
- }
- sub find_command {
- my $target_command = shift;
- my @path_elems = ();
- my $path_elem = '';
- @path_elems = split( /:/, $ENV{ "PATH" } );
- foreach $path_elem ( @path_elems ) {
- my $test_item = '';
- $test_item = $path_elem . '/' . $target_command;
- if ( -x $test_item ) {
- return $test_item;
- }
- }
- }
- sub get_mysql_dbs {
- my @mysql_dbs = ();
- # Simon/24may2000 - mysqlshow is half-borked on most
- # of my systems, so this is an alternate method
- # to retrieve databases
- #
- # The real problem occurs in "get_mysql_dbs_tables",
- # see comments there for a better explanation.
- if ( $mysql_noshow )
- {
- open( MYSQL_SHOW,
- $mysql_command . ' ' .
- '--user=' . $database_user . ' ' .
- '--password=' . $database_user_password . ' ' .
- '--batch --execute='show databases' |'
- ) or die "$!n";
- # Eat first line, yummy.
- <MYSQL_SHOW>;
- while( <MYSQL_SHOW> ) {
- my $input_line = $_;
- chomp($input_line);
- push( @mysql_dbs, $input_line );
- }
- }
- else
- {
- open( MYSQL_SHOW,
- $mysqlshow_command . ' ' .
- '--user=' . $database_user . ' ' .
- '--password=' . $database_user_password .
- ' |'
- ) or die "$!n";
- while( <MYSQL_SHOW> ) {
- my $input_line = $_;
- if ( $input_line =~ /+/ ) { next; }
- if ( $input_line =~ /Databases/ ) { next; }
- if ( $input_line =~ /|s*(.*)s*|/ ) {
- my $target = $1;
- while( $target =~ / $/ ) {
- $target =~ s/ $//g;
- }
- push( @mysql_dbs, $target );
- }
- }
- }
- close( MYSQL_SHOW );
- return @mysql_dbs;
- }
- sub get_mysql_dbs_tables {
- my $dbs = shift;
- my @mysql_tables = ();
- # Simon/24may2000 -
- # When mysqlshow --user=blah --password=blah database_name
- # is executed on my system, sometimes, it will simply
- # display the database name, rather than the tables it contains.
- # Obviously, this isn't ideal, so I've added a couple of extra
- # routines to get around it.
- $mysql_noshow = detect_bad_mysqlshow();
- # Simon/24may2000 - mysqlshow is half-borked on most
- # of my systems, so this is an alternate method
- # to retrieve table listings
- if ( $mysql_noshow )
- {
- open( MYSQL_SHOW,
- $mysql_command . ' ' .
- '--user=' . $database_user . ' ' .
- '--password=' . $database_user_password . ' ' .
- '--batch --execute "show tables"' . ' ' .
- $dbs . ' |'
- ) or die "$!n";
- <MYSQL_SHOW>;
- while( <MYSQL_SHOW> ) {
- my $input_line = $_;
- chomp( $input_line );
- push( @mysql_tables, $input_line );
- }
- close( MYSQL_SHOW );
- }
- else
- {
- my $command = $mysqlshow_command . ' ' .
- '--user=' . $database_user . ' ' .
- '--password=' . $database_user_password . ' ' .
- $dbs ;
- if ( $mysql_new_flag == 1 ) {
- # % Doesn't work on all versions of mysql
- # The percent sign is a table wild card . ' % ';
- $command .= ' % ';
- }
- #print_log( $command );
- open( MYSQL_SHOW, $command . ' |') or die "$!n";
- while( <MYSQL_SHOW> ) {
- #print_log( 'OUTPUT: ' . $_ );
- my $input_line = $_;
- if ( $input_line =~ /+/ ) { next; }
- if ( $input_line =~ /Tables/ ) { next; }
- if ( $input_line =~ /|s*(.*)s*|/ ) {
- my $target = $1;
- while( $target =~ / $/ ) {
- $target =~ s/ $//g;
- }
- push( @mysql_tables, $target );
- }
- }
- }
- close( MYSQL_SHOW );
- return @mysql_tables;
- }
- sub get_mysql_database_existence {
- my $target_dbs = shift;
- my @mysql_dbs = get_mysql_dbs();
- my $item = '';
- foreach $item ( @mysql_dbs ) {
- #print $item . "***n";
- if ( $item eq $target_dbs ) {
- # dbs is already there...
- return 1;
- }
- }
- return 0;
- }
- sub get_mysql_table_existence {
- my $target_dbs = shift;
- my $target_table = shift;
- my @mysql_tables = get_mysql_dbs_tables( $target_dbs );
- my $item = '';
- foreach $item ( @mysql_tables ) {
- #print $item . "***n";
- if ( $item eq $target_table ) {
- # dbs is already there...
- return 1;
- }
- }
- return 0;
- }
- sub run_mysql_script {
- my $script = shift;
- my $target_dbs = shift;
- my @command = ();
- @command = (
- $mysql_command,
- '--user=' . $database_user,
- '--password=' . $database_user_password
- );
- if ( $target_dbs ne "" ) { push( @command, $target_dbs ); }
- push( @command, ">> create_log.log 2>> create_log.stderr" );
- push( @command, "< $script" );
- system( join( ' ', @command ) );
- }
- sub create_mysql_dbs {
- my $target_dbs = shift;
- my $target_dbs_create = shift;
- my $dbs_tables = shift;
- my $table_cheks = shift;
- my $db_there = 0;
- $db_there = get_mysql_database_existence( $target_dbs );
- if ( $db_there == 1 ) {
- die
- "There seems to be a issue : $target_dbs is already theren" .
- "and since i don't know how to handle this i'm exitingn";
- }
- run_mysql_script( $target_dbs_create );
- $db_there = get_mysql_database_existence( $target_dbs );
- if ( $db_there == 1 ) {
- print "$target_dbs - Database was created!n";
- } else {
- die
- "Hmm the database $target_dbs is still not there after i tried ton" .
- "create it, i'm guessing that there is something afoot!n";
- }
- my $table = '';
- foreach $table ( @$dbs_tables ) {
- if ( -f 'pre_' . $table ) {
- run_mysql_script( 'pre_' . $table, $target_dbs );
- }
- run_mysql_script( $table, $target_dbs );
- if ( -f 'post_' . $table ) {
- run_mysql_script( 'post_' . $table, $target_dbs );
- }
- }
- foreach $table ( @$table_cheks ) {
- if ( get_mysql_table_existence( $target_dbs, $table ) ) {
- # Table is there
- print "tTable : $table checked out good tt[ OK ]n";
- } else {
- die "tTable : $table checked DID NOT out good t[ ERROR ]n";
- }
- }
- print "$target_dbs - " .
- "Okay looks like we have a database to use now, have fun!nn";
- }
- # Simon/23may2000 -
- # Adding get_mysql_version to retrieve mysql's version number, so that
- # we can build in a bit of backward compatibility.
- sub detect_bad_mysqlshow {
- my $badversion = $mysql_noshow;
- if ( ! $mysql_noshow )
- {
- print "tDetected faulty 'mysqlshow', using alternate routine.t[ WARNING ]n";
- open( BAD_MYSQLVIEW,
- $mysqlshow_command . ' ' .
- '--user=' . $database_user . ' ' .
- '--password=' . $database_user_password .
- ' |'
- ) or die "$!n";
- while( <BAD_MYSQLVIEW> ) {
- my $input_line = $_;
- if ( $input_line =~ /+/ ) { next; }
- if ( $input_line =~ /Databases/ ) {
- $badversion = 1;
- }
- }
- }
- return $badversion;
- }
- sub create_mysql_session_dbs {
- my $target_dbs = 'prometheus_sessions';
- my $target_dbs_create = $SQL_BASE .
- '/database/mysql/Sessions/create_prometheus_sessions.sql';
- my @dbs_tables = (
- $SQL_BASE . '/table/mysql/Sessions/sessions_table.sql',
- $SQL_BASE . '/table/mysql/Sessions/sessions_data_table.sql',
- );
- my @table_cheks = (
- 'sessions_table',
- 'sessions_data_table',
- );
- create_mysql_dbs(
- $target_dbs,
- $target_dbs_create,
- @dbs_tables,
- @table_cheks,
- );
- }
- sub create_mysql_user_dbs {
- my $target_dbs = 'prometheus_users';
- my $target_dbs_create =
- $SQL_BASE . '/database/mysql/User/create_prometheus_users.sql';
- my @dbs_tables = (
- $SQL_BASE . '/table/mysql/User/user_table.sql',
- $SQL_BASE . '/table/mysql/User/user_theme_table.sql',
- );
- my @table_cheks = (
- 'user_table',
- 'user_theme_table',
- );
- create_mysql_dbs(
- $target_dbs,
- $target_dbs_create,
- @dbs_tables,
- @table_cheks
- );
- }
- sub create_mysql_user_settings_dbs {
- my $target_dbs = 'prometheus_settings';
- my $target_dbs_create =
- $SQL_BASE . '/database/mysql/PrometheusSettings/create_prometheus_settings.sql';
- my @dbs_tables = (
- $SQL_BASE . '/table/mysql/PrometheusSettings/user_theme_table.sql',
- #$SQL_BASE . '/table/mysql/PrometheusSettings/user_language_table.sql',
- );
- my @table_cheks = (
- 'user_theme_table',
- #'user_language_table',
- );
- create_mysql_dbs(
- $target_dbs,
- $target_dbs_create,
- @dbs_tables,
- @table_cheks
- );
- }
- sub create_mysql_domain_dbs {
- my $target_dbs = 'prometheus_domains';
- my $target_dbs_create =
- $SQL_BASE . '/database/mysql/Domain/create_prometheus_domains.sql';
- my @dbs_tables = (
- $SQL_BASE . '/table/mysql/Domain/domain_table.sql',
- );
- my @table_cheks = (
- 'domain_table',
- );
- create_mysql_dbs(
- $target_dbs,
- $target_dbs_create,
- @dbs_tables,
- @table_cheks
- );
- }
- sub create_mysql_admin_privileges_dbs {
- my $target_dbs = 'prometheus_privileges';
- my $target_dbs_create =
- $SQL_BASE . '/database/mysql/AdminPrivileges/create_prometheus_privileges.sql';
- my @dbs_tables = (
- $SQL_BASE . '/table/mysql/AdminPrivileges/admin_privileges_table.sql',
- );
- my @table_cheks = (
- 'admin_privileges_table',
- );
- create_mysql_dbs(
- $target_dbs,
- $target_dbs_create,
- @dbs_tables,
- @table_cheks
- );
- }
- sub create_mysql_mail_settings_dbs {
- my $target_dbs = 'mail_settings';
- my $target_dbs_create =
- $SQL_BASE . '/database/mysql/MailServerSettings/create_mail_settings.sql';
- my @dbs_tables = (
- $SQL_BASE . '/table/mysql/MailServerSettings/server_settings_table.sql',
- $SQL_BASE . '/table/mysql/MailServerSettings/user_signatures_table.sql',
- );
- my @table_cheks = (
- 'server_settings_table',
- 'user_signatures_table',
- );
- create_mysql_dbs(
- $target_dbs,
- $target_dbs_create,
- @dbs_tables,
- @table_cheks
- );
- }
- sub create_mysql_customer_dbs {
- my $target_dbs = 'prometheus_customers';
- my $target_dbs_create =
- $SQL_BASE . '/database/mysql/CustomerInformation/create_prometheus_customers.sql';
- my @dbs_tables = (
- $SQL_BASE . '/table/mysql/CustomerInformation/address_table.sql',
- $SQL_BASE . '/table/mysql/CustomerInformation/ccard_table.sql',
- $SQL_BASE . '/table/mysql/CustomerInformation/customer_notes.sql',
- $SQL_BASE . '/table/mysql/CustomerInformation/customer_services_table.sql',
- $SQL_BASE . '/table/mysql/CustomerInformation/personal_table.sql',
- $SQL_BASE . '/table/mysql/CustomerInformation/phone_num_table.sql',
- $SQL_BASE . '/table/mysql/CustomerInformation/service_table.sql',
- $SQL_BASE . '/table/mysql/CustomerInformation/services_completed_table.sql'
- );
- my @table_cheks = (
- 'address_table',
- 'ccard_table',
- 'customer_notes',
- 'customer_services_table',
- 'personal_table',
- 'phone_num_table',
- 'service_type_table',
- 'services_completed_table'
- );
- create_mysql_dbs(
- $target_dbs,
- $target_dbs_create,
- @dbs_tables,
- @table_cheks
- );
- }