| 1 | # ----------------------------------------------------------------------------- | ||||||||||
| 2 | |||||||||||
| 3 | =encoding utf8 | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Quiq::PostgreSql::PgDump - Wrapper für pg_dump | ||||||||||
| 8 | |||||||||||
| 9 | =head1 BASE CLASS | ||||||||||
| 10 | |||||||||||
| 11 | L<Quiq::Object> | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | use Quiq::PostgreSql::PgDump; | ||||||||||
| 16 | |||||||||||
| 17 | Quiq::PostgreSql::PgDump->run($database,@opt); | ||||||||||
| 18 | |||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||
| 20 | |||||||||||
| 21 | Die Klasse stellt einen Wrapper für den PostgreSQL-Client pg_dump dar. | ||||||||||
| 22 | |||||||||||
| 23 | =head1 EXAMPLE | ||||||||||
| 24 | |||||||||||
| 25 | $ perl -MQuiq::PostgreSql::PgDump -E 'Quiq::PostgreSql::PgDump->run("prod","--table","p_muster.admviews","--schema-only","--debug")' | ||||||||||
| 26 | |||||||||||
| 27 | =cut | ||||||||||
| 28 | |||||||||||
| 29 | # ----------------------------------------------------------------------------- | ||||||||||
| 30 | |||||||||||
| 31 | package Quiq::PostgreSql::PgDump; | ||||||||||
| 32 | 1 | ▶ | use base qw/Quiq::Object/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | 1 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | 1 | our $VERSION = '1.240'; | |||||||||
| 39 | |||||||||||
| 40 | 1 | ▶ | use Quiq::Stopwatch; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 1 | ▶ | use Quiq::Udl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 1 | ▶ | use Quiq::CommandLine; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 1 | ▶ | use Expect (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | # ----------------------------------------------------------------------------- | ||||||||||
| 46 | |||||||||||
| 47 | =head1 METHODS | ||||||||||
| 48 | |||||||||||
| 49 | =head2 Klassenmethoden | ||||||||||
| 50 | |||||||||||
| 51 | =head3 run() - Rufe pg_dump ohne Passwortabfrage auf | ||||||||||
| 52 | |||||||||||
| 53 | =head4 Synopsis | ||||||||||
| 54 | |||||||||||
| 55 | $class->run($database,@opt); | ||||||||||
| 56 | |||||||||||
| 57 | =head4 Arguments | ||||||||||
| 58 | |||||||||||
| 59 | =over 4 | ||||||||||
| 60 | |||||||||||
| 61 | =item $database | ||||||||||
| 62 | |||||||||||
| 63 | Name der Datenbank oder der Universal Database Locator (UDL). | ||||||||||
| 64 | Ist ein Name angegeben, muss in der Datenbank-Konfigurationsdatei | ||||||||||
| 65 | definiert sein. | ||||||||||
| 66 | |||||||||||
| 67 | =back | ||||||||||
| 68 | |||||||||||
| 69 | =head4 Options | ||||||||||
| 70 | |||||||||||
| 71 | Alle Optionen von C<pg_dump>, plus | ||||||||||
| 72 | |||||||||||
| 73 | =over 4 | ||||||||||
| 74 | |||||||||||
| 75 | =item -debug => $bool (Default: 0) | ||||||||||
| 76 | |||||||||||
| 77 | Gib das ausgeführte pg_dump-Kommando auf STDOUT aus. | ||||||||||
| 78 | |||||||||||
| 79 | =back | ||||||||||
| 80 | |||||||||||
| 81 | =cut | ||||||||||
| 82 | |||||||||||
| 83 | # ----------------------------------------------------------------------------- | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||
| 86 | 0 | my ($class,$database) = splice @_,0,2; | |||||||||
| 87 | # @_: @opt | ||||||||||
| 88 | |||||||||||
| 89 | 0 | my $stw = Quiq::Stopwatch->new; | |||||||||
| 90 | |||||||||||
| 91 | # Options (alle anderen Argumente befinden sich auf @_) | ||||||||||
| 92 | |||||||||||
| 93 | 0 | my $debug = 0; | |||||||||
| 94 | |||||||||||
| 95 | 0 | $class->parameters(1,\@_, | |||||||||
| 96 | -debug => \$debug, | ||||||||||
| 97 | ); | ||||||||||
| 98 | |||||||||||
| 99 | # Führe Operation aus | ||||||||||
| 100 | |||||||||||
| 101 | 0 | my $udl = Quiq::Udl->new($database); | |||||||||
| 102 | 0 | ▶ | if ($udl->dbms ne 'postgresql') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | $class->throw( | |||||||||
| 104 | 'PSQL-00001: Not a PostgeSQL UDL', | ||||||||||
| 105 | Udl => $udl->asString, | ||||||||||
| 106 | ); | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 0 | my $c = Quiq::CommandLine->new('pg_dump'); | |||||||||
| 110 | 0 | for my $opt (qw/user host port/) { | |||||||||
| 111 | 0 | ▶ | if (my $val = $udl->$opt) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 0 | $c->addLongOption( | |||||||||
| 113 | "--$opt" => $val, | ||||||||||
| 114 | ); | ||||||||||
| 115 | } | ||||||||||
| 116 | } | ||||||||||
| 117 | 0 | $c->addBoolOption('--schema-only'=>1); | |||||||||
| 118 | 0 | $c->addString("@_"); | |||||||||
| 119 | |||||||||||
| 120 | 0 | ▶ | if (my $database = $udl->db) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 0 | $c->addArgument($database); | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 0 | my $cmd = $c->command; | |||||||||
| 125 | 0 | ▶ | if ($debug) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | say $cmd; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 0 | my $exp = Expect->new; | |||||||||
| 130 | 0 | ▶ | $exp->spawn($cmd) || do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | 0 | $class->throw( | |||||||||
| 132 | 'EXPECT-00099: Cannot spawn command', | ||||||||||
| 133 | Command => $cmd, | ||||||||||
| 134 | ); | ||||||||||
| 135 | }; | ||||||||||
| 136 | |||||||||||
| 137 | # Anmeldung. Wir unterscheiden drei Fälle. | ||||||||||
| 138 | |||||||||||
| 139 | 0 | my $interact = 0; | |||||||||
| 140 | $exp->expect(3,[ | ||||||||||
| 141 | # mit Passwort | ||||||||||
| 142 | 0 | ▶ | -re => 'Password.*?:',sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 143 | 0 | my $exp = shift; | |||||||||
| 144 | 0 | $exp->send($udl->password."\n"); | |||||||||
| 145 | 0 | $interact = 1; | |||||||||
| 146 | }, | ||||||||||
| 147 | ], | ||||||||||
| 148 | #[ | ||||||||||
| 149 | # # ohne Passwort | ||||||||||
| 150 | # -re => '^psql ',sub { | ||||||||||
| 151 | # $interact = 1; | ||||||||||
| 152 | # }, | ||||||||||
| 153 | #], | ||||||||||
| 154 | [ | ||||||||||
| 155 | # Verbindung kommt nicht zustande | ||||||||||
| 156 | 0 | ▶ | -re => '^pg_dump:',sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||
| 157 | # Ende der Kommunikation | ||||||||||
| 158 | }, | ||||||||||
| 159 | 0 | ]); | |||||||||
| 160 | |||||||||||
| 161 | 0 | ▶ | if ($interact) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | # Benutzer-Interaktion | ||||||||||
| 163 | 0 | $exp->interact; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 0 | printf "Duration: %s\n",$stw->elapsedReadable; | |||||||||
| 167 | |||||||||||
| 168 | 0 | return; | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | # ----------------------------------------------------------------------------- | ||||||||||
| 172 | |||||||||||
| 173 | =head1 VERSION | ||||||||||
| 174 | |||||||||||
| 175 | 1.240 | ||||||||||
| 176 | |||||||||||
| 177 | =head1 AUTHOR | ||||||||||
| 178 | |||||||||||
| 179 | Frank Seitz, L<http://fseitz.de/> | ||||||||||
| 180 | |||||||||||
| 181 | =head1 COPYRIGHT | ||||||||||
| 182 | |||||||||||
| 183 | Copyright (C) 2026 Frank Seitz | ||||||||||
| 184 | |||||||||||
| 185 | =head1 LICENSE | ||||||||||
| 186 | |||||||||||
| 187 | This code is free software; you can redistribute it and/or modify | ||||||||||
| 188 | it under the same terms as Perl itself. | ||||||||||
| 189 | |||||||||||
| 190 | =cut | ||||||||||
| 191 | |||||||||||
| 192 | # ----------------------------------------------------------------------------- | ||||||||||
| 193 | |||||||||||
| 194 | 1 | 1; | |||||||||
| 195 | |||||||||||
| 196 | # eof | ||||||||||