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 |
|
|
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 $multiplemaster = 0; |
26 |
|
|
27 |
my $result = GetOptions( |
my $result = GetOptions( |
28 |
"debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help, |
"debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help, |
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 |
|
"multiplemaster" => \$multiplemaster, |
35 |
); |
); |
36 |
|
|
37 |
if (defined($help) || (scalar(@ARGV) < 2)) { |
if (defined($help) || (scalar(@ARGV) < 2)) { |
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 |
} |
} |
56 |
|
|
57 |
my $master = $ARGV[0] || "master"; |
my $master = $ARGV[0] || "master"; |
58 |
my $slave = $ARGV[1] || "slave"; |
my $slave = $ARGV[1] || "slave"; |
59 |
|
my $tables = $#ARGV < 2 ? undef : { map {($_, undef)} @ARGV[2..$#ARGV] }; |
60 |
|
|
61 |
# first, let's check if a instance is alrealy running |
# first, let's check if a instance is alrealy running |
62 |
|
|
123 |
exit 1; |
exit 1; |
124 |
} |
} |
125 |
|
|
126 |
$slavehost = hostname if (! $slavehost); |
$slavehost = 'localhost' if (! $slavehost); |
127 |
my $slaveId = GetSlaveId($mconn, $slave, $slavehost); |
$masterhost = 'localhost' if (! $masterhost); |
128 |
|
|
129 |
die "\n>>>>>>>>>>>>> ERROR: Can't GetSlaveID for $sinfo\n" if (! $slaveId); |
my $slaveId = GetServerId($mconn, $slave, $slavehost); |
130 |
|
|
131 |
|
die "\n>>>>>>>>>>>>> ERROR: Can't GetServerId for $sinfo\n" if (! defined($slaveId)); |
132 |
|
|
133 |
if ($slaveId < 0) { |
if ($slaveId < 0) { |
134 |
unlink $fname; |
unlink $fname; |
135 |
die "\n>>>>>>>>>>>>> ERROR\n"; |
die "\n>>>>>>>>>>>>> ERROR: GetServerId returned $slaveId < 0\n"; |
136 |
} |
} |
137 |
SyncSync($mconn, $sconn); |
SyncSync($mconn, $sconn); |
138 |
|
|
139 |
|
my $masterId = 0; |
140 |
|
if ($multiplemaster || $slaveId == 0) { |
141 |
|
$masterId = GetServerId($sconn, $master, $masterhost); |
142 |
|
|
143 |
|
die "\n>>>>>>>>>>>>> ERROR: Can't GetServerId for $sinfo\n" if (! defined($masterId)); |
144 |
|
|
145 |
|
if ($masterId < 0) { |
146 |
|
unlink $fname; |
147 |
|
die "\n>>>>>>>>>>>>> ERROR: GetServerId returned $masterId < 0\n"; |
148 |
|
} |
149 |
|
SyncSync($sconn, $mconn); |
150 |
|
} |
151 |
|
|
152 |
my $outf = new IO::File; |
my $outf = new IO::File; |
153 |
open $outf, ">$snapshot"; |
open $outf, ">$snapshot"; |
154 |
print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose); |
print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose); |
155 |
my $res = PrepareSnapshot($mconn, $sconn, $outf, $slaveId); |
print "master id: $masterId slave id: $slaveId\n" if ($debug); |
156 |
|
my $res = PrepareSnapshot($mconn, $sconn, $outf, $masterId, $slaveId, $tables); |
157 |
close $outf; |
close $outf; |
158 |
if ($res < 0) { |
if ($res < 0) { |
159 |
unlink $fname; |
unlink $fname; |
170 |
my $inpf = new IO::File; |
my $inpf = new IO::File; |
171 |
open $inpf, "<$snapshot"; |
open $inpf, "<$snapshot"; |
172 |
print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose); |
print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose); |
173 |
$res = ApplySnapshot($sconn, $inpf); |
$res = ApplySnapshot($sconn, $inpf, $tables); |
174 |
close $inpf; |
close $inpf; |
175 |
if ($res < 0) { |
if ($res < 0) { |
176 |
unlink $fname; |
unlink $fname; |