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", "host=s", |
20 |
"masteruser=s", "slaveuser=s", "user=s", |
21 |
"masterpassword=s", "slavepassword=s", "password=s"); |
22 |
|
23 |
my $debug = $opt_debug || 0; |
24 |
my $verbose = $opt_verbose || 0; |
25 |
my $snapshot = $opt_snapshot || ".__Snapshot"; |
26 |
$snapshot .= '.' . $$; |
27 |
|
28 |
if (defined($opt_help) || (scalar(@ARGV) < 2)) { |
29 |
print "Usage: $0 --snapshot=file --host=name --user=name --password=string masterdb slavedb\n"; |
30 |
print "\t--masterhost=name --masteruser=name --masterpassword=string\n"; |
31 |
print "\t--slavehost=name --slaveuser=name --slavepassword=string\n"; |
32 |
exit ((scalar(@ARGV) < 2)? 1:0); |
33 |
} |
34 |
|
35 |
my $master = $ARGV[0] || "master"; |
36 |
my $slave = $ARGV[1] || "slave"; |
37 |
|
38 |
$masterhost = $opt_masterhost || hostname; |
39 |
$slavehost = $opt_slavehost || hostname; |
40 |
|
41 |
# first, let's check if a instance is alrealy running |
42 |
|
43 |
$str = $master.'@'.$masterhost.':'.$slave.'@'.$slavehost; |
44 |
my $fname = "/tmp/.lock_rserv-$str"; |
45 |
if (-e $fname) { |
46 |
print STDERR "rServ is already running for $str. Remove $fname to override this.\n"; |
47 |
exit(1); |
48 |
} |
49 |
open ARQ, ">$fname" || die "Cannot open $fname: $!\n"; |
50 |
print ARQ "$$\n"; |
51 |
close ARQ; |
52 |
|
53 |
my $minfo = "dbname=$master"; |
54 |
$minfo = "$minfo host=$opt_masterhost" if (defined($opt_masterhost)); |
55 |
$minfo = "$minfo user=$opt_masteruser" if (defined($opt_masteruser)); |
56 |
$minfo = "$minfo password=$opt_masterpassword" if (defined($opt_masterpassword)); |
57 |
my $sinfo = "dbname=$slave"; |
58 |
$sinfo = "$sinfo host=$opt_slavehost" if (defined($opt_slavehost)); |
59 |
$sinfo = "$sinfo user=$opt_slaveuser" if (defined($opt_slaveuser)); |
60 |
$sinfo = "$sinfo password=$opt_slavepassword" if (defined($opt_slavepassword)); |
61 |
|
62 |
print "Master connection is $minfo\n" if ($debug); |
63 |
print "Slave connection is $sinfo\n" if ($debug); |
64 |
|
65 |
my $mconn = Pg::connectdb($minfo); |
66 |
if ($mconn->status != PGRES_CONNECTION_OK) { |
67 |
print STDERR "Failed opening $minfo\n"; |
68 |
unlink $fname; |
69 |
exit 1; |
70 |
} |
71 |
my $sconn = Pg::connectdb($sinfo); |
72 |
if ($sconn->status != PGRES_CONNECTION_OK) { |
73 |
print STDERR "Failed opening $sinfo\n"; |
74 |
unlink $fname; |
75 |
exit 1; |
76 |
} |
77 |
|
78 |
my $slaveId = GetSlaveId($mconn, $slave, defined($opt_slavehost) ? $opt_slavehost : hostname); |
79 |
if ($slaveId < 0) { |
80 |
unlink $fname; |
81 |
die "\n>>>>>>>>>>>>> ERROR\n"; |
82 |
} |
83 |
SyncSync($mconn, $sconn); |
84 |
|
85 |
my $outf = new IO::File; |
86 |
open $outf, ">$snapshot"; |
87 |
print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose); |
88 |
$res = PrepareSnapshot($mconn, $sconn, $outf, $slaveId); |
89 |
close $outf; |
90 |
if ($res < 0) { |
91 |
unlink $fname; |
92 |
die "\n>>>>>>>>>>>>> ERROR\n"; |
93 |
} |
94 |
if ($res == 0) |
95 |
{ |
96 |
print "\n>>>>>>>>>>>>> DBases are sync-ed\n" if ($verbose); |
97 |
unlink $snapshot unless ($debug); |
98 |
unlink $fname; |
99 |
exit(0); |
100 |
} |
101 |
|
102 |
my $inpf = new IO::File; |
103 |
open $inpf, "<$snapshot"; |
104 |
print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose); |
105 |
$res = ApplySnapshot($sconn, $inpf); |
106 |
close $inpf; |
107 |
if ($res < 0) { |
108 |
unlink $fname; |
109 |
die "\n>>>>>>>>>>>>> ERROR\n"; |
110 |
} |
111 |
|
112 |
if ($res > 0) |
113 |
{ |
114 |
print "Snapshot applied\n" if ($verbose); |
115 |
unlink $snapshot unless ($debug); |
116 |
SyncSync($mconn, $sconn); |
117 |
} |
118 |
|
119 |
unlink $fname; |
120 |
exit(0); |
121 |
|
122 |
########################################################################### |
123 |
|
124 |
sub SyncSync |
125 |
{ |
126 |
($mconn, $sconn) = @_; |
127 |
|
128 |
print "\n>>>>>>>>>>>>> Sync SyncID\n\n" if ($verbose); |
129 |
print "Get last SyncID from Slave DB\n" if ($verbose); |
130 |
$syncid = GetSyncID($sconn); |
131 |
if ($syncid > 0) |
132 |
{ |
133 |
print "Last SyncID applied: $syncid\n" if ($verbose); |
134 |
print "Sync SyncID\n" if ($verbose); |
135 |
|
136 |
$res = SyncSyncID($mconn, $slaveId, $syncid); |
137 |
|
138 |
print "Succeeded\n" if (($res > 0) && ($verbose)); |
139 |
} |
140 |
} |