]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/PBS/Job.pm
1 package PMG
::API2
::PBS
::Job
;
6 use POSIX
qw(strftime);
8 use PVE
::JSONSchema
qw(get_standard_option);
11 use PVE
::Tools
qw(extract_param);
14 use PMG
::RESTEnvironment
;
18 use base
qw(PVE::RESTHandler);
20 __PACKAGE__-
>register_method ({
24 description
=> "List all configured Proxmox Backup Server jobs.",
25 permissions
=> { check
=> [ 'admin', 'audit' ] },
29 additionalProperties
=> 0,
31 node
=> get_standard_option
('pve-node'),
36 items
=> PMG
::PBSConfig-
>createSchema(1),
37 links
=> [ { rel
=> 'child', href
=> "{remote}" } ],
44 my $conf = PMG
::PBSConfig-
>new();
46 foreach my $remote (keys %{$conf->{ids
}}) {
47 my $d = $conf->{ids
}->{$remote};
50 server
=> $d->{server
},
51 datastore
=> $d->{datastore
},
60 __PACKAGE__-
>register_method ({
61 name
=> 'remote_index',
64 description
=> "Backup Job index.",
66 additionalProperties
=> 0,
68 node
=> get_standard_option
('pve-node'),
70 description
=> "Proxmox Backup Server ID.",
71 type
=> 'string', format
=> 'pve-configid',
79 properties
=> { section
=> { type
=> 'string'} },
81 links
=> [ { rel
=> 'child', href
=> "{section}" } ],
87 { section
=> 'snapshots' },
88 { section
=> 'backup' },
89 { section
=> 'restore' },
90 { section
=> 'timer' },
95 __PACKAGE__-
>register_method ({
96 name
=> 'get_snapshots',
97 path
=> '{remote}/snapshots',
99 description
=> "Get snapshots stored on remote.",
102 permissions
=> { check
=> [ 'admin', 'audit' ] },
104 additionalProperties
=> 0,
106 node
=> get_standard_option
('pve-node'),
108 description
=> "Proxmox Backup Server ID.",
109 type
=> 'string', format
=> 'pve-configid',
118 time => { type
=> 'string'},
119 ctime
=> { type
=> 'string'},
120 size
=> { type
=> 'integer'},
123 links
=> [ { rel
=> 'child', href
=> "{time}" } ],
128 my $remote = $param->{remote
};
129 my $node = $param->{node
};
131 my $conf = PMG
::PBSConfig-
>new();
133 my $remote_config = $conf->{ids
}->{$remote};
134 die "PBS remote '$remote' does not exist\n" if !$remote_config;
136 return [] if $remote_config->{disable
};
139 group
=> "host/$node",
142 my $pbs = PVE
::PBSClient-
>new($remote_config, $remote, $conf->{secret_dir
});
143 my $snapshots = $pbs->get_snapshots($snap_param);
145 foreach my $item (@$snapshots) {
146 my $btype = $item->{"backup-type"};
147 my $bid = $item->{"backup-id"};
148 my $epoch = $item->{"backup-time"};
149 my $size = $item->{size
} // 1;
151 my @pxar = grep { $_->{filename
} eq 'pmgbackup.pxar.didx' } @{$item->{files
}};
152 die "unexpected number of pmgbackup archives in snapshot\n" if (scalar(@pxar) != 1);
155 next if !($btype eq 'host');
156 next if !($bid eq $node);
158 my $time = strftime
("%FT%TZ", gmtime($epoch));
172 __PACKAGE__-
>register_method ({
173 name
=> 'forget_snapshot',
174 path
=> '{remote}/snapshots/{time}',
176 description
=> "Forget a snapshot",
179 permissions
=> { check
=> [ 'admin', 'audit' ] },
181 additionalProperties
=> 0,
183 node
=> get_standard_option
('pve-node'),
185 description
=> "Proxmox Backup Server ID.",
186 type
=> 'string', format
=> 'pve-configid',
189 description
=> "Backup time in RFC 3399 format",
194 returns
=> {type
=> 'null' },
198 my $remote = $param->{remote
};
199 my $node = $param->{node
};
200 my $time = $param->{time};
202 my $snapshot = "host/$node/$time";
204 my $conf = PMG
::PBSConfig-
>new();
206 my $remote_config = $conf->{ids
}->{$remote};
207 die "PBS remote '$remote' does not exist\n" if !$remote_config;
208 die "PBS remote '$remote' is disabled\n" if $remote_config->{disable
};
210 my $pbs = PVE
::PBSClient-
>new($remote_config, $remote, $conf->{secret_dir
});
213 $pbs->forget_snapshot($snapshot);
215 die "Forgetting backup failed: $@" if $@;
221 __PACKAGE__-
>register_method ({
222 name
=> 'run_backup',
223 path
=> '{remote}/backup',
225 description
=> "run backup and prune the backupgroup afterwards.",
228 permissions
=> { check
=> [ 'admin', 'audit' ] },
230 additionalProperties
=> 0,
232 node
=> get_standard_option
('pve-node'),
234 description
=> "Proxmox Backup Server ID.",
235 type
=> 'string', format
=> 'pve-configid',
239 returns
=> { type
=> "string" },
243 my $rpcenv = PMG
::RESTEnvironment-
>get();
244 my $authuser = $rpcenv->get_user();
246 my $remote = $param->{remote
};
247 my $node = $param->{node
};
249 my $conf = PMG
::PBSConfig-
>new();
251 my $remote_config = $conf->{ids
}->{$remote};
252 die "PBS remote '$remote' does not exist\n" if !$remote_config;
253 die "PBS remote '$remote' is disabled\n" if $remote_config->{disable
};
255 my $pbs = PVE
::PBSClient-
>new($remote_config, $remote, $conf->{secret_dir
});
256 my $backup_dir = "/var/lib/pmg/backup/current";
261 print "starting update of current backup state\n";
263 -d
$backup_dir || mkdir $backup_dir;
264 PMG
::Backup
::pmg_backup
($backup_dir, $param->{statistic
});
268 pxarname
=> 'pmgbackup',
272 $pbs->backup_tree($pbs_opts);
274 print "backup finished\n";
276 my $group = "host/$node";
277 print "starting prune of $group\n";
278 my $prune_opts = $conf->prune_options($remote);
279 my $res = $pbs->prune_group(undef, $prune_opts, $group);
281 foreach my $pruned (@$res){
282 my $time = strftime
("%FT%TZ", gmtime($pruned->{'backup-time'}));
283 my $snap = $pruned->{'backup-type'} . '/' . $pruned->{'backup-id'} . '/' . $time;
284 print "pruned snapshot: $snap\n";
287 print "prune finished\n";
292 return $rpcenv->fork_worker('pbs_backup', undef, $authuser, $worker);
296 __PACKAGE__-
>register_method ({
298 path
=> '{remote}/restore',
300 description
=> "Restore the system configuration.",
301 permissions
=> { check
=> [ 'admin' ] },
305 additionalProperties
=> 0,
307 PMG
::Backup
::get_restore_options
(),
309 description
=> "Proxmox Backup Server ID.",
310 type
=> 'string', format
=> 'pve-configid',
312 'backup-time' => {description
=> "backup-time to restore",
313 optional
=> 1, type
=> 'string'
315 'backup-id' => {description
=> "backup-id (hostname) of backup snapshot",
316 optional
=> 1, type
=> 'string'
320 returns
=> { type
=> "string" },
324 my $rpcenv = PMG
::RESTEnvironment-
>get();
325 my $authuser = $rpcenv->get_user();
327 my $remote = $param->{remote
};
328 my $backup_id = $param->{'backup-id'} // $param->{node
};
329 my $snapshot = "host/$backup_id";
330 $snapshot .= "/$param->{'backup-time'}" if defined($param->{'backup-time'});
332 my $conf = PMG
::PBSConfig-
>new();
334 my $remote_config = $conf->{ids
}->{$remote};
335 die "PBS remote '$remote' does not exist\n" if !$remote_config;
336 die "PBS remote '$remote' is disabled\n" if $remote_config->{disable
};
338 my $pbs = PVE
::PBSClient-
>new($remote_config, $remote, $conf->{secret_dir
});
341 my $dirname = "/tmp/proxrestore_$$.$time";
343 $param->{database
} //= 1;
345 die "nothing selected - please select what you want to restore (config or database?)\n"
346 if !($param->{database
} || $param->{config
});
349 pxarname
=> 'pmgbackup',
351 snapshot
=> $snapshot,
357 print "starting restore of $snapshot from $remote\n";
359 $pbs->restore_pxar($pbs_opts);
360 print "starting restore of PMG config\n";
361 PMG
::Backup
::pmg_restore
($dirname, $param->{database
},
362 $param->{config
}, $param->{statistic
});
363 print "restore finished\n";
368 return $rpcenv->fork_worker('pbs_restore', undef, $authuser, $worker);