quarta-feira, 9 de fevereiro de 2011

PostgreSQL - Agente de backup para tabelas particionadas

 Com este agente escrito em Perl é possível fazer o backup de tabelas particionadas, desde que as mesmas estejam no padrão de documentação de particionamento de tabelas do PostgreSQL. Com este agente serão gerados dumps anuais em formato custom que serão validados pelo pg_restore através do argumento --list.

   Depois de feito o backup você pode retirar as tabelas dumpeadas da tabela pai e mandar um bom DROP TABLE nelas.


#!/usr/bin/perl
#dump-get
#
#Description: Script to generate dump table
#
#Author:
#        Gabriel Prestes
#
#02-09-2009 : Created

use strict;
use Getopt::Long;
use POSIX;
use File::Basename;

#--------------------------------------------------
# Setting environment
#--------------------------------------------------
$ENV{"USER"}="root";
$ENV{"HOME"}="/root";

#--------------------------------------------------
# Global variables
#--------------------------------------------------
our $name = basename($0);
our $version = "0.1a";
our $path = "/opt";
our $temp_log = "$path/$name.log";
our ($opt_help, $opt_version, $opt_db, $opt_host, $opt_table, $opt_user, $opt_year, $opt_port);

sub main {

        # --- Get Options --- #
        getoption();

# --- Set default port and user --- #
if(!$opt_user){$opt_user="postgres";}
if(!$opt_port){$opt_port=5432;}

# --- Init proc --- #
logger("INIT DUMP process");

# --- My vars --- #
my @cmd=();
my $counter;
my $flagcontrol=0;

# --- Create directory --- #
@cmd=`\$\(which mkdir\) $path/$opt_year-dumps >> $temp_log 2>&1`;
$flagcontrol=+$?;

# --- If directory problem --- #
if($flagcontrol!=0){logger("ERROR - Directory $path/$opt_year-dumps can not be created"); exit;}

# --- Generate dump --- #
for($counter=1;$counter<=12;$counter++){

         $counter = sprintf("%02d", $counter);

@cmd=`\$\(which pg_dump\) -h $opt_host -p $opt_port -U $opt_user -F c -v -f $path/$opt_year-dumps/$opt_table\_$opt_year\_$counter.dmp -t $opt_table\_$opt_year\_$counter $opt_db >> $temp_log 2>&1`;
$flagcontrol=+$?;

@cmd=`\$\(which pg_restore\) -l $path/$opt_year-dumps/$opt_table\_$opt_year\_$counter.dmp >> $temp_log 2>&1`;
$flagcontrol=+$?;

}

# --- Thresholds --- #
if($flagcontrol!=0){

logger("ERROR - Agent run with problem");
exit;

}

if($flagcontrol==0){

                logger("OK - Agent run without problem");
                exit;

        }

logger("UNKNOWN - Unexpected return");
        exit;

}

#--------------------------------------------------------------------------------------
sub getoption {
     Getopt::Long::Configure('bundling');
     GetOptions(

         'D|database=s'              => \$opt_db,
         'H|host=s'                  => \$opt_host,
             'T|table=s'                 => \$opt_table,
             'U|user=s'                  => \$opt_user,
             'V|version'                 => \$opt_version,
             'Y|year=i'                  => \$opt_year,
             'h|help'                    => \$opt_help,
             'p|port=i'                  => \$opt_port,
        );

     if($opt_help){

             printHelp();
             exit;

     }

     if($opt_version){

             print "$name - '$version'\n";
             exit;

     }

     if((!$opt_host) or (!$opt_db) or (!$opt_year) or (!$opt_table)){

printUsage();
exit;

     }

}
#--------------------------------------------------------------------------------------
sub logger {

        my $msg = shift (@_);

        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
        $wday++;
        $yday++;
        $mon++;
        $year+=1900;
        $isdst++;

           open(LOG, ">>$temp_log");
           printf LOG ("%02i/%02i/%i - %02i:%02i:%02i => %s\n",$mday,$mon,$year,$hour,$min,$sec,$msg);
           close(LOG);

}
#--------------------------------------------------------------------------------------

sub printUsage {

       print <<EOB

Usage: $name.pl [OPTION]...

        -D, --database     Set database
        -H, --host         Set IP database
        -T, --table        Set table prefix
        -U, --user         Set user database
        -V, --version      Show version
        -Y, --year         Set year of partitioned table
        -h, --help         Show help
        -p, --port         Set port database


EOB

}
#--------------------------------------------------------------------------------------
sub printHelp {

                my $help = <<'HELP';


                Thanks for use DUMP-GET.

                How do?

You need use -T parameter to specify table and -Y to specify year. Other parameters are the string connection.

This agent only work if used trust method.

                API required:

                strict;
                Getopt::Long;
                POSIX;
                File::Basename;      

                Words of God: "Cordeiro de Deus, retirai os pecados do mundo, tende piedade de nos..."







HELP

                system("clear");
                print $help;

}
#--------------------------------------------------------------------------------------
&main

sexta-feira, 4 de fevereiro de 2011

Oracle - Quando as crianças ORA-01034 e ORA-27101 vem te abraçar juntinhas

Você tenta conectar localmente e tudo funciona, mas conexões remotas apresentam a mensagem:

ERROR: ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist



Não se desespere, o que normalmente ocorre é que seu SID ou ORACLE_HOME estão incorretos, lembre-se que o '/etc/oratab', 'listener.ora' e as variáveis de ambiente do Oracle devem estar iguais. 


Exemplo, se meu SID é 'teste' deve ser colocado assim em todos os lugares citados acima, se em algum estiver 'Teste' ou 'TESTE' dará problema.

No $ORACLE_HOME não coloque '/' no final, se tiver retire, feito isto faça um reload no ser listener e pronto.

terça-feira, 1 de fevereiro de 2011

Oracle - ORA-00101: Resolvendo o invalid specification for system parameter MTS_DISPATCHERS

ORA-00101: invalid specification for system parameter DISPATCHERS

Cause: The syntax for the DISPATCHERS parameter is incorrect.
Action: Refer to the manual for correct syntax.

Como diria no desenho pica-pau, "e lá vamos nós".

Este erro ocorre em razão do registro do listener não ser o default, logo a correção deve ser feita no 'init.ora'.

Basta ajustar ou comentar no 'init.ora':

#mts_dispatchers = "(PROTOCOL=TCP)(SER=MODOSE)"

Depois disto ajuste seu listener mais ou menos assim:

LISTENER =
  (ADDRESS_LIST=
       (ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))
       (ADDRESS=(PROTOCOL=ipc)(KEY=PNPKEY)))

 SID_LIST_LISTENER=
   (SID_LIST=
       (SID_DESC=
          (SID_NAME=hsodbc)
          (ORACLE_HOME=/private/app/oracle/product/8.1.6)
          (PROGRAM=hsodbc)
        )
       )

CONNECT_TIMEOUT_LISTENER = 0

Suba o listener(lsnctl start) e depois a database(sqlplus '\as sysdba' - 'startup open') e pronto, seu Oracle no ar.