backuppcQueueBackup/backuppcQueueDeleteBackup

154 lines
3.8 KiB
Text
Raw Permalink Normal View History

2018-08-13 16:14:08 +02:00
#!/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);
use Data::Dumper;
my $verbose = 0;
my $opt_h = 0;
my $goodOpt = 0;
my $user = 'system';
my $dry_run = 0;
my $host;
my $num;
my $all = 0;
my @numExcluded;
$goodOpt = GetOptions(
'v+' => \$verbose, 'verbose+' => \$verbose,
'h' => \$opt_h, 'help' => \$opt_h,
'j+' => \$dry_run, 'dry-run+' => \$dry_run,
'u=s' => \$user, 'user=s' => \$user,
'H=s' => \$host, 'host=s' => \$host,
'n=s' => \$num, 'num=s' => \$num,
'a+' => \$all, 'all+' => \$all,
'x=s' => \@numExcluded, 'exclude=s' => \@numExcluded);
if (not $opt_h and not $host) {
print " ERROR : You must specify on witch host you want to delete backup";
$goodOpt=0;
}
elsif (not $opt_h and $all == 0 && not $num) {
print " ERROR : You must specify witch backup number you want to delete (or use --all parameter)\n";
$goodOpt=0;
}
elsif ($#numExcluded != -1 and $all == 0) {
print " ERROR : -x/--exclude parameter could be only use in all mode.\n";
$goodOpt=0;
}
if ($opt_h or not $goodOpt) {
print "backuppcQueueDeleteBackup\n\n";
print "Script to queue delete 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 Host\n";
print " --num,-n Specific backup number to delete\n";
print " --all,-a Delete all backups of the specified host\n";
print " --exclude,-x Do not delete backup(s) specified by number (in all mode)\n";
print " --user,-u BackupPC user that doing this action for BackupPC\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');
# undump the output... BackupPC uses Data::Dumper
my %Status;
eval $status_raw;
if (not grep {/^$host$/} keys(%Status))
{
print("Unknown host ($host)\n");
exit 1;
}
@numExcluded = () if $#numExcluded < 0;
# Delete backup
sub DeleteBackup {
my ($host, $num) = @_;
return 1 if ($Status{$host}{'type'} eq 'archive');
my %host_conf = %{$server->ConfigDataRead($host)};
print "Delete backup #$num of host $host as user $user\n" if ($verbose);
return 1 if ($dry_run);
$server->ServerMesg("delete $user ${EscURI($host)} $num -r");
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);
#}
if (not $num) {
# Backups
my @Backups = $server->BackupInfoRead($host);
foreach my $Backup (@Backups) {
next if (@numExcluded and grep {/^$Backup->{num}$/} @numExcluded);
DeleteBackup($host, $Backup->{num});
}
}
else {
DeleteBackup($host, $num);
}