calculate size for each backup (this is more accurate than reading meta data
authordpavlin <dpavlin@8392b6e1-25fa-0310-8288-cc32f8e212ea>
Mon, 22 Aug 2005 00:09:59 +0000 (00:09 +0000)
committerdpavlin <dpavlin@8392b6e1-25fa-0310-8288-cc32f8e212ea>
Mon, 22 Aug 2005 00:09:59 +0000 (00:09 +0000)
if you aren't staring from clean BackupPC installation).

git-svn-id: svn+ssh://llin/home/dpavlin/private/svn/BackupPC/trunk@66 8392b6e1-25fa-0310-8288-cc32f8e212ea

bin/BackupPC_updatedb
lib/BackupPC/SearchLib.pm

index 9f45641..19f7760 100755 (executable)
@@ -96,6 +96,7 @@ if ($opt{c}) {
                        date    integer         NOT NULL, 
                        type    CHAR(4)         not null,
                        shareID integer         not null references shares(id),
+                       size    integer         not null,
                        PRIMARY KEY(hostID, num, shareID) 
                );            
        });
@@ -188,8 +189,8 @@ WHERE hostID=? AND num=? AND shareid=?
 });
 
 $sth->{insert_backups} = $dbh->prepare(qq{
-INSERT INTO backups (hostID, num, date, type, shareid)
-VALUES (?,?,?,?,?)
+INSERT INTO backups (hostID, num, date, type, shareid, size)
+VALUES (?,?,?,?,?,?)
 });
 
 $sth->{insert_files} = $dbh->prepare(qq{
@@ -262,22 +263,24 @@ foreach my $host_key (keys %{$hosts}) {
                        # dump some log
                        print strftime($t_fmt,localtime())," ", $share;
 
-                       my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
+                       my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
 
                        $sth->{insert_backups}->execute(
                                $hostID,
                                $backupNum,
                                $backup->{'endTime'},
                                $backup->{'type'},
-                               $shareID
+                               $shareID,
+                               $size,
                        );
 
                        print " commit";
                        $dbh->commit();
 
                        my $dur = (time() - $t) || 1;
-                       printf(" %d/%d files %d/%d dirs [%.2f/s dur: %s]\n",
+                       printf(" %d/%d files %d/%d dirs %0.2f MB [%.2f/s dur: %s]\n",
                                $nf, $f, $nd, $d,
+                               ($size / 1024 / 1024),
                                ( ($f+$d) / $dur ),
                                fmt_time($dur)
                        );
@@ -359,7 +362,7 @@ sub recurseDir($$$$$$$$) {
 
        print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);
 
-       my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);
+       my ($nr_files, $new_files, $nr_dirs, $new_dirs, $size) = (0,0,0,0,0);
 
        { # scope
                my @stack;
@@ -369,6 +372,7 @@ sub recurseDir($$$$$$$$) {
 
                # first, add all the entries in current directory
                foreach my $path_key (keys %{$filesInBackup}) {
+                       print STDERR "# file ",Dumper($filesInBackup->{$path_key}),"\n" if ($debug >= 3);
                        my @data = (
                                $shareID,
                                $backupNum,
@@ -398,6 +402,7 @@ sub recurseDir($$$$$$$$) {
                                        $new_files++;
                                        print STDERR " file\n" if ($debug >= 2);
                                }
+                               $size += $filesInBackup->{$path_key}->{'size'} || 0;
                        }
 
                        if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
@@ -422,15 +427,16 @@ sub recurseDir($$$$$$$$) {
                print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);
 
                while ( my $dir = shift @stack ) {
-                       my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID);
+                       my ($f,$nf,$d,$nd, $s) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID);
                        print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);
                        $nr_files += $f;
                        $new_files += $nf;
                        $nr_dirs += $d;
                        $new_dirs += $nd;
+                       $size += $s;
                }
        }
 
-       return ($nr_files, $new_files, $nr_dirs, $new_dirs);
+       return ($nr_files, $new_files, $nr_dirs, $new_dirs, $size);
 }
 
index b9481f9..44dbc03 100644 (file)
@@ -105,7 +105,7 @@ sub getFiles($$) {
                FROM files 
                        INNER JOIN shares       ON files.shareID=shares.ID
                        INNER JOIN hosts        ON hosts.ID = shares.hostID
-                       INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID
+                       INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
        };
 
        my $sql_dvd_from = qq{
@@ -169,36 +169,28 @@ sub getBackupsNotBurned() {
        my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
        my $sql = q{ 
        SELECT
-               hosts.ID                AS hostid,
+               backups.hostID          AS hostid,
                min(hosts.name)         AS host,
                backups.num             AS backupno,
                min(backups.type)       AS type,
-               min(backups.date)       AS date
-       FROM backups, shares, files, hosts
+               min(backups.date)       AS date,
+               min(backups.size)       AS size
+       FROM backups
+               INNER JOIN hosts        ON hosts.ID = backups.hostID
        WHERE 
-               backups.num     = files.backupNum       AND
-               shares.ID       = files.shareID         AND         
-               backups.hostID  = shares.hostID         AND
-               hosts.ID        = backups.hostID        AND
                files.dvdid     IS NULL
        GROUP BY 
-               backups.hostID, backups.num, hosts.id
+               backups.hostID, backups.num
        ORDER BY min(backups.date)
        };
        my $sth = $dbh->prepare( $sql );
        my @ret;
        $sth->execute();
 
-       while ( my $row = $sth->fetchrow_hashref() ) {      
-               push(@ret, { 
-                        'host'         => $row->{'host'},
-                        'hostid'       => $row->{'hostid'},
-                        'backupno'     => $row->{'backupno'},
-                        'type'         => $row->{'type'},
-                        'date'         => $row->{'date'},
-                        'age'          => sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) ),
-                      }
-               );
+       while ( my $row = $sth->fetchrow_hashref() ) {
+               $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );
+               $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);
+               push @ret, $row;
        }
       
        return @ret;      
@@ -247,6 +239,7 @@ EOF3
                <td class="tableheader">Type</td>
                <td class="tableheader">date</td>
                <td class="tableheader">age/days</td>
+               <td class="tableheader">size/MB</td>
                </tr>
        };
 
@@ -278,6 +271,7 @@ EOF3
                        '<td class="fviewborder">' . $backup->{'type'} . '</td>' .
                        '<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .
                        '<td class="fviewborder">' . $backup->{'age'} . '</td>' .
+                       '<td class="fviewborder">' . $backup->{'size'} . '</td>' .
                        '</tr>';
        }