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 |
|
|
22 |
|
|
23 |
my $debug = $opt_debug || 0; |
my $debug = $opt_debug || 0; |
24 |
my $verbose = $opt_verbose || 0; |
my $verbose = $opt_verbose || 0; |
25 |
my $snapshot = $opt_snapshot || "__Snapshot"; |
my $snapshot = $opt_snapshot || ".__Snapshot"; |
26 |
|
$snapshot .= '.' . $$; |
27 |
|
|
28 |
if (defined($opt_help) || (scalar(@ARGV) < 2)) { |
if (defined($opt_help) || (scalar(@ARGV) < 2)) { |
29 |
print "Usage: $0 --snapshot=file --host=name --user=name --password=string masterdb slavedb\n"; |
print "Usage: $0 --snapshot=file --host=name --user=name --password=string masterdb slavedb\n"; |
34 |
|
|
35 |
my $master = $ARGV[0] || "master"; |
my $master = $ARGV[0] || "master"; |
36 |
my $slave = $ARGV[1] || "slave"; |
my $slave = $ARGV[1] || "slave"; |
37 |
my $server = 0; |
|
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"; |
my $minfo = "dbname=$master"; |
54 |
$minfo = "$minfo host=$opt_masterhost" if (defined($opt_masterhost)); |
$minfo = "$minfo host=$opt_masterhost" if (defined($opt_masterhost)); |
63 |
print "Slave connection is $sinfo\n" if ($debug); |
print "Slave connection is $sinfo\n" if ($debug); |
64 |
|
|
65 |
my $mconn = Pg::connectdb($minfo); |
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); |
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 |
$RServ::quiet = !$verbose; |
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); |
SyncSync($mconn, $sconn); |
84 |
|
|
85 |
my $outf = new IO::File; |
my $outf = new IO::File; |
86 |
open $outf, ">$snapshot"; |
open $outf, ">$snapshot"; |
87 |
print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose); |
print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose); |
88 |
$res = PrepareSnapshot($mconn, $outf, $server); |
$res = PrepareSnapshot($mconn, $sconn, $outf, $slaveId); |
89 |
close $outf; |
close $outf; |
90 |
die "\n>>>>>>>>>>>>> ERROR\n" if $res < 0; |
if ($res < 0) { |
91 |
|
unlink $fname; |
92 |
|
die "\n>>>>>>>>>>>>> ERROR\n"; |
93 |
|
} |
94 |
if ($res == 0) |
if ($res == 0) |
95 |
{ |
{ |
96 |
print "\n>>>>>>>>>>>>> DBases are sync-ed\n" if ($verbose); |
print "\n>>>>>>>>>>>>> DBases are sync-ed\n" if ($verbose); |
97 |
exit(0); |
unlink $snapshot unless ($debug); |
98 |
|
unlink $fname; |
99 |
|
exit(0); |
100 |
} |
} |
101 |
|
|
102 |
my $inpf = new IO::File; |
my $inpf = new IO::File; |
104 |
print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose); |
print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose); |
105 |
$res = ApplySnapshot($sconn, $inpf); |
$res = ApplySnapshot($sconn, $inpf); |
106 |
close $inpf; |
close $inpf; |
107 |
die "\n>>>>>>>>>>>>> ERROR\n" if $res < 0; |
if ($res < 0) { |
108 |
|
unlink $fname; |
109 |
|
die "\n>>>>>>>>>>>>> ERROR\n"; |
110 |
|
} |
111 |
|
|
112 |
if ($res > 0) |
if ($res > 0) |
113 |
{ |
{ |
114 |
print "Snapshot applied\n" if ($verbose); |
print "Snapshot applied\n" if ($verbose); |
115 |
unlink $snapshot unless ($debug); |
unlink $snapshot unless ($debug); |
116 |
SyncSync($mconn, $sconn); |
SyncSync($mconn, $sconn); |
117 |
} |
} |
118 |
|
|
119 |
|
unlink $fname; |
120 |
exit(0); |
exit(0); |
121 |
|
|
122 |
########################################################################### |
########################################################################### |
133 |
print "Last SyncID applied: $syncid\n" if ($verbose); |
print "Last SyncID applied: $syncid\n" if ($verbose); |
134 |
print "Sync SyncID\n" if ($verbose); |
print "Sync SyncID\n" if ($verbose); |
135 |
|
|
136 |
$res = SyncSyncID($mconn, $server, $syncid); |
$res = SyncSyncID($mconn, $slaveId, $syncid); |
137 |
|
|
138 |
print "Succeeded\n" if (($res > 0) && ($verbose)); |
print "Succeeded\n" if (($res > 0) && ($verbose)); |
139 |
} |
} |