/[BackupPC]/trunk/bin/BackupPC_incPartsUpdate
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/bin/BackupPC_incPartsUpdate

Parent Directory Parent Directory | Revision Log Revision Log


Revision 213 - (show annotations)
Sun Oct 16 12:33:02 2005 UTC (18 years, 7 months ago) by dpavlin
File size: 4260 byte(s)
 r8608@llin:  dpavlin | 2005-10-16 13:23:59 +0200
 fix for rename, show current and total number of increments

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26