c653db63bb011a5f7371452314e3266eda7d57a0
[BackupPC.git] / bin / BackupPC_incPartsUpdate
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 File::Pid;
12 use POSIX qw/strftime/;
13 use BackupPC::SearchLib;
14 use Cwd qw/abs_path/;
15
16 my $path = abs_path($0);
17 $path =~ s#/[^/]+$#/#;
18 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
19
20 die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
21
22 my $debug = 1;
23 $|=1;
24
25 my $start_t = time();
26
27 my $t_fmt = '%Y-%m-%d %H:%M:%S';
28
29 my $hosts;
30 my $bpc = BackupPC::Lib->new || die;
31 my %Conf = $bpc->Conf();
32 my $TopDir = $bpc->TopDir();
33 my $beenThere = {};
34
35 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
36 my $user = $Conf{SearchUser} || '';
37
38 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
39
40 my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
41
42 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
43
44 #---- subs ----
45
46 sub fmt_time {
47         my $t = shift || return;
48         my $out = "";
49         my ($ss,$mm,$hh) = gmtime($t);
50         $out .= "${hh}h" if ($hh);
51         $out .= sprintf("%02d:%02d", $mm,$ss);
52         return $out;
53 }
54
55 sub curr_time {
56         return strftime($t_fmt,localtime());
57 }
58
59 #----- main
60
61 my $sth = $dbh->prepare( qq{
62         
63 select
64         hosts.name as host,
65         shares.name as share,
66         num as num
67 from backups
68         join shares on backups.hostid = shares.hostid
69                 and shares.id = backups.shareid
70         join hosts on shares.hostid = hosts.id
71 where inc_size < 0 and not inc_deleted
72 order by backups.date
73
74 } );
75
76 $sth->execute();
77
78 %BackupPC::SearchLib::Conf = %Conf;
79
80 while (my $row = $sth->fetchrow_hashref) {
81         my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
82         print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
83
84         my $t = time();
85
86         my $cmd = qq{$tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file};
87         print STDERR "## $cmd\n" if ($debug);
88
89         system($cmd) == 0 or die "failed: $?";
90
91         print fmt_time(time() - $t),"\n";
92
93 }
94
95 undef $sth;
96 $dbh->disconnect;