]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/PBSPlugin.pm
PBS: use simple TCP ping for online check for now
[pve-storage.git] / PVE / Storage / PBSPlugin.pm
index c146a0c95251074b9941bbfed26e1b5b9458f217..6403e2e810b6086c28e3a91edd8c672872b133fd 100644 (file)
@@ -4,6 +4,7 @@ package PVE::Storage::PBSPlugin;
 
 use strict;
 use warnings;
+
 use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
 use HTTP::Request;
 use IO::File;
@@ -11,9 +12,10 @@ use JSON;
 use LWP::UserAgent;
 use POSIX qw(strftime ENOENT);
 
-use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach);
-use PVE::Storage::Plugin;
 use PVE::JSONSchema qw(get_standard_option);
+use PVE::Network;
+use PVE::Storage::Plugin;
+use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach $IPV6RE);
 
 use base qw(PVE::Storage::Plugin);
 
@@ -32,15 +34,22 @@ sub plugindata {
 sub properties {
     return {
        datastore => {
-           description => "Proxmox backup server datastore name.",
+           description => "Proxmox Backup Server datastore name.",
            type => 'string',
        },
        # openssl s_client -connect <host>:8007 2>&1 |openssl x509 -fingerprint -sha256
        fingerprint => get_standard_option('fingerprint-sha256'),
        'encryption-key' => {
-           description => "Encryption key.",
+           description => "Encryption key. Use 'autogen' to generate one automatically without passphrase.",
            type => 'string',
        },
+       port => {
+           description => "For non default port.",
+           type => 'integer',
+           minimum => 1,
+           maximum => 65535,
+           default => 8007,
+       }
     };
 }
 
@@ -48,6 +57,7 @@ sub options {
     return {
        server => { fixed => 1 },
        datastore => { fixed => 1 },
+       port => { optional => 1 },
        nodes => { optional => 1},
        disable => { optional => 1},
        content => { optional => 1},
@@ -55,6 +65,7 @@ sub options {
        password => { optional => 1 },
        'encryption-key' => { optional => 1 },
        maxfiles => { optional => 1 },
+       'prune-backups' => { optional => 1 },
        fingerprint => { optional => 1 },
     };
 }
@@ -112,7 +123,11 @@ sub pbs_delete_encryption_key {
 
     my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
 
-    unlink $pwfile;
+    if (!unlink $pwfile) {
+       return if $! == ENOENT;
+       die "failed to delete encryption key! $!\n";
+    }
+    delete $scfg->{'encryption-key'};
 }
 
 sub pbs_get_encryption_key {
@@ -147,14 +162,34 @@ sub print_volid {
     return "${storeid}:${volname}";
 }
 
+my sub get_server_with_port {
+    my ($scfg) = @_;
+
+    my $server = $scfg->{server};
+    $server = "[$server]" if $server =~ /^$IPV6RE$/;
+
+    if (my $port = $scfg->{port}) {
+       $server .= ":$port" if $port != 8007;
+    }
+    return $server;
+}
+
+my $USE_CRYPT_PARAMS = {
+    backup => 1,
+    restore => 1,
+    'upload-log' => 1,
+};
+
 my sub do_raw_client_cmd {
-    my ($scfg, $storeid, $client_cmd, $param, $can_encrypt, %opts) = @_;
+    my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
+
+    my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
 
     my $client_exe = '/usr/bin/proxmox-backup-client';
     die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
        if ! -x $client_exe;
 
-    my $server = $scfg->{server};
+    my $server = get_server_with_port($scfg);
     my $datastore = $scfg->{datastore};
     my $username = $scfg->{username} // 'root@pam';
 
@@ -168,7 +203,7 @@ my sub do_raw_client_cmd {
 
     # This must live in the top scope to not get closed before the `run_command`
     my $keyfd;
-    if ($can_encrypt) {
+    if ($use_crypto) {
        if (defined($keyfd = pbs_open_encryption_key($scfg, $storeid))) {
            my $flags = fcntl($keyfd, F_GETFD, 0)
                // die "failed to get file descriptor flags: $!\n";
@@ -206,9 +241,9 @@ my sub do_raw_client_cmd {
 # - restore backups
 # - restore files
 # with a sane API
-sub run_raw_client_cmd{
+sub run_raw_client_cmd {
     my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
-    return do_raw_client_cmd($scfg, $storeid, $client_cmd, $param, 1, %opts);
+    return do_raw_client_cmd($scfg, $storeid, $client_cmd, $param, %opts);
 }
 
 sub run_client_cmd {
@@ -222,7 +257,7 @@ sub run_client_cmd {
 
     $param = [@$param, '--output-format=json'] if !$no_output;
 
-    do_raw_client_cmd($scfg, $storeid, $client_cmd, $param, 0,
+    do_raw_client_cmd($scfg, $storeid, $client_cmd, $param,
                      outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
 
     return undef if $no_output;
@@ -251,12 +286,86 @@ sub extract_vzdump_config {
        die "unable to extract configuration for backup format '$format'\n";
     }
 
-    do_raw_client_cmd($scfg, $storeid, 'restore', [ $name, $config_name, '-' ], 0,
+    do_raw_client_cmd($scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
                      outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
 
     return $config;
 }
 
+sub prune_backups {
+    my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
+
+    $logfunc //= sub { print "$_[1]\n" };
+
+    my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']);
+
+    $type = 'vm' if defined($type) && $type eq 'qemu';
+    $type = 'ct' if defined($type) && $type eq 'lxc';
+
+    my $backup_groups = {};
+    foreach my $backup (@{$backups}) {
+       (my $backup_type = $backup->{format}) =~ s/^pbs-//;
+
+       next if defined($type) && $backup_type ne $type;
+
+       my $backup_group = "$backup_type/$backup->{vmid}";
+       $backup_groups->{$backup_group} = 1;
+    }
+
+    my @param;
+    foreach my $opt (keys %{$keep}) {
+       push @param, "--$opt";
+       push @param, "$keep->{$opt}";
+    }
+
+    push @param, '--dry-run' if $dryrun;
+
+    my $prune_list = [];
+    my $failed;
+
+    foreach my $backup_group (keys %{$backup_groups}) {
+       $logfunc->('info', "running 'proxmox-backup-client prune' for '$backup_group'")
+           if !$dryrun;
+       eval {
+           my $res = run_client_cmd($scfg, $storeid, 'prune', [ $backup_group, @param ]);
+
+           foreach my $backup (@{$res}) {
+               die "result from proxmox-backup-client is not as expected\n"
+                   if !defined($backup->{'backup-time'})
+                   || !defined($backup->{'backup-type'})
+                   || !defined($backup->{'backup-id'})
+                   || !defined($backup->{'keep'});
+
+               my $ctime = $backup->{'backup-time'};
+               my $type = $backup->{'backup-type'};
+               my $vmid = $backup->{'backup-id'};
+               my $volid = print_volid($storeid, $type, $vmid, $ctime);
+
+               push @{$prune_list}, {
+                   ctime => $ctime,
+                   mark => $backup->{keep} ? 'keep' : 'remove',
+                   type => $type eq 'vm' ? 'qemu' : 'lxc',
+                   vmid => $vmid,
+                   volid => $volid,
+               };
+           }
+       };
+       if (my $err = $@) {
+           $logfunc->('err', "prune '$backup_group': $err\n");
+           $failed = 1;
+       }
+    }
+    die "error pruning backups - check log\n" if $failed;
+
+    return $prune_list;
+}
+
+my $autogen_encryption_key = sub {
+    my ($scfg, $storeid) = @_;
+    my $encfile = pbs_encryption_key_file_name($scfg, $storeid);
+    run_command(['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile]);
+};
+
 sub on_add_hook {
     my ($class, $storeid, $scfg, %param) = @_;
 
@@ -267,7 +376,12 @@ sub on_add_hook {
     }
 
     if (defined(my $encryption_key = $param{'encryption-key'})) {
-       pbs_set_encryption_key($scfg, $storeid, $encryption_key);
+       if ($encryption_key eq 'autogen') {
+           $autogen_encryption_key->($scfg, $storeid);
+       } else {
+           pbs_set_encryption_key($scfg, $storeid, $encryption_key);
+       }
+       $scfg->{'encryption-key'} = 1;
     } else {
        pbs_delete_encryption_key($scfg, $storeid);
     }
@@ -286,7 +400,12 @@ sub on_update_hook {
 
     if (exists($param{'encryption-key'})) {
        if (defined(my $encryption_key = delete($param{'encryption-key'}))) {
-           pbs_set_encryption_key($scfg, $storeid, $encryption_key);
+           if ($encryption_key eq 'autogen') {
+               $autogen_encryption_key->($scfg, $storeid);
+           } else {
+               pbs_set_encryption_key($scfg, $storeid, $encryption_key);
+           }
+           $scfg->{'encryption-key'} = 1;
        } else {
            pbs_delete_encryption_key($scfg, $storeid);
        }
@@ -329,7 +448,7 @@ sub path {
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
-    my $server = $scfg->{server};
+    my $server = get_server_with_port($scfg);
     my $datastore = $scfg->{datastore};
     my $username = $scfg->{username} // 'root@pam';
 
@@ -435,6 +554,13 @@ sub status {
 
 sub activate_storage {
     my ($class, $storeid, $scfg, $cache) = @_;
+
+    # a 'status' client command is to expensive here
+    # TODO: use a dummy ping API call to ensure the PBS API daemon is available for real
+    my $server = $scfg->{server};
+    my $port = $scfg->{port} // 8007;
+    PVE::Network::tcp_ping($server, $port, 2);
+
     return 1;
 }