1 |
#!/usr/bin/perl -w |
2 |
# ApplySnapshot |
3 |
# Vadim Mikheev, (c) 2000, PostgreSQL Inc. |
4 |
|
5 |
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' |
6 |
& eval 'exec perl -S $0 $argv:q' |
7 |
if 0; |
8 |
|
9 |
BEGIN { |
10 |
my $basedir = $0; $basedir =~ s#/[^/]+$##; |
11 |
unshift(@INC, "$basedir/../share"); |
12 |
} |
13 |
|
14 |
use strict; |
15 |
use IO::File; |
16 |
use RServ; |
17 |
use Getopt::Long; |
18 |
|
19 |
$| = 1; |
20 |
|
21 |
my ($debug,$verbose) = (0,0); |
22 |
my ($help,$slavehost,$slaveport,$slaveuser,$slavepassword); |
23 |
my $snapshot; |
24 |
|
25 |
my $result = GetOptions( |
26 |
"debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help, |
27 |
"slavehost=s" => \$slavehost, "slaveport=i" => \$slaveport, |
28 |
"slaveuser=s" => \$slaveuser, "slavepassword=s" => \$slavepassword, |
29 |
"snapshot=s" => \$snapshot, |
30 |
); |
31 |
|
32 |
if (defined($help) || (scalar(@ARGV) < 1)) { |
33 |
print "Usage: $0 [options] slavedb |
34 |
Options: |
35 |
--slavehost=hostname --slaveport=port |
36 |
--slaveuser=username --slavepassword=string |
37 |
--snapshot=snapshot |
38 |
"; |
39 |
exit ((scalar(@ARGV) < 1)? 1:0); |
40 |
} |
41 |
|
42 |
my $slave = $ARGV[0] || "slave"; |
43 |
|
44 |
my $sinfo = "dbname=$slave"; |
45 |
$sinfo = "$sinfo host=$slavehost" if (defined($slavehost)); |
46 |
$sinfo = "$sinfo port=$slaveport" if (defined($slaveport)); |
47 |
$sinfo = "$sinfo user=$slaveuser" if (defined($slaveuser)); |
48 |
$sinfo = "$sinfo password=$slavepassword" if (defined($slavepassword)); |
49 |
|
50 |
my $conn = Pg::connectdb($sinfo); |
51 |
|
52 |
my $inpf; |
53 |
if (defined($snapshot)) { |
54 |
$inpf = new IO::File; |
55 |
$inpf->open("< $snapshot"); |
56 |
} else { |
57 |
$inpf = *STDIN; |
58 |
} |
59 |
|
60 |
my $res = ApplySnapshot ($conn, $inpf); |
61 |
|
62 |
if ($res > 0) |
63 |
{ |
64 |
printf STDERR "Snapshot applied\n"; |
65 |
} |
66 |
elsif ($res != 0) |
67 |
{ |
68 |
printf STDERR "ERROR\n"; |
69 |
exit(1); |
70 |
} |
71 |
|
72 |
exit(0); |