diff --git a/README.md b/README.md index c434275..e55d846 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,19 @@ -Script to queue backup in BackupPC from command line -==================================================== +Script to queue backup (or backup removal) in BackupPC from command line +======================================================================== -This script permit to queue backup (for all or specified host(s)) in BackupPC from -command line. It's provide parameters permiting : +This scripts permit to queue from command line some tasks to internal BackupPC internal scheduler : -* to specify witch type of backup to queue (full or incr) ; -* to specify as wich user the backup is run for BackupPC ; -* to stop running backup instead of starting new one, and how many hour(s) BackupPC could not start automatically backup ; +* queue backup for all or specified host(s) : It's provide parameters permiting : + + * to specify witch type of backup to queue (full or incr) ; + * to specify as wich user the backup is run for BackupPC ; + * to stop running backup instead of starting new one, and how many hour(s) BackupPC could not start automatically backup ; + +* queue backup removal (for specific host) : It's provide parameters permiting : + + * to specify witch backup to delete (specified by it's number) ; + * to specify to delete all backups of the host. Another parameter permit to specify witch backup(s) to omit ; + * to specify as wich user the backup removal is run for BackupPC ; Compatibility ------------- @@ -16,6 +23,8 @@ This script have been successfully test with BackupPC 3 (3.3.0) and 4 (4.1.5). Usage ----- +### backuppcQueueBackup + ``` backuppcQueueBackup @@ -38,10 +47,29 @@ Options: --dry-run,-j Perform a trial run (do nothing real) ``` +### backuppcQueueDeleteBackup + +``` +backuppcQueueDeleteBackup + +Script to queue delete backup in BackupPC internal queue + +Options: + --verbose,-v Increase verbosity + --help,-h Display this message + + --hostname,-H Host + --num,-n Specific backup number to delete + --all,-a Delete all backups of the specified host + --exclude,-x Do not delete backup(s) specified by number (in all mode) + --user,-u BackupPC user that doing this action for BackupPC + --dry-run,-j Perform a trial run (do nothing real) +``` + Copyright --------- -Copyright (c) 2017 Benjamin Renard +Copyright (c) 2017-2018 Benjamin Renard License ------- diff --git a/backuppcQueueDeleteBackup b/backuppcQueueDeleteBackup new file mode 100755 index 0000000..716a42a --- /dev/null +++ b/backuppcQueueDeleteBackup @@ -0,0 +1,153 @@ +#!/usr/bin/perl +# +# backuppcQueueBackup +# +# Script to queue backup in BackupPC from command line +# +# Author: Benjamin Renard +# +# 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); +}