1 |
#!/usr/local/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use lib "__INSTALLDIR__/lib"; |
5 |
|
6 |
use DBI; |
7 |
use BackupPC::Lib; |
8 |
use BackupPC::View; |
9 |
use Data::Dumper; |
10 |
use Time::HiRes qw/time/; |
11 |
use POSIX qw/strftime/; |
12 |
use BackupPC::SearchLib; |
13 |
use Cwd qw/abs_path/; |
14 |
|
15 |
my $path = abs_path($0); |
16 |
$path =~ s#/[^/]+$#/#; |
17 |
my $tarIncCreate = $path .= 'BackupPC_tarIncCreate'; |
18 |
|
19 |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
20 |
|
21 |
my $debug = 0; |
22 |
$|=1; |
23 |
|
24 |
my $start_t = time(); |
25 |
|
26 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
27 |
|
28 |
my $hosts; |
29 |
my $bpc = BackupPC::Lib->new || die; |
30 |
my %Conf = $bpc->Conf(); |
31 |
my $TopDir = $bpc->TopDir(); |
32 |
my $beenThere = {}; |
33 |
|
34 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
35 |
my $user = $Conf{SearchUser} || ''; |
36 |
|
37 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
38 |
|
39 |
my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir}; |
40 |
|
41 |
die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir); |
42 |
|
43 |
#---- subs ---- |
44 |
|
45 |
sub fmt_time { |
46 |
my $t = shift || return; |
47 |
my $out = ""; |
48 |
my ($ss,$mm,$hh) = gmtime($t); |
49 |
$out .= "${hh}h" if ($hh); |
50 |
$out .= sprintf("%02d:%02d", $mm,$ss); |
51 |
return $out; |
52 |
} |
53 |
|
54 |
sub curr_time { |
55 |
return strftime($t_fmt,localtime()); |
56 |
} |
57 |
|
58 |
#----- main |
59 |
|
60 |
my $sth = $dbh->prepare( qq{ |
61 |
|
62 |
select |
63 |
backups.id as backup_id, |
64 |
hosts.name as host, |
65 |
shares.name as share, |
66 |
backups.num as num, |
67 |
inc_size, |
68 |
parts |
69 |
from backups |
70 |
join shares on backups.hostid = shares.hostid |
71 |
and shares.id = backups.shareid |
72 |
join hosts on shares.hostid = hosts.id |
73 |
where not inc_deleted |
74 |
order by backups.date |
75 |
|
76 |
} ); |
77 |
|
78 |
$sth->execute(); |
79 |
|
80 |
my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? }); |
81 |
my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? }); |
82 |
|
83 |
%BackupPC::SearchLib::Conf = %Conf; |
84 |
|
85 |
while (my $row = $sth->fetchrow_hashref) { |
86 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
87 |
|
88 |
# this will return -1 if file doesn't exist |
89 |
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
90 |
|
91 |
print curr_time, " ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
92 |
|
93 |
my $t = time(); |
94 |
|
95 |
# re-create archive? |
96 |
if ($row->{'inc_size'} == -1 || $size == -1 || $row->{'inc_size'} != $size) { |
97 |
my $cmd = qq{rm -Rf $tar_dir/$tar_file && $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file}; |
98 |
print STDERR "## $cmd\n" if ($debug); |
99 |
|
100 |
system($cmd) == 0 or die "failed: $?"; |
101 |
|
102 |
$size = (stat( "$tar_dir/$tar_file" ))[7]; |
103 |
} |
104 |
|
105 |
if ($size > 45) { |
106 |
|
107 |
my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar"; |
108 |
$max_size *= 1024; # convert to bytes |
109 |
|
110 |
my $parts = int( ($size + $max_size - 1) / $max_size ); |
111 |
|
112 |
if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) { |
113 |
print " join"; |
114 |
|
115 |
my $in = my $out = "$tar_dir/$tar_file"; |
116 |
$out .= '.tmp'; |
117 |
|
118 |
# FIXME I should really order parts manually! |
119 |
system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?"; |
120 |
} |
121 |
|
122 |
if ($size > $max_size && ! -d "$tar_dir/$tar_file") { |
123 |
print " split/$parts"; |
124 |
my $in = my $out = "$tar_dir/$tar_file"; |
125 |
$out .= '.tmp'; |
126 |
rename $in, $out || die "can't rename $in: $!"; |
127 |
mkdir $in || die "can't mkdir $in: $!"; |
128 |
|
129 |
my $suffix_len = length("$parts"); |
130 |
system("split -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?"; |
131 |
unlink $out || die "can't unlink $out: $!"; |
132 |
} |
133 |
|
134 |
$sth_inc_size->execute($size, $parts, $row->{'backup_id'}); |
135 |
$sth_inc_deleted->execute(0, $row->{'backup_id'}); |
136 |
|
137 |
printf(" %1.2f MB", ($size / 1024 / 1024)); |
138 |
|
139 |
} else { |
140 |
$sth_inc_deleted->execute(1, $row->{'backup_id'}); |
141 |
unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n"; |
142 |
print " EMPTY"; |
143 |
} |
144 |
print ", dur: ",fmt_time(time() - $t), "\n"; |
145 |
|
146 |
$dbh->commit; |
147 |
|
148 |
} |
149 |
|
150 |
undef $sth; |
151 |
$dbh->disconnect; |