-
Notifications
You must be signed in to change notification settings - Fork 0
/
myimporter
executable file
·277 lines (194 loc) · 6.05 KB
/
myimporter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#! /usr/bin/env perl
use strict;
use warnings;
use threads;
use threads::shared;
use Thread::Queue;
use Time::HiRes qw(usleep gettimeofday tv_interval);
use Time::Duration;
use Getopt::Long;
use Pod::Usage;
use File::Basename;
our ($host, $port, $database, $user, $password, $threads, $socket, $help,
$noop);
my $filterfile;
# defaults
$port = 3306;
$socket = "/tmp/mysql.sock";
$password = "";
$database = "";
$noop = 0;
$threads = `grep -c "^processor" /proc/cpuinfo`;
$filterfile = undef;
my @suffixes = (".sql", ".sql.gz");
GetOptions (
"host|h=s" => \$host, #database host
"port|P=i" => \$port, #database port
"user|u=s" => \$user, #database user
"database|db|d=s" => \$database, #schema
"password|p=s" => \$password, #database password
"socket|S=s" => \$socket,
"filter|f=s" => \$filterfile,
"threads|t=i" => \$threads, #number of threads
"help" => \$help,
"noop" => \$noop,
) or die("Error parsing options");
pod2usage(-verbose=>2) if $help;
pod2usage(-verbose=>1) unless $database;
pod2usage(-verbose=>1) unless (($host and $port) || $socket);
pod2usage(-verbose=>1) unless $user;
$host = "localhost" unless ($host);
print "Host: $host\nPort: $port\nSocket: $socket\nThreads: $threads\n";
my @filter;
if (defined($filterfile)) {
if (-f $filterfile) {
open(FILE, "$filterfile") or die("Error opening $filterfile: $!");
@filter = <FILE>;
chomp(@filter);
} else {
die("No such file or directory: $filterfile");
}
}
my $counter = 0;
share $counter;
$| = 1;
my $q = Thread::Queue->new();
my $t0 = [gettimeofday]; # Import start time.
foreach my $arg (@ARGV) {
if ($filterfile) {
if (is_filtered(\@filter, $arg)) {
print "INFO: $arg matches filter list, skipping ...\n";
next;
} else {
$q->enqueue($arg);
}
} else {
$q->enqueue($arg);
}
}
for (my $i = 0; $i <= $threads - 1; $i++) {
my $thr = threads->create(\&worker);
$counter++;
}
{ lock($counter); cond_wait($counter) until $counter == 0; }
print get_timestring() . " - All threads done! Cleaning up.\n";
my $t1 = [gettimeofday]; # Import end time.
my $elapsed = tv_interval($t0, $t1);
print "Run time ", duration($elapsed), ".\n";
# join old threads when finished
my @threads = threads->list();
foreach my $thr (@threads) {
$thr->join();
}
exit(0);
sub get_timestring {
my $t_string = localtime();
return $t_string;
}
sub worker {
print(get_timestring() . " - T" . threads->tid() . " started.\n");
while ($q->pending()) {
my $gz = 0;
if (defined(my $item = $q->dequeue_nb())) {
print(get_timestring() . " - T" . threads->tid() . ": Processing $item ...\n");
if ( $item =~ m/.*\.gz$/ ) {
$gz = 1;
}
my $arg =
($gz ? "gunzip -c $item" : "cat $item") .
"|mysql --user=$user" .
($password ? " --password=$password" : "") .
($host ? " --host=$host" : "") .
($socket ? " --socket=$socket" : "") .
($database ? " --database=$database" : "");
if ($noop) {
{
lock ($counter);
print("$arg\n");
}
} else {
my $t0 = [gettimeofday];
system($arg);
my $t1 = [gettimeofday];
my $elapsed = tv_interval $t0, $t1;
{
lock ($counter);
print("TIMING: $item\t$elapsed\t (". duration($elapsed) .")\n");
}
}
} else {
usleep(100);
}
}
{ lock ($counter); $counter--; cond_signal($counter); }
print(get_timestring() . " - T" . threads->tid() . " exited, $counter threads still running.\n");
}
sub is_filtered {
my ($filter, $filename) = @_;
my ($tablename, undef, undef) = fileparse($filename, @suffixes);
$tablename =~ s/^[^\.]*?\.(.*)/$1/;
my $filtered = grep(/$tablename/, @$filter);
return ($filtered);
}
__END__
=head1 NAME
myimporter - Import dump files produced by B<mydumper>
(L<https://launchpad.net/mydumper>).
=head1 SYNOPSIS
myimporter [options] [file.sql file2.sql.gz ...]
=head1 OPTIONS
=over 8
=item B<--host>
Host to connect to.
=item B<--port>
Port, default: 3306
=item B<--user>
Database user.
=item B<--pass>
Database Password.
=item B<--database>
Database (Schema) to import into. (Must exist).
=item B<--socket>
Specifies a local socket. Alternative to B<Host> and B<Port>.
=item B<--threads>
Number of threads to use. Defaults to number of processors found in
/proc/cpuinfo.
=item B<--filter>
Filename with tables to filter from the import. One table name per line.
=back
=head1 DESCRIPTION
B<myimporter> will import all files given as arguments in the order given.
Recommendation is to list the biggest (or slowest to import) tables first.
=head1 EXAMPLE
=over 4
=item B<Backing up database: I<test>>
mydumper --host db-server.example.com --user backup-user --password pass
--database test --threads 6 --outputdir /tmp/dump.test --compress
mysqldump -v --no-data --routines --host=db-server2
--user=backup-user --password=pass test > /tmp/dump.test/.structure.sql
=item B<Importing previously backed up database I<test>>
mysql test E<lt> /tmp/dump.test/.structure.sql
ls -1hS /tmp/dump.test/*.sql.gz | xargs myimporter --threads 8 --database test
--user root --socket /mysql/main/run/mysql.sock
ls -1hS /tmp/dump.test/*.sql.gz | xargs myimporter --threads 8 --database test
--user root --socket /tmp/mysql.sock --filter /tmp/filter.txt
=back
=head1 DEPENDENCIES
=over 8
=item B<threads>
=item B<threads::shared>
=item B<Thread::Queue>
=item B<Time::HiRes>
=item B<Time::Duration>
=item B<Getopt::Long>
=item B<Pod::Usage>
=back
=head1 TOOD
=over 8
=item Restore is limited to one specific database.
=back
=head1 AUTHOR
Mikael Fridh
=head1 ACKNOWLEDGEMENTS
Domas Mituzas obviously.
=cut