/[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.3 - (show annotations)
Tue Aug 5 09:52:36 2003 UTC (20 years, 9 months ago) by dpavlin
Branch: MAIN
Changes since 1.2: +43 -8 lines
rserv 0.2 changes by Nélio Alves Pereira Filho

1 #!/usr/bin/perl
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 Pg;
10 use Getopt::Long;
11 use Sys::Hostname;
12
13 $| = 1;
14
15 $result = GetOptions("debug!", "verbose!", "quiet!", "help",
16 "masterhost=s", "masteruser=s", "masterpassword=s",
17 "slavehost=s", "slaveuser=s", "slavepassword=s");
18
19 my $debug = $opt_debug || 0;
20 my $verbose = $opt_verbose || 0;
21 my $quiet = $opt_quiet || 0;
22
23 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 exit ((scalar(@ARGV) < 1)? 1:0);
26 }
27
28 my $master = $ARGV[0] || "master";
29 my $slave = $ARGV[1] || "slave";
30
31 my $sinfo = "dbname=$slave";
32 $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
41 sub RollbackAndQuit {
42 my $conn = shift @_;
43
44 print STDERR $conn->errorMessage;
45 $conn->exec("ROLLBACK");
46 exit (-1);
47 }
48
49 # First, lets add the needed information in the slave database
50
51 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 " (tname name not null, cname name not null, reloid oid not null, key int4 not null)");
66 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
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 $result = $conn->exec("COMMIT");
96 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
97
98
99
100 exit (0);

  ViewVC Help
Powered by ViewVC 1.1.26