2 #============================================================= -*-perl-*-
4 # BackupPC_archiveStart: start an archive request from the
9 # Usage: BackupPC_archiveStart archiveHost userName hosts...
11 # Initiates an archive request on archive host archiveHost
12 # for the listed hosts. The latest backup for each host is
13 # archived. The userName is name of the requesting user,
14 # which appears in the log files.
17 # Craig Barratt <cbarratt@users.sourceforge.net>
18 # Dobrica Pavlinusic <dpavlin@rot13.org>
21 # Copyright (C) 2007-2009 Craig Barratt
23 # This program is free software; you can redistribute it and/or modify
24 # it under the terms of the GNU General Public License as published by
25 # the Free Software Foundation; either version 2 of the License, or
26 # (at your option) any later version.
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 # GNU General Public License for more details.
33 # You should have received a copy of the GNU General Public License
34 # along with this program; if not, write to the Free Software
35 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #========================================================================
39 # Version 3.2.0, released 31 Jul 2010.
41 # See http://backuppc.sourceforge.net.
43 #========================================================================
47 use lib "/usr/local/BackupPC/lib";
52 use Data::Dump qw(dump);
54 my $debug = $ENV{DEBUG} || 0;
56 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
60 # no options currently
61 if ( !getopts("", \%opts) || @ARGV < 2 ) {
63 usage: $0 archiveHost userName [hosts[:num]...]
68 my %Conf = $bpc->Conf();
70 my $dbh = DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 0 });
72 my $sth = $dbh->prepare(qq{
74 backups.id as backup_id,
81 join hosts on hosts.id = hostid
85 my $Hosts = $bpc->HostInfoRead();
86 my $ArchiveHost = shift @ARGV;
87 my $UserName = shift @ARGV;
88 my @HostFilter = @ARGV;
89 my $TopDir = $bpc->{Conf}{TopDir};
91 if ( !defined($Hosts->{$ArchiveHost}) ) {
92 print(STDERR "$0: archive host $ArchiveHost doesn't exist... quitting\n");
95 $bpc->ConfigRead($ArchiveHost);
97 my(@HostList, @BackupList);
102 foreach my $host ( keys %$Hosts ) {
103 my @backups = $bpc->BackupInfoRead($host);
105 warn "$0: host $host doesn't have any backups... skipping\n";
109 my $all_backup_numbers;
110 $all_backup_numbers->{ $_->{num} }++ foreach @backups;
112 $sth->execute( $host );
113 while ( my $row = $sth->fetchrow_hashref ) {
114 warn "# row ",dump($row) if $debug;
115 $all_backup_numbers->{ $row->{num} } =
116 $row->{inc_deleted} ? 0 :
117 $row->{size} == 0 ? 0 :
118 $row->{inc_size} > 0 ? 0 :
119 $row->{size} > 0 ? 1 :
123 warn "# $host all_backup_numbers = ",dump($all_backup_numbers),"\n";
125 $host_nums->{$host} = [
127 grep { $all_backup_numbers->{$_} }
128 keys %$all_backup_numbers
132 foreach ( @HostFilter ) {
133 my ($host,$num) = split(/:/,$_,2);
134 if ( !defined($Hosts->{$host}) ) {
135 print(STDERR "$0: host $host doesn't exist... quitting\n");
138 if ( defined $num ) {
139 warn "+ $host $num\n";
140 push(@HostList, $host);
141 push(@BackupList, $num);
143 foreach my $num ( @{ $host_nums->{$host} } ) {
144 warn "+ $host $num\n";
145 push(@HostList, $host);
146 push(@BackupList, $num);
151 if ( ! @HostFilter ) {
152 foreach my $host ( keys %$host_nums ) {
153 foreach my $num ( @{ $host_nums->{$host} } ) {
154 warn "+ $host $num\n";
155 push(@HostList, $host);
156 push(@BackupList, $num);
161 warn "# HostList=",dump(@HostList),"\n";
162 warn "# BackupList=",dump(@BackupList),"\n";
167 for ( my $i = 0 ; ; $i++ ) {
168 $ReqFileName="archiveReq.$$.$i";
169 last if ( !-f "$TopDir/pc/$ArchiveHost/$ReqFileName" );
172 archiveloc => $bpc->{Conf}{ArchiveDest},
174 compression => $bpc->{Conf}{ArchiveComp} eq 'none' ? $bpc->{Conf}{CatPath}
175 : ($bpc->{Conf}{ArchiveComp} eq 'gzip'
176 ? $bpc->{Conf}{GzipPath} : $bpc->{Conf}{Bzip2Path}),
177 compext => $bpc->{Conf}{ArchiveComp} eq 'none' ? ''
178 : ($bpc->{Conf}{ArchiveComp} eq 'gzip' ? '.gz' : '.bz2'),
179 parfile => $bpc->{Conf}{ArchivePar},
180 splitsize => $bpc->{Conf}{ArchiveSplit} . '000000', # mb -> bytes
181 host => $ArchiveHost,
182 HostList => \@HostList,
183 BackupList => \@BackupList,
187 my $archive = Data::Dumper->new([\%ArchiveReq], [qw(*ArchiveReq)]);
189 if ( !open(REQ, ">", "$TopDir/pc/$ArchiveHost/$ReqFileName") ) {
190 print(STDERR "$0: can't open/write request file $TopDir/pc/$ArchiveHost/$ReqFileName... quitting\n");
194 print REQ $archive->Dump;
196 $bpc->ServerConnect($bpc->{Conf}{ServerHost}, $bpc->{Conf}{ServerPort});
197 my $reply = $bpc->ServerMesg("archive $UserName $ArchiveHost $ReqFileName");
198 $bpc->ServerDisconnect();
199 print("Sent archive request, reply: $reply\n");