/[rserv]/bin/SlaveInit
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /bin/SlaveInit

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (show annotations)
Sun Nov 2 10:21:45 2003 UTC (20 years, 6 months ago) by dpavlin
Branch: MAIN
Changes since 1.7: +2 -11 lines
moved all info preparation into MkInfo in RServ.pm

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

  ViewVC Help
Powered by ViewVC 1.1.26