1 |
#!/usr/bin/perl -w |
2 |
# SlaveInit |
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 Pg; |
16 |
use Getopt::Long; |
17 |
use RServ; |
18 |
|
19 |
$| = 1; |
20 |
|
21 |
my ($debug,$verbose,$quiet) = (0,0,0); |
22 |
my ($help,$masterhost,$masterport,$masteruser,$masterpassword, |
23 |
$slavehost,$slaveport,$slaveuser,$slavepassword); |
24 |
|
25 |
my $result = GetOptions( |
26 |
"debug!" => \$debug, "verbose!" => \$verbose, |
27 |
"quiet!" => \$quiet, "help" => \$help, |
28 |
"masterhost=s" => \$masterhost, "masterport=i" => \$masterport, |
29 |
"masteruser=s" => \$masteruser, "masterpassword=s" => \$masterpassword, |
30 |
"slavehost=s" => \$slavehost, "slaveport=i" => \$slaveport, |
31 |
"slaveuser=s" => \$slaveuser, "slavepassword=s" => \$slavepassword, |
32 |
); |
33 |
|
34 |
if (defined($help) || (scalar(@ARGV) < 2)) { |
35 |
print "Usage: $0 [options] masterdb slavedb |
36 |
Options: |
37 |
--masterhost=hostname --masterport=port |
38 |
--masteruser=username --masterpassword=string |
39 |
--slavehost=hostname --slaveport=port |
40 |
--slaveuser=username --slavepassword=string |
41 |
"; |
42 |
exit ((scalar(@ARGV) < 2)? 1:0); |
43 |
} |
44 |
|
45 |
my $master = $ARGV[0] || "master"; |
46 |
my $slave = $ARGV[1] || "slave"; |
47 |
|
48 |
my $minfo = MkInfo($master,$masterhost,$masterport,$masteruser,$masterpassword); |
49 |
my $sinfo = MkInfo($slave,$slavehost,$slaveport,$slaveuser,$slavepassword); |
50 |
|
51 |
# First, lets add the needed information in the slave database |
52 |
|
53 |
print("Connecting to $sinfo\n") if ($debug || $verbose); |
54 |
my $conn = Pg::connectdb($sinfo); |
55 |
if ($conn->status != PGRES_CONNECTION_OK) { |
56 |
print STDERR "Failed opening $sinfo\n"; |
57 |
exit 1; |
58 |
} |
59 |
|
60 |
$result = $conn->exec("BEGIN"); |
61 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
62 |
|
63 |
$result = $conn->exec("set transaction isolation level serializable"); |
64 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
65 |
|
66 |
$result = $conn->exec("create table _RSERV_SLAVE_TABLES_" . |
67 |
" (tname name not null, cname name not null, reloid oid not null, key int4 not null)"); |
68 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
69 |
|
70 |
$result = $conn->exec("create table _RSERV_SLAVE_SYNC_" . |
71 |
" (syncid int4 not null, synctime timestamp)"); |
72 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
73 |
|
74 |
|
75 |
# next, let's tell the master that the slave is set up. |
76 |
|
77 |
print("Connecting to $minfo\n") if ($debug || $verbose); |
78 |
my $mconn = Pg::connectdb($minfo); |
79 |
if ($mconn->status != PGRES_CONNECTION_OK) { |
80 |
print STDERR "Failed opening $minfo\n"; |
81 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
82 |
# exit 1; |
83 |
} |
84 |
|
85 |
$slavehost="localhost" if (! $slavehost); |
86 |
|
87 |
$result = $mconn->exec("INSERT INTO _RSERV_SERVERS_ (host,dbase) VALUES ('$slavehost','$slave')"); |
88 |
if ($result->resultStatus ne PGRES_COMMAND_OK) { |
89 |
print STDERR $mconn->errorMessage; |
90 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
91 |
# exit (-1); |
92 |
} |
93 |
|
94 |
|
95 |
# then commit the slave transaction |
96 |
|
97 |
$result = $conn->exec("COMMIT"); |
98 |
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK); |
99 |
|
100 |
|
101 |
|
102 |
exit (0); |