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

Contents of /bin/Replicate

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Tue Aug 5 10:01:42 2003 UTC (20 years, 9 months ago) by dpavlin
Branch: MAIN
Changes since 1.3: +12 -6 lines
added port arguments, removed unused host and user arguments

1 #!/usr/bin/perl
2 # Replicate
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 lib "/usr/share/postgresql/contrib";
10
11 use IO::File;
12 use Getopt::Long;
13 use RServ;
14 use Sys::Hostname;
15
16 $| = 1;
17
18 $result = GetOptions("debug!", "verbose!", "help", "snapshot=s",
19 "masterhost=s", "slavehost=s",
20 "masteruser=s", "slaveuser=s",
21 "masterpassword=s", "slavepassword=s",
22 "masterport=i", "slaveport=i"
23 );
24
25 my $debug = $opt_debug || 0;
26 my $verbose = $opt_verbose || 0;
27 my $snapshot = $opt_snapshot || ".__Snapshot";
28 $snapshot .= '.' . $$;
29
30 if (defined($opt_help) || (scalar(@ARGV) < 2)) {
31 print "Usage: $0 --snapshot=file masterdb slavedb\n";
32 print "\t--masterhost=name --masterport=port\n";
33 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);
37 }
38
39 my $master = $ARGV[0] || "master";
40 my $slave = $ARGV[1] || "slave";
41
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";
58 $minfo = "$minfo host=$opt_masterhost" if (defined($opt_masterhost));
59 $minfo = "$minfo user=$opt_masteruser" if (defined($opt_masteruser));
60 $minfo = "$minfo password=$opt_masterpassword" if (defined($opt_masterpassword));
61 $minfo = "$minfo port=$opt_masterport" if (defined($opt_masterport));
62 my $sinfo = "dbname=$slave";
63 $sinfo = "$sinfo host=$opt_slavehost" if (defined($opt_slavehost));
64 $sinfo = "$sinfo user=$opt_slaveuser" if (defined($opt_slaveuser));
65 $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);
69 print "Slave connection is $sinfo\n" if ($debug);
70
71 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);
78 if ($sconn->status != PGRES_CONNECTION_OK) {
79 print STDERR "Failed opening $sinfo\n";
80 unlink $fname;
81 exit 1;
82 }
83
84 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);
90
91 my $outf = new IO::File;
92 open $outf, ">$snapshot";
93 print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose);
94 $res = PrepareSnapshot($mconn, $sconn, $outf, $slaveId);
95 close $outf;
96 if ($res < 0) {
97 unlink $fname;
98 die "\n>>>>>>>>>>>>> ERROR\n";
99 }
100 if ($res == 0)
101 {
102 print "\n>>>>>>>>>>>>> DBases are sync-ed\n" if ($verbose);
103 unlink $snapshot unless ($debug);
104 unlink $fname;
105 exit(0);
106 }
107
108 my $inpf = new IO::File;
109 open $inpf, "<$snapshot";
110 print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose);
111 $res = ApplySnapshot($sconn, $inpf);
112 close $inpf;
113 if ($res < 0) {
114 unlink $fname;
115 die "\n>>>>>>>>>>>>> ERROR\n";
116 }
117
118 if ($res > 0)
119 {
120 print "Snapshot applied\n" if ($verbose);
121 unlink $snapshot unless ($debug);
122 SyncSync($mconn, $sconn);
123 }
124
125 unlink $fname;
126 exit(0);
127
128 ###########################################################################
129
130 sub SyncSync
131 {
132 ($mconn, $sconn) = @_;
133
134 print "\n>>>>>>>>>>>>> Sync SyncID\n\n" if ($verbose);
135 print "Get last SyncID from Slave DB\n" if ($verbose);
136 $syncid = GetSyncID($sconn);
137 if ($syncid > 0)
138 {
139 print "Last SyncID applied: $syncid\n" if ($verbose);
140 print "Sync SyncID\n" if ($verbose);
141
142 $res = SyncSyncID($mconn, $slaveId, $syncid);
143
144 print "Succeeded\n" if (($res > 0) && ($verbose));
145 }
146 }

  ViewVC Help
Powered by ViewVC 1.1.26