backuppcQueueBackup/backuppcQueueBackup

182 lines
5.1 KiB
Perl
Executable file

#!/usr/bin/perl
#
# backuppcQueueBackup
#
# Script to queue backup in BackupPC from command line
#
# Author: Benjamin Renard <brenard@zionetrix.net>
#
# Copyright (C) 2017, Benjamin Renard
#
# Licensed under the GNU General Public License version 3 or
# any later version.
#
use strict;
no utf8;
use Getopt::Long;
Getopt::Long::Configure('bundling');
# BackupPC
use lib "/usr/share/backuppc/lib";
use BackupPC::Lib;
use BackupPC::CGI::Lib qw(:all);
my $verbose = 0;
my $opt_h = 0;
my $goodOpt = 0;
my $opt_t = 'incr';
my $user = 'system';
my $stop = 0;
my $dry_run = 0;
my $stop_backoff = 1;
my @hostsDesired;
my @hostsExcluded;
my @hostsStartFirst;
my $includeBackupsDisableHosts = 0;
my $all = 0;
$goodOpt = GetOptions(
'v+' => \$verbose, 'verbose+' => \$verbose,
't=s' => \$opt_t, 'type=s' => \$opt_t,
'stop+' => \$stop,
'includeBackupsDisableHosts+' => \$includeBackupsDisableHosts,
'backoff=i' => \$stop_backoff,
'h' => \$opt_h, 'help' => \$opt_h,
'u=s' => \$user, 'user=s' => \$user,
'j+' => \$dry_run, 'dry-run+' => \$dry_run,
'a+' => \$all, 'all+' => \$all,
'H=s' => \@hostsDesired, 'hostname=s' => \@hostsDesired,
'x=s' => \@hostsExcluded, 'exclude=s' => \@hostsExcluded,
'f=s' => \@hostsStartFirst, 'start-first=s' => \@hostsStartFirst);
if ($all == 0 && $#hostsDesired < 0 && $#hostsExcluded < 0) {
print " ERROR : You must specify on witch host(s) you want to queue backup using\n";
print " at least one of --all, --hostname or --exclude parameters.\n\n";
$goodOpt=0;
}
@hostsDesired = () if $#hostsDesired < 0;
@hostsExcluded = () if $#hostsExcluded < 0;
@hostsStartFirst = () if $#hostsStartFirst < 0;
if ($opt_t ne "full" && $opt_t ne "incr") {
print " ERROR : Unknow type\n\n";
$goodOpt=0;
}
if ($opt_h or not $goodOpt) {
print "backuppcQueueBackup\n\n";
print "Script to queue backup in BackupPC internal queue\n\n";
print "Options:\n";
print " --verbose,-v Increase verbosity\n";
print " --help,-h Display this message\n\n";
print " --hostname,-H Only queue backup for the specified host\n";
print " --exclude,-x Do not queue backup for the specified host\n";
print " --all,-a Queue backup for all BackupPC hosts\n";
print " --start-first,-f Start first queuing backup for this specified host(s)\n";
print " --includeBackupsDisableHosts Queue backup for all BackupPC hosts\n";
print " --type,-t Backup type : full or incr\n";
print " --user,-u BackupPC user that doing this action for BackupPC\n";
print " --stop Stop Backup instead of queuing new one\n";
print " --backoff During stop Backup action, say BackupPC do not automatically\n";
print " Start backup during next x hour(s)\n";
print " --dry-run,-j Perform a trial run (do nothing real)\n";
exit 0 if $goodOpt;
exit 1;
}
# Connect to BackupPC
my $server;
if (!($server = BackupPC::Lib->new))
{
print "Couldn't connect to BackupPC\n";
exit 1;
}
my %Conf = $server->Conf();
$server->ChildInit();
my $err = $server->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
if ($err)
{
print("Can't connect to server ($err)\n");
exit 1;
}
# query the BackupPC server for host status
my $status_raw = $server->ServerMesg('status hosts');
my $hosts_infos = $server->HostInfoRead();
# undump the output... BackupPC uses Data::Dumper
my %Status;
eval $status_raw;
foreach my $host (@hostsDesired, @hostsExcluded, @hostsStartFirst)
{
if (not grep {/^$host$/} keys(%Status))
{
print("Unknown host ($host)\n");
exit 1;
}
}
# Common parameters
my ($type, $doFull);
if (not $stop) {
$type = ($opt_t eq 'full') ? $Lang->{Type_full} : $Lang->{Type_incr};
$doFull = ($opt_t eq 'full') ? 1 : 0;
}
# Start/Stop backup
sub StartStopBackup {
my ($host) = @_;
return 1 if ($Status{$host}{'type'} eq 'archive');
my %host_conf = %{$server->ConfigDataRead($host)};
if (not $includeBackupsDisableHosts) {
$Status{$host}{BackupsDisable} = $host_conf{BackupsDisable};
return 1 if ( $Status{$host}{BackupsDisable} );
}
if (not $stop) {
if ($host_conf{dhcp}) {
print "Queue backup for DHCP host is not supported. Pass.\n";
return 0;
}
print "Queue $opt_t backup for host $host as user $user\n" if ($verbose);
return 1 if ($dry_run);
$server->ServerMesg("backup ${EscURI($host)} ${EscURI($host)} $user $doFull");
}
else {
print "Stop backup for host $host as user $user and disable it for next $stop_backoff hour(s)\n" if ($verbose);
return 1 if ($dry_run);
$server->ServerMesg("stop ${EscURI($host)} $user $stop_backoff");
}
return 1;
}
# First hosts
foreach my $host (@hostsStartFirst) {
StartStopBackup($host);
}
# Other hosts
foreach my $host (sort(keys(%Status))) {
next if $host =~ /^ /;
next if (@hostsStartFirst and grep {/^$host$/} @hostsStartFirst);
next if (@hostsDesired and not grep {/^$host$/} @hostsDesired);
next if (@hostsExcluded and grep {/^$host$/} @hostsExcluded);
StartStopBackup($host);
}