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

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.26