fix lib path and syntax error in stat
[BackupPC.git] / bin / BackupPC_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 #
19 # COPYRIGHT
20 #   Copyright (C) 2007-2009  Craig Barratt
21 #
22 #   This program is free software; you can redistribute it and/or modify
23 #   it under the terms of the GNU General Public License as published by
24 #   the Free Software Foundation; either version 2 of the License, or
25 #   (at your option) any later version.
26 #
27 #   This program is distributed in the hope that it will be useful,
28 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
29 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 #   GNU General Public License for more details.
31 #
32 #   You should have received a copy of the GNU General Public License
33 #   along with this program; if not, write to the Free Software
34 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35 #
36 #========================================================================
37 #
38 # Version 3.2.0, released 31 Jul 2010.
39 #
40 # See http://backuppc.sourceforge.net.
41 #
42 #========================================================================
43
44 use strict;
45 no  utf8;
46 use lib "/usr/local/BackupPC/lib";
47 use Getopt::Std;
48 use BackupPC::Lib;
49
50 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
51
52 my %opts;
53
54 # no options currently
55 if ( !getopts("", \%opts) || @ARGV < 3 ) {
56     print STDERR <<EOF;
57 usage: $0 archiveHost userName hosts...
58 EOF
59     exit(1);
60 }
61
62 my $Hosts       = $bpc->HostInfoRead();
63 my $ArchiveHost = $ARGV[0];
64 my $UserName    = $ARGV[1];
65 my $TopDir      = $bpc->{Conf}{TopDir};
66
67 if ( !defined($Hosts->{$ArchiveHost}) ) {
68     print(STDERR "$0: archive host $ArchiveHost doesn't exist... quitting\n");
69     exit(1);
70 }
71 $bpc->ConfigRead($ArchiveHost);
72
73 my(@HostList, @BackupList);
74 for ( my $i = 2 ; $i < @ARGV ; $i++ ) {
75     my $host = $ARGV[$i];
76     if ( !defined($Hosts->{$host}) ) {
77         print(STDERR "$0: host $host doesn't exist... quitting\n");
78         exit(1);
79     }
80     my @backups = $bpc->BackupInfoRead($host);
81     if ( !@backups ) {
82         print(STDERR "$0: host $host doesn't have any backups... quitting\n");
83         exit(1);
84     }
85     push(@HostList, $host);
86     push(@BackupList, $backups[$#backups]{num});
87 }
88
89 my $ReqFileName;
90 for ( my $i = 0 ; ; $i++ ) {
91     $ReqFileName="archiveReq.$$.$i";
92     last if ( !-f "$TopDir/pc/$ArchiveHost/$ReqFileName" );
93 }
94 my %ArchiveReq = (
95     archiveloc  => $bpc->{Conf}{ArchiveDest},
96     archtype    => 0,
97     compression => $bpc->{Conf}{ArchiveComp} eq 'none' ? $bpc->{Conf}{CatPath}
98                     : ($bpc->{Conf}{ArchiveComp} eq 'gzip'
99                       ? $bpc->{Conf}{GzipPath} : $bpc->{Conf}{Bzip2Path}),
100     compext     => $bpc->{Conf}{ArchiveComp} eq 'none' ? ''
101                     : ($bpc->{Conf}{ArchiveComp} eq 'gzip' ? '.gz' : '.bz2'),
102     parfile     => $bpc->{Conf}{ArchivePar},
103     splitsize   => '0000000',
104     host        => $ArchiveHost,
105     HostList    => \@HostList,
106     BackupList  => \@BackupList,
107     user        => $UserName,
108     reqTime     => time,
109 );
110 my $archive = Data::Dumper->new([\%ArchiveReq], [qw(*ArchiveReq)]);
111 $archive->Indent(1);
112 if ( !open(REQ, ">", "$TopDir/pc/$ArchiveHost/$ReqFileName") ) {
113     print(STDERR "$0: can't open/write request file $TopDir/pc/$ArchiveHost/$ReqFileName... quitting\n");
114     exit(1);
115 }
116 binmode(REQ);
117 print REQ $archive->Dump;
118 close(REQ);
119 $bpc->ServerConnect($bpc->{Conf}{ServerHost}, $bpc->{Conf}{ServerPort});
120 my $reply = $bpc->ServerMesg("archive $UserName $ArchiveHost $ReqFileName");
121 $bpc->ServerDisconnect();
122 print("Sent archive request, reply: $reply\n");
123 exit(0);