/[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.8 by dpavlin, Sun Aug 10 13:21:50 2003 UTC revision 1.18 by dpavlin, Sun Nov 2 21:07:14 2003 UTC
# Line 15  use strict; Line 15  use strict;
15  use IO::File;  use IO::File;
16  use Getopt::Long;  use Getopt::Long;
17  use RServ;  use RServ;
 use Sys::Hostname;  
18    
19  $| = 1;  $| = 1;
20    
21  my ($debug,$verbose) = (0,0);  #my ($debug,$verbose) = (0,0);
22  my ($help,$masterhost,$masterport,$masteruser,$masterpassword,  my ($help,$masterhost,$masterport,$masteruser,$masterpassword,
23          $slavehost,$slaveport,$slaveuser,$slavepassword);          $slavehost,$slaveport,$slaveuser,$slavepassword);
24  my $snapshot = ".__Snapshot";  my $snapshot = ".__Snapshot";
25    my $multimaster = 0;
26    
27  my $result = GetOptions(  my $result = GetOptions(
28          "debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help,          "debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help,
# Line 31  my $result = GetOptions( Line 31  my $result = GetOptions(
31          "slavehost=s" => \$slavehost, "slaveport=i" => \$slaveport,          "slavehost=s" => \$slavehost, "slaveport=i" => \$slaveport,
32          "slaveuser=s" => \$slaveuser, "slavepassword=s" => \$slavepassword,          "slaveuser=s" => \$slaveuser, "slavepassword=s" => \$slavepassword,
33          "snapshot=s" => \$snapshot,          "snapshot=s" => \$snapshot,
34            "multimaster" => \$multimaster,
35          );          );
36    
37  if (defined($help) || (scalar(@ARGV) < 2)) {  if (defined($help) || (scalar(@ARGV) < 2)) {
# Line 39  Options: Line 40  Options:
40          --masterhost=hostname --masterport=port          --masterhost=hostname --masterport=port
41          --masteruser=username --masterpassword=string          --masteruser=username --masterpassword=string
42          --snapshot=snapshot          --snapshot=snapshot
43            [--masterserver=master_number]
44  ";  ";
45      exit ((scalar(@ARGV) < 2)? 1:0);      exit ((scalar(@ARGV) < 2)? 1:0);
46  }  }
# Line 46  Options: Line 48  Options:
48  $snapshot .= '.' . $$;  $snapshot .= '.' . $$;
49    
50  $RServ::quiet = !$verbose;  $RServ::quiet = !$verbose;
51  if ($debug) {  #if ($debug) {
52          $RServ::quiet = 0;  #print "## q:$RServ::quiet v:$RServ::verbose d:$RServ::debug\n";
53          no warnings 'vars';  #       $RServ::quiet = 0;
54          $RServ::debug = $debug;  #       no warnings 'vars';
55  }  #       $RServ::debug = $debug;
56    #print "## q:$RServ::quiet v:$RServ::verbose d:$RServ::debug\n";
57    #}
58    
59  my $master = $ARGV[0] || "master";  my $master = $ARGV[0] || "master";
60  my $slave = $ARGV[1] || "slave";  my $slave = $ARGV[1] || "slave";
61    my $tables = $#ARGV < 2 ? undef : { map {($_, undef)} @ARGV[2..$#ARGV] };
62    
63  # first, let's check if a instance is alrealy running  # first, let's check if a instance is alrealy running
64    
# Line 63  $str .= ':'.$slave; Line 68  $str .= ':'.$slave;
68  $str .= '@'.$slavehost if (defined($slavehost));  $str .= '@'.$slavehost if (defined($slavehost));
69  my $fname = "/tmp/.lock_rserv-$str";  my $fname = "/tmp/.lock_rserv-$str";
70  if (-e $fname) {  if (-e $fname) {
71      print STDERR "rServ is already running for $str. Remove $fname to override this.\n";          open(PID,$fname) || die "can't open pid file '$fname'";
72      exit(1);          my $pid = <PID>; chomp($pid);
73            close(PID);
74            open(PS,"ps ax |") || die "can't start ps to check for running rServ with pid $pid\n";
75            my $is_running = 0;
76            while(<PS>) {
77                    chomp;
78                    $is_running = 1 if (/^\s*$pid\s/);
79            }
80            close(PS);
81            if ($is_running) {
82                    print STDERR "rServ [$pid] is already running for $str. Remove $fname to override this.\n";
83                    exit(1);
84            } else {
85                    print STDERR "found stale pid file, but no running rServ [$pid], overriding.\n";
86            }
87  }  }
88  open ARQ, ">$fname" || die "Cannot open $fname: $!\n";  open ARQ, ">$fname" || die "Cannot open $fname: $!\n";
89  print ARQ "$$\n";  print ARQ "$$\n";
90  close ARQ;  close ARQ;
91    
92  my $minfo = "dbname=$master";  $SIG{__DIE__} = \&mydie;
93  $minfo = "$minfo host=$masterhost" if (defined($masterhost));  sub mydie {
94  $minfo = "$minfo port=$masterport" if (defined($masterport));          unlink $fname;
95  $minfo = "$minfo user=$masteruser" if (defined($masteruser));          print @_;
96  $minfo = "$minfo password=$masterpassword" if (defined($masterpassword));          exit 1;
97    }
98  my $sinfo = "dbname=$slave";  
99  $sinfo = "$sinfo host=$slavehost" if (defined($slavehost));  my $minfo = MkInfo($master,$masterhost,$masterport,$masteruser,$masterpassword);
100  $sinfo = "$sinfo port=$slaveport" if (defined($slaveport));  my $sinfo = MkInfo($slave,$slavehost,$slaveport,$slaveuser,$slavepassword);
 $sinfo = "$sinfo user=$slaveuser" if (defined($slaveuser));  
 $sinfo = "$sinfo password=$slavepassword" if (defined($slavepassword));  
101    
102  print "Master connection is $minfo\n" if ($debug);  print "Master connection is $minfo\n" if ($debug);
103  print "Slave connection is $sinfo\n" if ($debug);  print "Slave connection is $sinfo\n" if ($debug);
104    
105  my $mconn = Pg::connectdb($minfo);  my $mconn = Connect($minfo);
106  if ($mconn->status != Pg::PGRES_CONNECTION_OK) {  my $sconn = Connect($sinfo);
107      print STDERR "Failed opening $minfo\n";  
108      unlink $fname;  $slavehost = 'localhost' if (! $slavehost);
109      exit 1;  $masterhost = 'localhost' if (! $masterhost);
110  }  
111  my $sconn = Pg::connectdb($sinfo);  my $slaveId = GetServerId($mconn, $slave, $slavehost);
112  if ($sconn->status != Pg::PGRES_CONNECTION_OK) {  
113      print STDERR "Failed opening $sinfo\n";  die "\n>>>>>>>>>>>>> ERROR: Can't GetServerId for $sinfo\n" if (! defined($slaveId));
114    
115    if ($slaveId < 0) {
116      unlink $fname;      unlink $fname;
117      exit 1;      die "\n>>>>>>>>>>>>> ERROR: GetServerId returned $slaveId < 0\n";
118  }  }
119    #SyncSync($mconn, $sconn);
120    
121  my $slaveId = GetSlaveId($mconn, $slave, defined($slavehost) ? $slavehost : hostname);  my $masterId = GetServerId($mconn, $master, $masterhost);
122  if ($slaveId < 0) {  
123    die "\n>>>>>>>>>>>>> ERROR: Can't GetServerId for $sinfo\n" if (! defined($masterId));
124    
125    if ($masterId < 0) {
126      unlink $fname;      unlink $fname;
127      die "\n>>>>>>>>>>>>> ERROR\n";      die "\n>>>>>>>>>>>>> ERROR: GetServerId returned $masterId < 0\n";
128  }  }
129  SyncSync($mconn, $sconn);  #SyncSync($sconn, $mconn);
130    
131    print "Using snapshot file: $snapshot\n" if ($debug);
132    
133  my $outf = new IO::File;  my $outf = new IO::File;
134  open $outf, ">$snapshot";  open $outf, ">$snapshot";
135  print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose);  print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose);
136  my $res = PrepareSnapshot($mconn, $sconn, $outf, $slaveId);  print "master id: $masterId slave id: $slaveId\n" if ($debug);
137    #my $res = PrepareSnapshot($mconn, $sconn, $outf, $masterId, $slaveId, $multimaster, $tables);
138    my $res = PrepareSnapshot($mconn, $sconn, $outf, $masterId, $slaveId, $multimaster);
139  close $outf;  close $outf;
140  if ($res < 0) {  if ($res < 0) {
141      unlink $fname;      unlink $fname;
# Line 125  if ($res == 0) Line 152  if ($res == 0)
152  my $inpf = new IO::File;  my $inpf = new IO::File;
153  open $inpf, "<$snapshot";  open $inpf, "<$snapshot";
154  print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose);  print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose);
155  $res = ApplySnapshot($sconn, $inpf);  #$res = ApplySnapshot($sconn, $inpf, $multimaster, $tables);
156    $res = ApplySnapshot($sconn, $inpf, $multimaster);
157  close $inpf;  close $inpf;
158  if ($res < 0) {  if ($res < 0) {
159      unlink $fname;      unlink $fname;

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.18

  ViewVC Help
Powered by ViewVC 1.1.26