Ir ao conteúdo
  • Cadastre-se

Esse arquivo é de mysql ou script perl?


Posts recomendados

Esse codigo pertence a um arquivo .pl e queria sabe que aplicativo se executa, eu acho que é mysql mas nao tenho certeza disto ou é Perl Script?
Link do procedimento que preciso fazer:
https://www.mythtv.org/wiki/Database_Backup_and_Restore
Codigo abaixo:
 

#!/usr/bin/perl -w
#
# mythconverg_restore.pl
#
# Restores a backup of the MythTV database.
#
# For details, see:
# mythconverg_restore.pl --help

# Includes
use Getopt::Long;
use File::Temp qw/ tempfile /;

# Script info
$NAME = 'MythTV Database Restore Script';
$VERSION = '1.0.18';

my @files = <$backup_conf{'directory'}/$backup_conf{'filename'}*>;
@files = grep(!/.*mythconverg_(backup|restore).*\.pl$/, @files);
my $num_files = @files;
if ($num_files < 1)
{
verbose($verbose_level_error,
'ERROR: Unable to find any backup files in'.
' DBBackupDir and none specified.');
}
else
{
my @sorted_files = sort { lc($b) cmp lc($a) } @files;
$backup_conf{'filename'} = $sorted_files[0];
$backup_conf{'filename'} =~ s#^$backup_conf{'directory'}/?##;
verbose($verbose_level_debug,
'Using database backup file:',
"$backup_conf{'directory'}/$backup_conf{'filename'}");
}
}
if (!-e "$backup_conf{'directory'}/$backup_conf{'filename'}")
{
my $temp_filename = $backup_conf{'filename'};
# Perhaps the user specified some unnecessary path information in the
# filename (i.e. using the shell's filename completion)
$temp_filename =~ s#^.*/##;
if (-e "$backup_conf{'directory'}/$temp_filename")
{
$backup_conf{'filename'} = $temp_filename;
}
else
{
verbose($verbose_level_error,
'', 'ERROR: The specified backup file does not exist.',
"$backup_conf{'directory'}/$backup_conf{'filename'}");
die("\nInvalid backup filename, stopped");
}
}
if ((-d "$backup_conf{'directory'}/$backup_conf{'filename'}") ||
(-p "$backup_conf{'directory'}/$backup_conf{'filename'}") ||
(-S "$backup_conf{'directory'}/$backup_conf{'filename'}") ||
(-b "$backup_conf{'directory'}/$backup_conf{'filename'}") ||
(-c "$backup_conf{'directory'}/$backup_conf{'filename'}") ||
(!-s "$backup_conf{'directory'}/$backup_conf{'filename'}"))
{
verbose($verbose_level_error,
'', 'ERROR: The specified backup file is empty or is'.
' not a file.',
"$backup_conf{'directory'}/$backup_conf{'filename'}");
die("\nInvalid backup filename, stopped");
}
if (!-r "$backup_conf{'directory'}/$backup_conf{'filename'}")
{
verbose($verbose_level_error,
'', 'ERROR: The specified backup file cannot be read.');
die("\nInvalid backup filename, stopped");
}
if (!$mysql_conf{'db_name'})
{
verbose($verbose_level_debug,
'', "WARNING: DBName not specified. Using $d_db_name");
$mysql_conf{'db_name'} = $d_db_name;
}
}

sub check_config
{
verbose($verbose_level_debug,
'', 'Checking configuration.');

sub connect_to_database
{
my $use_db = shift;
my $show_errors = shift;
my $result = 1;
my $connect_string = 'dbi:mysql:database=';
if ($use_db)
{
$connect_string .= $mysql_conf{'db_name'};
}
$connect_string .= ":host=$mysql_conf{'db_host'}";
$dbh->disconnect if (defined($dbh));
$dbh = DBI->connect($connect_string,
"$mysql_conf{'db_user'}",
"$mysql_conf{'db_pass'}",
{ PrintError => 0 });
$result = 0 if (!defined($dbh));
if ($show_errors && !defined($dbh))
{
verbose($verbose_level_always,
'', 'Unable to connect to database.',
" database: $mysql_conf{'db_name'}",
" host: $mysql_conf{'db_host'}",
" username: $mysql_conf{'db_user'}"
);
if ($debug < $verbose_level_debug)
{
verbose($verbose_level_always,
'To see the password used, please re-run the script '.
'with the --verbose',
'argument.');
}
# Connection issues will only occur with improper user configuration
# Because they should be rare, output the password with --verbose
verbose($verbose_level_debug,
" password: $mysql_conf{'db_pass'}");
verbose($verbose_level_always,
'', 'Please check your configuration files to verify the'.
' database connection',
'information is correct. The files that are used to'.
' retrieve connection',
'information are prefixed with "parsing" in the "Parsing'.
' configuration files"',
'section of the --verbose output.');
verbose($verbose_level_always,
'', 'Also note that any [client] or [mysql] password'.
' specified in the MySQL options',
'file (/etc/my.cnf or /etc/mysql/my.cnf or ~/.my.cnf)'.
' will take precedence over',
'the password specified in the MythTV configuration'.
' files.');
}
return $result;
}

sub is_database_empty
{
my $result = 1;
connect_to_database(1, 1);
if (!defined($dbh))
{
verbose($verbose_level_error,
'', 'ERROR: Unable to connect to database.');
return -1;
}

if (defined($dbh))
{
my $sth = $dbh->table_info('', '', '', 'TABLE');
my $num_tables = keys %{$sth->fetchall_hashref('TABLE_NAME')};
verbose($verbose_level_debug,
'', "Found $num_tables tables in the database.");
if ($num_tables > 0)
{
if (!defined($change_hostname) && !defined($partial_restore))
{
verbose($verbose_level_debug,
'WARNING: Database not empty.');
}
$result = 0;
}
}
return $result;
}

sub create_initial_database
{
return 0 if (!$create_database && !$drop_database);

my $database_exists = (connect_to_database(1, 0) && defined($dbh));
if ($database_exists)
{
if ($drop_database && !$create_database)
{
verbose($verbose_level_error,
'', 'ERROR: Refusing to drop the database without'.
' the --create_database argument.',
'If you really want to drop the database, please '.
're-run the script and specify',
'the --create_database argument, too.');
return 2;
}
}
else
{
if (!$create_database)
{
verbose($verbose_level_error,
'', 'ERROR: The database does not exist.');
return 1;
}
}

verbose($verbose_level_debug,
'', 'Preparing initial database.');

my ($query, $sth);

if ($database_exists && $drop_database)
{
verbose($verbose_level_debug, 'Dropping database.');
connect_to_database(0, 1);
if (!defined($dbh))
{
verbose($verbose_level_error,
'', 'ERROR: Unable to connect to database.');
return -1;
}

$query = qq{DROP DATABASE $mysql_conf{'db_name'};};
$sth = $dbh->prepare($query);
if (! $sth->execute())
{
verbose($verbose_level_error,
'', 'ERROR: Unable to drop database.',
$sth->errstr);
return -2;
}
}

connect_to_database(0, 1) if (!defined($dbh));

if (!defined($dbh))
{
verbose($verbose_level_error,
'', 'ERROR: Unable to connect to database.');
return -1;
}

verbose($verbose_level_debug, 'Creating database.');
$query = qq{CREATE DATABASE $mysql_conf{'db_name'};};
$sth = $dbh->prepare($query);
if (! $sth->execute())
{
verbose($verbose_level_error,
'', 'ERROR: Unable to create database.',
$sth->errstr);
return -4;
}

verbose($verbose_level_debug, 'Setting database character set.');
$query = qq{ALTER DATABASE $mysql_conf{'db_name'}
DEFAULT CHARACTER SET latin1
COLLATE latin1_swedish_ci;};
$sth = $dbh->prepare($query);
if (! $sth->execute())
{
verbose($verbose_level_error,
'', 'ERROR: Unable to create database.',
$sth->errstr);
return -8;
}

return 0;
}

sub check_database_libs
{
# Try to load the DBI library if available (but don't require it)
BEGIN
{
our $has_dbi = 1;
eval 'use DBI;';
if ($@)
{
$has_dbi = 0;
}
}
verbose($verbose_level_debug,
'', 'DBI is not installed.') if (!$has_dbi);
# Try to load the DBD::mysql library if available (but don't # require it)
BEGIN
{
our $has_dbd = 1;
eval 'use DBD::mysql;';
if ($@)
{
$has_dbd = 0;
}
}
verbose($verbose_level_debug,
'', 'DBD::mysql is not installed.') if (!$has_dbd);
return ($has_dbi + $has_dbd);
}

sub check_database
{
my $have_database_libs = check_database_libs;
if ($have_database_libs < 2)
{
if ($create_database || $drop_database)
{
verbose($verbose_level_error,
'', 'ERROR: Unable to drop or create the initial '.
'database without Perl database',
' libraries.',
'Please ensure the Perl DBI and DBD::mysql modules'.
' are installed.');
die("\nPerl database libraries missing, stopped");
}
if ($change_hostname)
{
verbose($verbose_level_error,
'', 'ERROR: Unable to change hostname without Perl'.
' database libraries.',
'Please ensure the Perl DBI and DBD::mysql modules'.
' are installed.');
die("\nPerl database libraries missing, stopped");
}
else
{
verbose($verbose_level_debug,
'Blindly assuming your database is prepared for a'.
' restore. For better checking,',
'please ensure the Perl DBI and DBD::mysql modules'.
' are installed.');
return 1;
}
}
# DBI/DBD::mysql are available; check the DB status
verbose($verbose_level_debug,
'', 'Checking database.');
my $initial_database = create_initial_database;
if ($initial_database)
{
return 0;
}
my $database_empty = is_database_empty;
if ($database_empty == -1)
{
# Unable to connect to database
return 0;
}
if ($change_hostname)
{
if ($database_empty)
{
verbose($verbose_level_error,
'', 'ERROR: Unable to change hostname. The database'.
' is empty.',
' Please restore a backup, first, then re-run'.
' this script.');
return 0;
}
}
elsif ($partial_restore)
{
if ($database_empty)
{
verbose($verbose_level_error,
'', 'ERROR: Unable to do a partial restore. The'.
' database is empty.',
' Please run mythtv-setup, first, then re-run'.
' this script.');
return 0;
}
}
else
{
if (!$database_empty)
{
verbose($verbose_level_error,
'', 'ERROR: Unable to do a full restore. The'.
' database contains data.');
return 0;
}
}
return 1;
}

sub is_gzipped
{
# Simple magic number verification.
# This naive approach works without requiring File::MMagic or any other
# modules.
my $result = 0;
my $magic_number;
my $gzip_magic_number = pack("C*", 0x1f, 0x8b);
open(BACKUPFILE, "$backup_conf{'directory'}/$backup_conf{'filename'}")
or return $result;
binmode(BACKUPFILE);
read(BACKUPFILE, $magic_number, 2);
close(BACKUPFILE);
return ($gzip_magic_number eq $magic_number);
}

# Though it's possible to uncompress the file without writing the uncompressed
# data to a file, doing so is complicated by supporting the use of
# IO::Uncompress::Gunzip /and/ external uncompress programs. Also,
# uncompressing the file separately allows for easier and more precise error
# reporting.
sub uncompress_backup_file
{
if (($d_uncompress eq $uncompress) || ('gunzip' eq $uncompress))
{
if (!is_gzipped)
{
verbose($verbose_level_debug,
'', 'Backup file is uncompressed.');
return 0;
}
verbose($verbose_level_debug,
'', 'Backup file is compressed.');
# Try to load the IO::Uncompress::Gunzip library if available (but
# don't require it)
BEGIN
{
our $has_uncompress_gunzip = 1;
# Though this does nothing, it prevents an invalid "only used
# once" warning that occurs for users without IO::Uncompress
# installed.
undef $GunzipError;
eval 'use IO::Uncompress::Gunzip qw(gunzip $GunzipError);';
if ($@)
{
$has_uncompress_gunzip = 0;
}
}
if (!$has_uncompress_gunzip)
{
verbose($verbose_level_debug,
' - IO::Uncompress::Gunzip is not installed.');
}
else
{
verbose($verbose_level_debug,
' - Uncompressing backup file with'.
' IO::Uncompress::Gunzip.');
my ($bfh, $temp_backup_filename) = tempfile(UNLINK => 1);
my $result = gunzip(
"$backup_conf{'directory'}/$backup_conf{'filename'}" =>
$bfh);
if ((defined($result)) &&
(-f "$temp_backup_filename") &&
(-r "$temp_backup_filename") &&
(-s "$temp_backup_filename"))
{
$backup_conf{'directory'} = '';
$backup_conf{'filename'} = "$temp_backup_filename";
return 0;
}
verbose($verbose_level_error,
" ERROR: $GunzipError");
}
}
else
{
verbose($verbose_level_debug,
'', 'Unrecognized uncompress program.'.
' Assuming backup file is compressed.',
' - If the file is not compressed, please do not specify'.
' a custom uncompress',
' program name.');
}
# Try to uncompress the file with the uncompress binary.
# With the approach, the original backup file will be uncompressed and
# left uncompressed.
my $safe_uncompress = $uncompress;
$safe_uncompress =~ s/'/'\\''/sg;
verbose($verbose_level_debug,
" - Uncompressing backup file with $uncompress.",
' The original backup file will be left uncompressed.'.
' Please recompress,',
' if desired.');
my $backup_path = "$backup_conf{'directory'}/$backup_conf{'filename'}";
$backup_path =~ s/'/'\\''/sg;
my $output = `'$safe_uncompress' '$backup_path' 2>&1`;
my $exit = $? >> 8;
verbose($verbose_level_debug,
'', "$uncompress exited with status: $exit");
if ($exit)
{
verbose($verbose_level_debug,
"$uncompress output:", $output);
}
else
{
if (!-r "$backup_conf{'directory'}/$backup_conf{'filename'}")
{
# Assume the final extension was removed by uncompressing.
$backup_conf{'filename'} =~ s/\.\w+$//;
if (!-r "$backup_conf{'directory'}/$backup_conf{'filename'}")
{
verbose($verbose_level_error,
'',
'ERROR: Unable to find uncompressed backup file.');
die("\nInvalid backup filename, stopped");
}
}
}
return $exit;
}

sub do_hostname_change
{
my $exit = 0;
if (!$new_hostname)
{
$exit++;
verbose($verbose_level_error,
'', 'ERROR: Cannot change hostname without --new_hostname'.
' value.');
}
if (!$old_hostname)
{
$exit++;
verbose($verbose_level_error,
'', 'ERROR: Cannot change hostname without --old_hostname'.
' value.');
}
if ($exit > 0)
{
die("\nInvalid --old/--new_hostname value(s) for".
' --change_hostname, stopped');
}
# Get a list of all tables in the DB.
if (defined($dbh))
{
my $num_tables = 0;
my $sth_tables;
my $table_cat;
my $table_schema;
my $table_name;
my $sth_columns;
my $column_name;
my $query;
my $sth_update;
my $result;

$sth_tables = $dbh->table_info('', '', '', 'TABLE');
while (my $table = $sth_tables->fetchrow_hashref)
{
# Loop over all tables in the DB, checking for a hostname column.
$num_tables++;
$table_cat = $table->{'TABLE_CAT'};
$table_schema = $table->{'TABLE_SCHEM'};
$table_name = $table->{'TABLE_NAME'};
$sth_columns = $dbh->column_info($table_cat, $table_schema,
$table_name, '%');
while (my $column = $sth_columns->fetchrow_hashref)
{
# If a hostname column exists, change its value.
$column_name = $column->{'COLUMN_NAME'};
if (($column_name eq 'hostname') ||
($column_name eq 'host'))
{
verbose($verbose_level_debug,
"Found '$column_name' column in $table_name.");
$query = "UPDATE $table_name SET $column_name = ?".
" WHERE $column_name = ?";
$sth_update = $dbh->prepare($query);
$sth_update->bind_param(1, $new_hostname);
$sth_update->bind_param(2, $old_hostname);
$result = $sth_update->execute;
if (!defined($result))
{
verbose($verbose_level_always,
"Unable to update $column_name in table: ".
$table_name,
$sth_update->errstr);
$exit++;
}
else
{
verbose($verbose_level_debug,
'Updated '.
(($result == 0E0) ? '0' : $result)
." rows in table: $table_name");
}
last;
}
}
}
if ($num_tables == 0)
{
verbose($verbose_level_always,
'Database is empty. Cannot change hostname.');
return 1;
}
# delete (orphaned) rows with hostname coded into chainid in tvchain
# live-<hostname>-2008-06-26T18:43:18
$table_name = 'tvchain';
$query = "DELETE FROM $table_name WHERE chainid LIKE ?";
$sth_update = $dbh->prepare($query);
$sth_update->bind_param(1, '%'.$old_hostname.'%');
$result = $sth_update->execute;
if (!defined($result))
{
verbose($verbose_level_debug,
"Unable to remove orphaned $table_name rows.",
$sth_update->errstr);
}
else
{
verbose($verbose_level_debug,
'Removed '.
(($result == 0E0) ? '0' : $result)
." orphaned entries in table: $table_name");
}
# hostname coded into SGweightPerDir setting in settings (modify)
# SGweightPerDir:<hostname>:<directory>
$table_name = 'settings';
$query = "UPDATE $table_name SET value = REPLACE(value, ?, ?)".
" WHERE value LIKE ?";
$sth_update = $dbh->prepare($query);
$sth_update->bind_param(1, 'SGweightPerDir:'.$old_hostname.':');
$sth_update->bind_param(2, 'SGweightPerDir:'.$new_hostname.':');
$sth_update->bind_param(3, 'SGweightPerDir:'.$old_hostname.':%');
$result = $sth_update->execute;
if (!defined($result))
{
verbose($verbose_level_always,
'Unable to update SGweightPerDir setting for host.',
$sth_update->errstr);
}
else
{
verbose($verbose_level_debug,
'Updated '.
(($result == 0E0) ? '0' : $result)
.' SGweightPerDir settings.');
}
}
return $exit;
}

sub get_db_schema_ver
{
connect_to_database(1, 1) if (!defined($dbh));
if (!defined($dbh))
{
verbose($verbose_level_error,
'', 'ERROR: Unable to connect to database.');
return -1;
}
my $query = 'SELECT data FROM settings WHERE value = ?';
if (defined($dbh))
{
my $sth = $dbh->prepare($query);
if ($sth->execute('DBSchemaVer'))
{
while (my @data = $sth->fetchrow_array)
{
$mysql_conf{'db_schemaver'} = $data[0];
verbose($verbose_level_debug,
'', 'Found DBSchemaVer:'.
" $mysql_conf{'db_schemaver'}.");
}
}
else
{
verbose($verbose_level_debug,
"Unable to retrieve DBSchemaVer from".
" database.");
}
}

return 0;
}

sub set_database_charset
{
return 0 if (!$create_database && !$drop_database);

if (get_db_schema_ver && ! $mysql_conf{'db_schemaver'})
{
verbose($verbose_level_error,
"Unknown database schema version. Assuming current.");
$mysql_conf{'db_schemaver'} = '1216';
}

if ($mysql_conf{'db_schemaver'} > 1215)
{
connect_to_database(0, 1) if (!defined($dbh));
if (!defined($dbh))
{
verbose($verbose_level_error,
'', 'ERROR: Unable to connect to database.');
return -1;
}

verbose($verbose_level_debug, 'Setting database character set.');

my ($query, $sth);
$query = qq{ALTER DATABASE $mysql_conf{'db_name'}
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci;};
$sth = $dbh->prepare($query);
if (! $sth->execute())
{
verbose($verbose_level_error,
'', 'ERROR: Unable to set database character set.',
$sth->errstr);
return -16;
}
}

return 0;
}

sub restore_backup
{
my $exit = 0;
my $defaults_extra_file = create_defaults_extra_file;
my $host_arg = '';
my $port_arg = '';
my $user_arg = '';
my $filter = '';
if ($defaults_extra_file)
{
$defaults_arg = " --defaults-extra-file='$defaults_extra_file'";
}
else
{
$defaults_arg = '';
}
my $safe_mysql_client = $mysql_client;
$safe_mysql_client =~ s/'/'\\''/g;
# Create the args for host, port, and user, shell-escaping values, as
# necessary.
my $safe_string;
if ($mysql_conf{'db_host'})
{
$safe_string = $mysql_conf{'db_host'};
$safe_string =~ s/'/'\\''/g;
$host_arg = " --host='$safe_string'";
}
if ($mysql_conf{'db_port'} > 0)
{
$safe_string = $mysql_conf{'db_port'};
$safe_string =~ s/'/'\\''/g;
$port_arg = " --port='$safe_string'";
}
if ($mysql_conf{'db_user'})
{
$safe_string = $mysql_conf{'db_user'};
$safe_string =~ s/'/'\\''/g;
$user_arg = " --user='$safe_string'";
}
# Configure a filter for a partial/new-host restore
if ($partial_restore)
{
my @partial_restore_tables;
if (defined($with_plugin_data))
{


 

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...