/[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

Annotation of /bin/SlaveInit

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations)
Sun Nov 2 10:31:44 2003 UTC (20 years, 6 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +6 -8 lines
another code clean-up

1 dpavlin 1.5 #!/usr/bin/perl -w
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 dpavlin 1.9 BEGIN {
10     my $basedir = $0; $basedir =~ s#/[^/]+$##;
11     unshift(@INC, "$basedir/../share");
12     }
13    
14 dpavlin 1.5 use strict;
15 dpavlin 1.1 use Pg;
16     use Getopt::Long;
17 dpavlin 1.9 use RServ;
18 dpavlin 1.1
19     $| = 1;
20    
21 dpavlin 1.5 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 dpavlin 1.1 }
44    
45 dpavlin 1.3 my $master = $ARGV[0] || "master";
46     my $slave = $ARGV[1] || "slave";
47 dpavlin 1.1
48 dpavlin 1.8 my $minfo = MkInfo($master,$masterhost,$masterport,$masteruser,$masterpassword);
49     my $sinfo = MkInfo($slave,$slavehost,$slaveport,$slaveuser,$slavepassword);
50 dpavlin 1.1
51 dpavlin 1.3 # First, lets add the needed information in the slave database
52    
53 dpavlin 1.1 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 dpavlin 1.5 $result = $conn->exec("BEGIN");
61 dpavlin 1.1 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 dpavlin 1.3 " (tname name not null, cname name not null, reloid oid not null, key int4 not null)");
68 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
69    
70     $result = $conn->exec("create table _RSERV_SLAVE_SYNC_" .
71 dpavlin 1.6 " (syncid int4 not null, synctime timestamp)");
72 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
73    
74 dpavlin 1.3
75     # next, let's tell the master that the slave is set up.
76    
77     print("Connecting to $minfo\n") if ($debug || $verbose);
78 dpavlin 1.5 my $mconn = Pg::connectdb($minfo);
79 dpavlin 1.3 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 dpavlin 1.7 $slavehost="localhost" if (! $slavehost);
86 dpavlin 1.3
87 dpavlin 1.7 $result = $mconn->exec("INSERT INTO _RSERV_SERVERS_ (host,dbase) VALUES ('$slavehost','$slave')");
88 dpavlin 1.3 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 dpavlin 1.1 $result = $conn->exec("COMMIT");
98     RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
99    
100 dpavlin 1.3
101    
102 dpavlin 1.1 exit (0);

  ViewVC Help
Powered by ViewVC 1.1.26