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

Diff of /bin/SlaveInit

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.1.1 by dpavlin, Thu Dec 21 14:27:11 2000 UTC revision 1.3 by dpavlin, Tue Aug 5 09:52:36 2003 UTC
# Line 1  Line 1 
1  # -*- perl -*-  #!/usr/bin/perl
2  # SlaveInit  # SlaveInit
3  # Vadim Mikheev, (c) 2000, PostgreSQL Inc.  # Vadim Mikheev, (c) 2000, PostgreSQL Inc.
4    
# Line 8  eval '(exit $?0)' && eval 'exec perl -S Line 8  eval '(exit $?0)' && eval 'exec perl -S
8    
9  use Pg;  use Pg;
10  use Getopt::Long;  use Getopt::Long;
11    use Sys::Hostname;
12    
13  $| = 1;  $| = 1;
14    
15  $result = GetOptions("debug!", "verbose!", "quiet!", "help",  $result = GetOptions("debug!", "verbose!", "quiet!", "help",
16                       "host=s", "user=s", "password=s");                       "masterhost=s", "masteruser=s", "masterpassword=s",
17                         "slavehost=s", "slaveuser=s", "slavepassword=s");
18    
19  my $debug = $opt_debug || 0;  my $debug = $opt_debug || 0;
20  my $verbose = $opt_verbose || 0;  my $verbose = $opt_verbose || 0;
21  my $quiet = $opt_quiet || 0;  my $quiet = $opt_quiet || 0;
22    
23  if (defined($opt_help) || (scalar(@ARGV) < 1)) {  if (defined($opt_help) || (scalar(@ARGV) < 2)) {
24      print "Usage: $0 --host=name --user=name --password=string slavedb\n";      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);      exit ((scalar(@ARGV) < 1)? 1:0);
26  }  }
27    
28  my $slave = $ARGV[0] || "slave";  my $master = $ARGV[0] || "master";
29    my $slave = $ARGV[1] || "slave";
30    
31  my $sinfo = "dbname=$slave";  my $sinfo = "dbname=$slave";
32  $sinfo = "$sinfo host=$opt_host" if (defined($opt_host));  $sinfo = "$sinfo host=$opt_slavehost" if (defined($opt_slavehost));
33  $sinfo = "$sinfo user=$opt_user" if (defined($opt_user));  $sinfo = "$sinfo user=$opt_slaveuser" if (defined($opt_slaveuser));
34  $sinfo = "$sinfo password=$opt_password" if (defined($opt_password));  $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 {  sub RollbackAndQuit {
42      my $conn = shift @_;      my $conn = shift @_;
# Line 38  sub RollbackAndQuit { Line 46  sub RollbackAndQuit {
46      exit (-1);      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);  print("Connecting to $sinfo\n") if ($debug || $verbose);
52  my $conn = Pg::connectdb($sinfo);  my $conn = Pg::connectdb($sinfo);
53  if ($conn->status != PGRES_CONNECTION_OK) {  if ($conn->status != PGRES_CONNECTION_OK) {
# Line 52  $result = $conn->exec("set transaction i Line 62  $result = $conn->exec("set transaction i
62  RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);  RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
63    
64  $result = $conn->exec("create table _RSERV_SLAVE_TABLES_" .  $result = $conn->exec("create table _RSERV_SLAVE_TABLES_" .
65                        " (tname name, cname name, reloid oid, key int4)");                        " (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);  RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
67    
68  $result = $conn->exec("create table _RSERV_SLAVE_SYNC_" .  $result = $conn->exec("create table _RSERV_SLAVE_SYNC_" .
69                        " (syncid int4, synctime timestamp)");                        " (syncid int4, synctime timestamp)");
70  RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);  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");  $result = $conn->exec("COMMIT");
96  RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);  RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
97    
98    
99    
100  exit (0);  exit (0);

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.26