BUGFIX: update inc_deleted when producing new increment
[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 = 0;
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         backups.id as backup_id,
65         hosts.name as host,
66         shares.name as share,
67         backups.num as num
68 from backups
69         join shares on backups.hostid = shares.hostid
70                 and shares.id = backups.shareid
71         join hosts on shares.hostid = hosts.id
72 where inc_size < 0 and not inc_deleted
73 order by backups.date
74
75 } );
76
77 $sth->execute();
78
79 my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ? where id = ? });
80 my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });
81
82 %BackupPC::SearchLib::Conf = %Conf;
83
84 while (my $row = $sth->fetchrow_hashref) {
85         my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
86         print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
87
88         my $t = time();
89
90         my $cmd = qq{$tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file};
91         print STDERR "## $cmd\n" if ($debug);
92
93         system($cmd) == 0 or die "failed: $?";
94
95         my $size = (stat( "$tar_dir/$tar_file" ))[7];
96
97         print " dur: ",fmt_time(time() - $t)," $size bytes";
98
99         if ($size > 45) {
100                 $sth_inc_size->execute($size, $row->{'backup_id'});
101                 $sth_inc_deleted->execute(0, $row->{'backup_id'});
102         } else {
103                 $sth_inc_deleted->execute(1, $row->{'backup_id'});
104                 unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";
105                 print " EMPTY";
106         }
107         print "\n";
108
109         $dbh->commit;
110
111 }
112
113 undef $sth;
114 $dbh->disconnect;