Skip to content

Commit

Permalink
wip: moved interactive restore to rvd_back
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejol committed Nov 18, 2024
1 parent 506807a commit faee41c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
25 changes: 24 additions & 1 deletion lib/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6606,6 +6606,7 @@ sub _req_method {
,compact => \&_cmd_compact
,purge => \&_cmd_purge
,backup => \&_cmd_backup
,restore_backup => \&_cmd_restore_backup

,list_storage_pools => \&_cmd_list_storage_pools
,active_storage_pool => \&_cmd_active_storage_pool
Expand Down Expand Up @@ -7292,6 +7293,18 @@ sub _cmd_backup($self, $request) {
$request->output($domain->backup());
}

sub _cmd_restore_backup($self, $request) {
my $user = Ravada::Auth::SQL->search_by_id($request->args('uid'));
die "Error: ".$user->name." not authorized to backup"
if !$user->is_admin;

my $file = $request->args('file');
die "Error: missing file '$file'" if ! -e $file;

$self->restore_backup($file,0);
$request->output();
}

=head2 set_debug_value
Sets debug global variable from setting
Expand Down Expand Up @@ -7329,9 +7342,19 @@ sub restore_backup($self, $file, $interactive=undef) {
$interactive = $ENV{TERM};
}

return Ravada::Domain::restore_backup(undef, $file, $interactive, $self);
my ($name) = $file =~ m{.*/(.*?).\d{4}-\d\d-\d\d_\d\d-\d\d-};
my $domain = $self->search_domain($name);

die "Error: ".$domain->name." is active, shut it down to restore.\n"
if $domain && $domain->is_active;

return if $domain && $interactive && !$self->_confirm_restore();

return Ravada::Domain::restore_backup($domain, $file);
}



sub _restore_backup_data($self, $file_data, $file_data_extra
,$file_data_owner) {
open my $f,"<",$file_data or die "$! $file_data";
Expand Down
20 changes: 1 addition & 19 deletions lib/Ravada/Domain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7731,17 +7731,6 @@ sub backup($self) {
return $file_backup;
}

sub _confirm_restore($self) {
if ($ENV{TERM}) {
print "Virtual Machine ".$self->name." already exists."
." All the data will be overwritten."
." Are you sure you want to restore a backup ?";
my $answer = <STDIN>;
return 0 unless $answer =~ /^y/i;
}
return 1;
}

sub _parse_file($file) {
CORE::open my $f,"<",$file or confess "$! $file";
my $json = join "",<$f>;
Expand Down Expand Up @@ -7834,20 +7823,13 @@ sub _check_parent_base_volumes($data, $file) {

}

sub restore_backup($self, $backup, $interactive, $rvd_back=undef) {
sub restore_backup($self, $backup) {
my $file = $backup;
$file = $backup->{file} if ref($backup);

die "Error: missing file '$file'" if ! -e $file;

my ($name) = $file =~ m{.*/(.*?).\d{4}-\d\d-\d\d_\d\d-\d\d-};
if (!$self) {
$self = $rvd_back->search_domain($name);
}
die "Error: ".$self->name." is active, shut it down to restore.\n"
if $self && $self->is_active;

return if $self && $interactive && !$self->_confirm_restore();

my $data = _extract_metadata($file,$name);
_check_metadata_before_restore($data);
Expand Down
1 change: 1 addition & 0 deletions lib/Ravada/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ our %VALID_ARG = (
,compact => { uid => 1, id_domain => 1 , keep_backup => 2 }
,purge => { uid => 1, id_domain => 1 }
,backup => { uid => 1, id_domain => 1, compress => 2}
,restore_backup => { uid => 1, file => 1, id_domain => 2 }

,list_machine_types => { uid => 1, id_vm => 2, vm_type => 2}
,list_cpu_models => { uid => 1, id_domain => 1}
Expand Down
46 changes: 40 additions & 6 deletions script/rvd_back
Original file line number Diff line number Diff line change
Expand Up @@ -1038,22 +1038,56 @@ sub backup($rvd_back) {
exit 0;
}

sub _confirm_restore($name) {
if ($ENV{TERM}) {
print "Virtual Machine $name already exists."
." All the data will be overwritten."
." Are you sure you want to restore a backup ?";
my $answer = <STDIN>;
return 0 unless $answer =~ /^y/i;
}
return 1;
}


sub restore($rvd_back) {
die "Error: please provide the file to restore.\n"
unless scalar(@ARGV);

my @reqs;
for my $item (@ARGV) {
if ($item =~ m{/}) {
my $dom = $rvd_back->restore_backup($item,1);
if ($dom) {
print $dom->name." restored successfuly.\n";
} else {
print "backup for $item aborted.\n";
}

my ($name) = $item =~ m{.*/(.*?).\d{4}-\d\d-\d\d_\d\d-\d\d-};
my $dom = $rvd_back->search_domain($name);
next if $dom && !_confirm_restore($name);
my $id_domain;
$id_domain = $dom->id if $dom;

push @reqs,(Ravada::Request->restore_backup(
id_domain => $id_domain
,file => $item
,uid => Ravada::Utils::user_daemon->id
));
} else {
warn "Error: I can't find '$item'. Plese pass the path with the backed up filename.\n";
}
}

for (;;) {
last if !@reqs;
my @reqs2;
for my $req ( @reqs ) {
if ( $req->status eq 'done' ) {
warn $req->error if $req->error;
print $req->args('file')." restored.\n";
print(($req->output or '')."\n");
next;
}
push @reqs2,($req);
}
@reqs = @reqs2;
}
}

sub _list_domains($rvd_back
Expand Down

0 comments on commit faee41c

Please sign in to comment.