/[rserv]/bin/Replicate
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/Replicate

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

revision 1.2 by dpavlin, Tue Aug 5 09:20:08 2003 UTC revision 1.4 by dpavlin, Tue Aug 5 10:01:42 2003 UTC
# Line 11  use lib "/usr/share/postgresql/contrib"; Line 11  use lib "/usr/share/postgresql/contrib";
11  use IO::File;  use IO::File;
12  use Getopt::Long;  use Getopt::Long;
13  use RServ;  use RServ;
14    use Sys::Hostname;
15    
16  $| = 1;  $| = 1;
17    
18  $result = GetOptions("debug!", "verbose!", "help", "snapshot=s",  $result = GetOptions("debug!", "verbose!", "help", "snapshot=s",
19                       "masterhost=s", "slavehost=s", "host=s",                       "masterhost=s", "slavehost=s",
20                       "masteruser=s", "slaveuser=s", "user=s",                       "masteruser=s", "slaveuser=s",
21                       "masterpassword=s", "slavepassword=s", "password=s");                       "masterpassword=s", "slavepassword=s",
22                         "masterport=i", "slaveport=i"
23                         );
24    
25  my $debug = $opt_debug || 0;  my $debug = $opt_debug || 0;
26  my $verbose = $opt_verbose || 0;  my $verbose = $opt_verbose || 0;
27  my $snapshot = $opt_snapshot || "__Snapshot";  my $snapshot = $opt_snapshot || ".__Snapshot";
28    $snapshot .= '.' . $$;
29    
30  if (defined($opt_help) || (scalar(@ARGV) < 2)) {  if (defined($opt_help) || (scalar(@ARGV) < 2)) {
31      print "Usage: $0 --snapshot=file --host=name --user=name --password=string masterdb slavedb\n";      print "Usage: $0 --snapshot=file masterdb slavedb\n";
32      print "\t--masterhost=name --masteruser=name --masterpassword=string\n";      print "\t--masterhost=name --masterport=port\n";
33      print "\t--slavehost=name --slaveuser=name --slavepassword=string\n";      print "\t--masteruser=name --masterpassword=string\n";
34        print "\t--slavehost=name --slaveport=port\n";
35        print "\t--slaveuser=name --slavepassword=string\n";
36      exit ((scalar(@ARGV) < 2)? 1:0);      exit ((scalar(@ARGV) < 2)? 1:0);
37  }  }
38    
39  my $master = $ARGV[0] || "master";  my $master = $ARGV[0] || "master";
40  my $slave = $ARGV[1] || "slave";  my $slave = $ARGV[1] || "slave";
41  my $server = 0;  
42    $masterhost = $opt_masterhost || hostname;
43    $slavehost = $opt_slavehost || hostname;
44    
45    # first, let's check if a instance is alrealy running
46    
47    $str = $master.'@'.$masterhost.':'.$slave.'@'.$slavehost;
48    my $fname = "/tmp/.lock_rserv-$str";
49    if (-e $fname) {
50        print STDERR "rServ is already running for $str. Remove $fname to override this.\n";
51        exit(1);
52    }
53    open ARQ, ">$fname" || die "Cannot open $fname: $!\n";
54    print ARQ "$$\n";
55    close ARQ;
56    
57  my $minfo = "dbname=$master";  my $minfo = "dbname=$master";
58  $minfo = "$minfo host=$opt_masterhost" if (defined($opt_masterhost));  $minfo = "$minfo host=$opt_masterhost" if (defined($opt_masterhost));
59  $minfo = "$minfo user=$opt_masteruser" if (defined($opt_masteruser));  $minfo = "$minfo user=$opt_masteruser" if (defined($opt_masteruser));
60  $minfo = "$minfo password=$opt_masterpassword" if (defined($opt_masterpassword));  $minfo = "$minfo password=$opt_masterpassword" if (defined($opt_masterpassword));
61    $minfo = "$minfo port=$opt_masterport" if (defined($opt_masterport));
62  my $sinfo = "dbname=$slave";  my $sinfo = "dbname=$slave";
63  $sinfo = "$sinfo host=$opt_slavehost" if (defined($opt_slavehost));  $sinfo = "$sinfo host=$opt_slavehost" if (defined($opt_slavehost));
64  $sinfo = "$sinfo user=$opt_slaveuser" if (defined($opt_slaveuser));  $sinfo = "$sinfo user=$opt_slaveuser" if (defined($opt_slaveuser));
65  $sinfo = "$sinfo password=$opt_slavepassword" if (defined($opt_slavepassword));  $sinfo = "$sinfo password=$opt_slavepassword" if (defined($opt_slavepassword));
66    $sinfo = "$sinfo port=$opt_slaveport" if (defined($opt_slaveport));
67    
68  print "Master connection is $minfo\n" if ($debug);  print "Master connection is $minfo\n" if ($debug);
69  print "Slave connection is $sinfo\n" if ($debug);  print "Slave connection is $sinfo\n" if ($debug);
70    
71  my $mconn = Pg::connectdb($minfo);  my $mconn = Pg::connectdb($minfo);
72    if ($mconn->status != PGRES_CONNECTION_OK) {
73        print STDERR "Failed opening $minfo\n";
74        unlink $fname;
75        exit 1;
76    }
77  my $sconn = Pg::connectdb($sinfo);  my $sconn = Pg::connectdb($sinfo);
78    if ($sconn->status != PGRES_CONNECTION_OK) {
79        print STDERR "Failed opening $sinfo\n";
80        unlink $fname;
81        exit 1;
82    }
83    
84  $RServ::quiet = !$verbose;  my $slaveId = GetSlaveId($mconn, $slave, defined($opt_slavehost) ? $opt_slavehost : hostname);
85    if ($slaveId < 0) {
86        unlink $fname;
87        die "\n>>>>>>>>>>>>> ERROR\n";
88    }
89  SyncSync($mconn, $sconn);  SyncSync($mconn, $sconn);
90    
91  my $outf = new IO::File;  my $outf = new IO::File;
92  open $outf, ">$snapshot";  open $outf, ">$snapshot";
93  print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose);  print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose);
94  $res = PrepareSnapshot($mconn, $outf, $server);  $res = PrepareSnapshot($mconn, $sconn, $outf, $slaveId);
95  close $outf;  close $outf;
96  die "\n>>>>>>>>>>>>> ERROR\n" if $res < 0;  if ($res < 0) {
97        unlink $fname;
98        die "\n>>>>>>>>>>>>> ERROR\n";
99    }
100  if ($res == 0)  if ($res == 0)
101  {  {
102          print "\n>>>>>>>>>>>>> DBases are sync-ed\n" if ($verbose);      print "\n>>>>>>>>>>>>> DBases are sync-ed\n" if ($verbose);
103          exit(0);      unlink $snapshot unless ($debug);
104        unlink $fname;
105        exit(0);
106  }  }
107    
108  my $inpf = new IO::File;  my $inpf = new IO::File;
# Line 70  open $inpf, "<$snapshot"; Line 110  open $inpf, "<$snapshot";
110  print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose);  print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose);
111  $res = ApplySnapshot($sconn, $inpf);  $res = ApplySnapshot($sconn, $inpf);
112  close $inpf;  close $inpf;
113  die "\n>>>>>>>>>>>>> ERROR\n" if $res < 0;  if ($res < 0) {
114        unlink $fname;
115        die "\n>>>>>>>>>>>>> ERROR\n";
116    }
117    
118  if ($res > 0)  if ($res > 0)
119  {  {
120          print "Snapshot applied\n" if ($verbose);      print "Snapshot applied\n" if ($verbose);
121          unlink $snapshot unless ($debug);      unlink $snapshot unless ($debug);
122          SyncSync($mconn, $sconn);      SyncSync($mconn, $sconn);
123  }  }
124    
125    unlink $fname;
126  exit(0);  exit(0);
127    
128  ###########################################################################  ###########################################################################
# Line 95  sub SyncSync Line 139  sub SyncSync
139          print "Last SyncID applied: $syncid\n" if ($verbose);          print "Last SyncID applied: $syncid\n" if ($verbose);
140          print "Sync SyncID\n" if ($verbose);          print "Sync SyncID\n" if ($verbose);
141    
142          $res = SyncSyncID($mconn, $server, $syncid);          $res = SyncSyncID($mconn, $slaveId, $syncid);
143    
144          print "Succeeded\n" if (($res > 0) && ($verbose));          print "Succeeded\n" if (($res > 0) && ($verbose));
145      }      }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.26