added custom DumpPostFinishCmd command
[BackupPC.git] / bin / BackupPC_ASA_ArchiveStart
1 #!/usr/bin/perl
2 #============================================================= -*-perl-*-
3 #
4 # BackupPC_archiveStart: start an archive request from the
5 # command line.
6 #
7 # DESCRIPTION
8 #  
9 #   Usage: BackupPC_archiveStart archiveHost userName hosts...
10 #
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.
15 #
16 # AUTHOR
17 #   Craig Barratt  <cbarratt@users.sourceforge.net>
18 #   Dobrica Pavlinusic <dpavlin@rot13.org>
19 #
20 # COPYRIGHT
21 #   Copyright (C) 2007-2009  Craig Barratt
22 #
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.
27 #
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.
32 #
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
36 #
37 #========================================================================
38 #
39 # Version 3.2.0, released 31 Jul 2010.
40 #
41 # See http://backuppc.sourceforge.net.
42 #
43 #========================================================================
44
45 use strict;
46 no  utf8;
47 use lib "/usr/local/BackupPC/lib";
48 use Getopt::Std;
49 use BackupPC::Lib;
50
51 use DBI;
52
53 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
54
55 my %opts;
56
57 # no options currently
58 if ( !getopts("", \%opts) || @ARGV < 3 ) {
59     print STDERR <<EOF;
60 usage: $0 archiveHost userName hosts[:num]...
61 EOF
62     exit(1);
63 }
64
65 my %Conf = $bpc->Conf();
66
67 my $dbh = DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 0 });
68
69 my $sth = $dbh->prepare(qq{
70         select
71                 backups.id as backup_id,
72                 hosts.name as host,
73                 backups.num as num
74         from backups
75         join hosts on hosts.id = hostid
76         where hosts.name = ? and inc_size < 0 and size > 0 and not inc_deleted
77 });
78
79 my $Hosts       = $bpc->HostInfoRead();
80 my $ArchiveHost = $ARGV[0];
81 my $UserName    = $ARGV[1];
82 my $TopDir      = $bpc->{Conf}{TopDir};
83
84 if ( !defined($Hosts->{$ArchiveHost}) ) {
85     print(STDERR "$0: archive host $ArchiveHost doesn't exist... quitting\n");
86     exit(1);
87 }
88 $bpc->ConfigRead($ArchiveHost);
89
90 my(@HostList, @BackupList);
91 for ( my $i = 2 ; $i < @ARGV ; $i++ ) {
92     my ($host,$num) = split(/:/,$ARGV[$i],2);
93     if ( !defined($Hosts->{$host}) ) {
94         print(STDERR "$0: host $host doesn't exist... quitting\n");
95         exit(1);
96     }
97     my @backups = $bpc->BackupInfoRead($host);
98     if ( !@backups ) {
99         print(STDERR "$0: host $host doesn't have any backups... quitting\n");
100         exit(1);
101     }
102
103     $sth->execute( $host );
104     if ( $sth->rows == 0 ) {
105         warn "no backups to archive on $host\n";
106         next;
107     }
108
109     while ( my $row = $sth->fetchrow_hashref ) {
110         warn "+ ", $row->{host}, " ", $row->{num}, "\n";
111         push(@HostList, $host);
112         push(@BackupList, $row->{num});
113     }
114 }
115
116 my $ReqFileName;
117 for ( my $i = 0 ; ; $i++ ) {
118     $ReqFileName="archiveReq.$$.$i";
119     last if ( !-f "$TopDir/pc/$ArchiveHost/$ReqFileName" );
120 }
121 my %ArchiveReq = (
122     archiveloc  => $bpc->{Conf}{ArchiveDest},
123     archtype    => 0,
124     compression => $bpc->{Conf}{ArchiveComp} eq 'none' ? $bpc->{Conf}{CatPath}
125                     : ($bpc->{Conf}{ArchiveComp} eq 'gzip'
126                       ? $bpc->{Conf}{GzipPath} : $bpc->{Conf}{Bzip2Path}),
127     compext     => $bpc->{Conf}{ArchiveComp} eq 'none' ? ''
128                     : ($bpc->{Conf}{ArchiveComp} eq 'gzip' ? '.gz' : '.bz2'),
129     parfile     => $bpc->{Conf}{ArchivePar},
130     splitsize   => '0000000',
131     host        => $ArchiveHost,
132     HostList    => \@HostList,
133     BackupList  => \@BackupList,
134     user        => $UserName,
135     reqTime     => time,
136 );
137 my $archive = Data::Dumper->new([\%ArchiveReq], [qw(*ArchiveReq)]);
138 $archive->Indent(1);
139 if ( !open(REQ, ">", "$TopDir/pc/$ArchiveHost/$ReqFileName") ) {
140     print(STDERR "$0: can't open/write request file $TopDir/pc/$ArchiveHost/$ReqFileName... quitting\n");
141     exit(1);
142 }
143 binmode(REQ);
144 print REQ $archive->Dump;
145 close(REQ);
146 $bpc->ServerConnect($bpc->{Conf}{ServerHost}, $bpc->{Conf}{ServerPort});
147 my $reply = $bpc->ServerMesg("archive $UserName $ArchiveHost $ReqFileName");
148 $bpc->ServerDisconnect();
149 print("Sent archive request, reply: $reply\n");
150 exit(0);