blib/script/narada-mysqldump

filter: Statementstmt 38.2% 86 / 225 Branchbran 1.8% 1 / 54 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 66.6% 28 / 42 Podpod n/a totaltotal 33.0% 115 / 348 CC 39 SCAR 31.5
CC
39
Cov
29%
init_globals
72.0
del_dumps_except
72.0
get_key
42.0
CRAP 23.2
« blib/script/narada-setup-mysql ↑ Summary blib/script/narada-new-1 »
1 #!/usr/bin/env perl
2 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = 'v2.3.4';
8  
9 2 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use lib "$FindBin::Bin/../lib/perl5";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Narada;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Narada::Lock qw( exclusive_lock unlock_new unlock );;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Narada::Config qw( get_config get_config_line get_db_config );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Fcntl qw(F_SETFD);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use constant IS_NARADA => Narada::detect() eq 'narada';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use constant CONFIG_DIR => IS_NARADA ? 'mysql' : 'db';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use constant SQL_DIR => IS_NARADA ? 'var/mysql' : 'var/sql';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use constant SCHEME => SQL_DIR.'/db.scheme.sql';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use constant REQUIRED_TABLE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use constant OPTIONAL_TABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use constant TIMELOCAL_MONTH=> 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use constant STAT_MTIME => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use constant DESC_FIELD => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use constant DESC_TYPE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use constant DESC_NULL => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use constant DESC_KEY => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use constant DESC_DEFAULT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use constant DESC_EXTRA => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # GLOBALS: $::dbh, $::mycnf, $::MYSQLDUMP
36  
37  
38 2 ▶main(@ARGV) if !caller;
Branch: 1 branch
BranchTrueFalseTotal
unless &CORE::GLOBAL::caller() 0 2 2
39  
40  
41 0 ▶sub err { die "narada-mysqldump: @_\n" }
Subroutine err: not called CC=1 SCAR=6.9
42  
43 sub main {
44 0 ▶ die "Usage: narada-mysqldump\n" if @_;
Subroutine main: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
45  
46 0 ▶ init_globals() or return;
Branch: 1 branch
BranchTrueFalseTotal
init_globals() or return 0 0 0
47  
48 0 exclusive_lock();
49 0 unlock_new();
50  
51 0 my ($full, $incremental, $ignore) = list_tables();
52 0 my $unchanged = detect_unchanged($full);
53  
54 0 del_dumps_except($incremental, $unchanged);
55 0 dump_scheme_except($ignore);
56 0 dump_full($full, $unchanged);
57 0 dump_incremental($incremental);
58  
59 # To correctly detect_unchanged() we should guarantee next table's
60 # Update_time after executing narada-mysqldump will be at least 1
61 # second later than previous (which kept in dump file's mtime).
62 0 sleep 1;
63  
64 0 unlock();
65  
66 0 return;
67 }
68  
69 sub init_globals {
70 0 ▶ my $db = get_db_config() or return;
Subroutine init_globals: not called CC=8 SCAR=42.8
Branch: 1 branch
BranchTrueFalseTotal
my $db = get_db_config() or return 0 0 0
71 $::dbh = DBI->connect($db->{dsn}, $db->{login}, $db->{pass},
72 0 ▶ {RaiseError=>1}) or err DBI->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$main::dbh = "DBI"->connect($db->{"dsn"}, $db->{"login"}, $db->{"pass"}, {"RaiseError", 1}) or "DBI"->err->errstr 0 0 0
73 0 $::mycnf = tempfile(DIR=>'tmp');
74 0 print {$::mycnf} "[client]\n";
75 0 print {$::mycnf} "user = $db->{login}\n";
76 0 ▶ print {$::mycnf} "password = $db->{pass}\n" if length $db->{pass}; ## no critic (ProhibitPostfixControls)
Branch: 1 branch
BranchTrueFalseTotal
if length $db->{"pass"} 0 0 0
77 0 ▶ print {$::mycnf} "host = $db->{host}\n" if length $db->{host}; ## no critic (ProhibitPostfixControls)
Branch: 1 branch
BranchTrueFalseTotal
if length $db->{"host"} 0 0 0
78 0 ▶ print {$::mycnf} "port = $db->{port}\n" if length $db->{port}; ## no critic (ProhibitPostfixControls)
Branch: 1 branch
BranchTrueFalseTotal
if length $db->{"port"} 0 0 0
79 0 $::mycnf->flush;
80 0 ▶ sysseek $::mycnf, 0, 0 or err "sysseek: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysseek $main::mycnf, 0, 0 or err("sysseek: $!") 0 0 0
81 0 ▶ fcntl $::mycnf, F_SETFD, 0 or err "fcntl: $!";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $main::mycnf, 2, 0 or err("fcntl: $!") 0 0 0
82 0 my $fd = fileno $::mycnf;
83 0 $::MYSQLDUMP = "mysqldump --defaults-file=/proc/self/fd/\Q$fd\E \Q$db->{db}\E";
84 0 return 1;
85 }
86  
87 sub list_tables {
88 0 ▶ my $incremental = load_db_conf('dump/incremental', REQUIRED_TABLE);
Subroutine list_tables: not called CC=3 SCAR=24.8
89 0 my $empty = load_db_conf('dump/empty', REQUIRED_TABLE);
90 0 my $ignore = load_db_conf('dump/ignore', OPTIONAL_TABLE);
91 0 my %other = map {$_=>1} @{$incremental}, @{$empty}, @{$ignore};
92 0 my @full;
93 0 for my $table (@{ $::dbh->selectcol_arrayref('SHOW TABLES') }) {
94 0 ▶ next if $other{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $other{$table} 0 0 0
95 0 push @full, $table;
96 }
97 0 return ([sort @full], $incremental, $ignore);
98 }
99  
100 sub load_db_conf {
101 0 ▶ my ($conf, $required) = @_;
Subroutine load_db_conf: not called CC=4 SCAR=30.0
102 0 my @conf = eval { split /\s*\n/xms, get_config(CONFIG_DIR."/$conf") };
103 0 my @tables;
104 0 for my $table (@conf) {
105 0 local ($::dbh->{RaiseError}, $::dbh->{PrintError});
106 0 my $desc = $::dbh->selectall_arrayref('DESC '.$table);
107 0 ▶ if ($desc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($desc) { } 0 0 0
elsif ($required == 0) { } 0 0 0
108 0 push @tables, $table;
109 }
110 elsif ($required == REQUIRED_TABLE) {
111 0 err "Table $table listed in ".CONFIG_DIR."/$conf does not exists\n";
112 }
113 }
114 0 return [sort @tables];
115 }
116  
117 sub detect_unchanged {
118 0 ▶ my ($full) = @_;
Subroutine detect_unchanged: not called CC=4 SCAR=30.0
119 0 my @unchanged;
120 0 for my $table (@{$full}) {
121 0 my $file = SQL_DIR."/$table.sql";
122 0 ▶ if (-f $file && mtime($file) == get_table_status($table, 'Update_time')) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file and mtime($file) == get_table_status($table, "Update_time")) 0 0 0
Condition0%0/3
-f $file and mtime($file) == get_table_status($table, "Update_time")
!ll&&!rl&&r
000
MC/DC0%0/2
-f $file and mtime($file) == get_table_status($table, "Update_time")
-f $file mtime($file) == get_table_status($table, "Update_time")
Truth table0%0/3
A and B
A= -f $fileB= mtime($file) == get_table_status($table, "Update_time")
ABresult
0X0
100
111
123 0 push @unchanged, $table;
124 }
125 }
126 0 return \@unchanged;
127 }
128  
129 sub del_dumps_except {
130 0 ▶ my ($incremental, $unchanged) = @_;
Subroutine del_dumps_except: not called CC=8 SCAR=42.8
131 0 my %incremental = map {$_=>1} @{$incremental};
132 0 my %unchanged = map {$_=>1} @{$unchanged};
133 0 for my $file (glob SQL_DIR.'/*.sql') {
134 0 ▶ if ($file =~ m{\A\Q${\SQL_DIR}\E/([^.]*)[.]sql\z}xms) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ m[\A\Q${\'var/mysql';}\E/([^.]*)[.]sql\z]msx) { } 0 0 0
elsif ($file =~ m[\A\Q${\'var/mysql';}\E/([^.]*)[.]\d+-(\d+)[.]sql\z]msx) { } 0 0 0
135 0 my $table = $1;
136 0 ▶ next if $unchanged{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $unchanged{$table} 0 0 0
137 }
138 0 elsif ($file =~ m{\A\Q${\SQL_DIR}\E/([^.]*)[.]\d+-(\d+)[.]sql\z}xms) {
139 0 my $table = $1;
140 0 ▶ next if $incremental{$table}
Branch: 1 branch
BranchTrueFalseTotal
if $incremental{$table} and mtime($file) >= get_table_status($table, "Create_time") 0 0 0
Condition0%0/3
$incremental{$table} and mtime($file) >= get_table_status($table, "Create_time")
!ll&&!rl&&r
000
MC/DC0%0/2
$incremental{$table} and mtime($file) >= get_table_status($table, "Create_time")
$incremental{$table} mtime($file) >= get_table_status($table, "Create_time")
Truth table0%0/3
A and B
A= $incremental{$table}B= mtime($file) >= get_table_status($table, "Create_time")
ABresult
0X0
100
111
141 && mtime($file) >= get_table_status($table, 'Create_time');
142 }
143 0 ▶ unlink $file or err "unlink($file): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or err("unlink($file): $!\n") 0 0 0
144 }
145 0 return;
146 }
147  
148 sub dump_scheme_except {
149 0 ▶ my ($ignore) = @_;
Subroutine dump_scheme_except: not called CC=1 SCAR=6.9
150 0 my $db = get_config_line(CONFIG_DIR.'/db');
151 0 my $tables = join q{ }, map {"--ignore-table=\Q$db\E.\Q$_\E"} @{$ignore};
152 0 mysqldump("--opt -d $tables", SCHEME, time);
153 0 return;
154 }
155  
156 sub dump_full {
157 0 ▶ my ($full, $unchanged) = @_;
Subroutine dump_full: not called CC=3 SCAR=24.8
158 0 my %unchanged = map {$_=>1} @{$unchanged};
159 0 for my $table (@{$full}) {
160 0 ▶ next if $unchanged{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $unchanged{$table} 0 0 0
161 0 my $file = SQL_DIR."/$table.sql";
162 0 my $t = get_table_status($table, 'Update_time');
163 0 mysqldump("--opt -t \Q$table\E", $file, $t);
164 }
165 0 return;
166 }
167  
168 sub dump_incremental {
169 0 ▶ my ($incremental) = @_;
Subroutine dump_incremental: not called CC=3 SCAR=24.8
170 0 for my $table (@{$incremental}) {
171 0 my $key = get_key($table);
172 0 my $prev = max(0, map {m/-(\d+)[.]sql\z/xms} glob SQL_DIR."/\Q$table\E.*.sql");
173 0 my $next = get_table_status($table, 'Auto_increment');
174 0 ▶ if ($prev < $next-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev < $next - 1) 0 0 0
175 0 my $from = $prev+1;
176 0 my $to = $next-1;
177 0 my $file = SQL_DIR."/$table.$from-$to.sql";
178 0 my $where= "$key>=$from AND $key<=$to";
179 0 my $t = get_table_status($table, 'Update_time');
180 0 mysqldump("--opt -t -w \Q$where\E \Q$table\E", $file, $t);
181 }
182 }
183 0 return;
184 }
185  
186 ###
187  
188 sub get_table_status {
189 0 ▶ my ($table, $field) = @_;
Subroutine get_table_status: not called CC=3 SCAR=24.8
190 my $val = $::dbh->selectrow_hashref('SHOW TABLE STATUS LIKE ?',
191 0 undef, $table)->{$field};
192 0 ▶ if ($field =~ /_time\z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /_time\z/msx) 0 0 0
193 0 ▶ if (!defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
194 0 $val = 0;
195 }
196 else {
197 0 my @datetime = reverse split /\D+/xms, $val;
198 0 $datetime[TIMELOCAL_MONTH]--;
199 0 $val = timelocal(@datetime);
200 }
201 }
202 0 return $val;
203 }
204  
205 sub get_key {
206 0 ▶ my ($table) = @_;
Subroutine get_key: not called CC=6 SCAR=37.4
207 0 my $desc = $::dbh->selectall_arrayref('DESC '.$table);
208 err "First field in table $table must be: INT AUTO_INCREMENT PRIMARY KEY\n"
209 0 if !(@{ $desc }
210 && $desc->[0][DESC_TYPE]=~/\A\w*int\b/xms
211 && $desc->[0][DESC_KEY] eq 'PRI'
212 && $desc->[0][DESC_EXTRA] eq 'auto_increment'
213 0 ▶ && 1 == grep {$_->[DESC_KEY] eq 'PRI'} @{$desc});
Branch: 1 branch
BranchTrueFalseTotal
unless @{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx and $desc->[0][3] eq "PRI" and $desc->[0][5] eq "auto_increment" and 1 == grep({$_->[3] eq "PRI";} @{$desc;}) 0 0 0
Condition0%0/12
@{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx and $desc->[0][3] eq "PRI" and $desc->[0][5] eq "auto_increment" and 1 == grep({$_->[3] eq "PRI";} @{$desc;})
!ll&&!rl&&r
000
@{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx and $desc->[0][3] eq "PRI" and $desc->[0][5] eq "auto_increment"
!ll&&!rl&&r
000
@{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx and $desc->[0][3] eq "PRI"
!ll&&!rl&&r
000
@{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx
!ll&&!rl&&r
000
MC/DC0%0/5
@{$desc;} and $desc->[0][1] =~ /\A\w*int\b/msx and $desc->[0][3] eq "PRI" and $desc->[0][5] eq "auto_increment" and 1 == grep({$_->[3] eq "PRI";} @{$desc;})
@{$desc;} $desc->[0][1] =~ /\A\w*int\b/msx $desc->[0][3] eq "PRI" $desc->[0][5] eq "auto_increment" 1 == grep({$_->[3] eq "PRI";} @{$desc;})
Truth table0%0/6
A and B and C and D and E
A= @{$desc;}B= $desc->[0][1] =~ /\A\w*int\b/msxC= $desc->[0][3] eq "PRI"D= $desc->[0][5] eq "auto_increment"E= 1 == grep({$_->[3] eq "PRI";} @{$desc;})
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
214 0 return $desc->[0][DESC_FIELD];
215 }
216  
217 sub mtime {
218 0 ▶ my ($file) = @_;
Subroutine mtime: not called CC=1 SCAR=6.9
219 0 return (stat $file)[STAT_MTIME];
220 }
221  
222 sub mysqldump {
223 0 ▶ my ($opt, $file, $t) = @_;
Subroutine mysqldump: not called CC=4 SCAR=30.0
224 0 ▶ system("$::MYSQLDUMP $opt > \Q$file\E.tmp")
Branch: 1 branch
BranchTrueFalseTotal
system("$main::MYSQLDUMP $opt > \Q$file\E.tmp") == 0 or err("system($main::MYSQLDUMP $opt > $file.tmp): $?") 0 0 0
225 == 0 or err "system($::MYSQLDUMP $opt > $file.tmp): $?";
226 0 ▶ rename "$file.tmp", $file or err "rename($file.tmp, $file): $!";
Branch: 1 branch
BranchTrueFalseTotal
rename "$file.tmp", $file or err("rename($file.tmp, $file): $!") 0 0 0
227 0 ▶ utime $t, $t, $file or err "utime($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
utime $t, $t, $file or err("utime($file): $!") 0 0 0
228 0 return;
229 }
230  
231  
232 2 1; # Magic true value required at end of module
233 __END__
234  
235 =encoding utf8
236  
237 =head1 NAME
238  
239 narada-mysqldump - dump project database
240  
241 =head1 VERSION
242  
243 This document describes narada-mysqldump version v2.3.4
244  
245  
246 =head1 USAGE
247  
248 narada-mysqldump
249  
250  
251 =head1 DESCRIPTION
252  
253 Should be executed in project deploy directory (or Narada 1.x project root
254 directory).
255  
256 Backup your Narada project's MySQL database.
257  
258 If "config/mysql/db" absent or empty do nothing.
259  
260 Dump database as quickly as possible to files "var/mysql/*.sql":
261  
262 =over
263  
264 =item *
265  
266 Tables listed in "config/mysql/dump/ignore" doesn't included in dump
267 (even in database scheme).
268  
269 =item *
270  
271 Database scheme saved in "var/mysql/db.scheme.sql".
272  
273 =item *
274  
275 Content for tables listed in "config/mysql/dump/empty" doesn't dumped.
276  
277 =item *
278  
279 Content for tables listed in "config/mysql/dump/incremental" saved in
280 "var/mysql/TABLE_NAME.FROM-TO.sql".
281  
282 =over
283  
284 =item *
285  
286 Only new rows will be saved, which is absent in already existing files.
287  
288 =back
289  
290 =item *
291  
292 Content for other tables saved in "var/mysql/TABLE_NAME.sql".
293  
294 =over
295  
296 =item *
297  
298 If table wasn't changed since previous dump - do nothing and just keep
299 file with previous dump.
300  
301 =back
302  
303 =item *
304  
305 All other "*.sql" files will be removed from "var/mysql/".
306  
307 =back
308  
309 To force full dump (including incremental and unchanged tables) run
310 "rm var/mysql/*.sql" before "narada-mysqldump".
311  
312 Will set exclusive lock on this project while doing database analyse and
313 dumping database scheme and non-incremental tables. Incremental tables
314 will be dumped after releasing lock.
315  
316 Will set dump file's mtime to Update_time of related database table, and
317 use mtime on next dump to detect table change.
318  
319 All incremental tables MUST have first column's type "INT AUTO_INCREMENT
320 PRIMARY KEY" (can also use "MEDIUMINT", etc.).
321  
322 After ALTER incremental table or doing TRUNCATE existing files with
323 incremental dumps will be automatically removed and replaced by new ones.
324  
325 Tables listed in "config/mysql/dump/empty" and "config/mysql/dump/incremental"
326 must exists; listed in "config/mysql/dump/ignore" may not exists.
327  
328  
329 =head1 CONFIGURATION AND ENVIRONMENT
330  
331 config/mysql/db
332 config/mysql/login
333 config/mysql/pass
334 config/mysql/host
335 config/mysql/port
336 config/mysql/dump/incremental
337 config/mysql/dump/empty
338 config/mysql/dump/ignore
339 var/mysql/db.scheme.sql
340 var/mysql/*.sql
341  
342 Narada 1.x project use C<config/db/> instead of C<config/mysql/>,
343 and C<var/sql/> instead of C<var/mysql/>.
344  
345  
346 =head1 SUPPORT
347  
348 =head2 Bugs / Feature Requests
349  
350 Please report any bugs or feature requests through the issue tracker
351 at L<https://github.com/powerman/Narada/issues>.
352 You will be notified automatically of any progress on your issue.
353  
354 =head2 Source Code
355  
356 This is open source software. The code repository is available for
357 public review and contribution under the terms of the license.
358 Feel free to fork the repository and submit pull requests.
359  
360 L<https://github.com/powerman/Narada>
361  
362 git clone https://github.com/powerman/Narada.git
363  
364 =head2 Resources
365  
366 =over
367  
368 =item * MetaCPAN Search
369  
370 L<https://metacpan.org/search?q=Narada>
371  
372 =item * CPAN Ratings
373  
374 L<http://cpanratings.perl.org/dist/Narada>
375  
376 =item * AnnoCPAN: Annotated CPAN documentation
377  
378 L<http://annocpan.org/dist/Narada>
379  
380 =item * CPAN Testers Matrix
381  
382 L<http://matrix.cpantesters.org/?dist=Narada>
383  
384 =item * CPANTS: A CPAN Testing Service (Kwalitee)
385  
386 L<http://cpants.cpanauthors.org/dist/Narada>
387  
388 =back
389  
390  
391 =head1 AUTHOR
392  
393 Alex Efros E<lt>powerman@cpan.orgE<gt>
394  
395  
396 =head1 COPYRIGHT AND LICENSE
397  
398 This software is Copyright (c) 2008-2016 by Alex Efros E<lt>powerman@cpan.orgE<gt>.
399  
400 This is free software, licensed under:
401  
402 The MIT (X11) License
403  
404  
405 =cut
« blib/script/narada-setup-mysql ↑ Summary blib/script/narada-new-1 »