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