]>
Commit | Line | Data |
---|---|---|
0f79c55f DM |
1 | package PMG::API2::Backup; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
fb9e46a0 | 5 | use Time::Local; |
0f79c55f DM |
6 | use Data::Dumper; |
7 | ||
8 | use PVE::SafeSyslog; | |
9 | use PVE::Tools; | |
10 | use PVE::JSONSchema qw(get_standard_option); | |
11 | use PVE::RESTHandler; | |
12 | use PVE::INotify; | |
13 | ||
9a8d51a4 | 14 | use PMG::RESTEnvironment; |
0f79c55f | 15 | use PMG::Config; |
9a8d51a4 | 16 | use PMG::Backup; |
0f79c55f DM |
17 | |
18 | use base qw(PVE::RESTHandler); | |
19 | ||
4c952525 | 20 | my $backup_dir = "/var/lib/pmg/backup"; |
fb673414 | 21 | my $backup_filename_pattern = 'pmg-backup_[0-9A-Za-z_-]+\.tgz'; |
be87858b DM |
22 | |
23 | my $backup_filename_property = { | |
24 | description => "The backup file name.", | |
25 | type => "string", | |
26 | pattern => $backup_filename_pattern, | |
27 | minLength => 4, | |
28 | maxLength => 256, | |
29 | }; | |
fb9e46a0 | 30 | |
0f79c55f DM |
31 | __PACKAGE__->register_method ({ |
32 | name => 'list', | |
33 | path => '', | |
34 | method => 'GET', | |
35 | description => "List all stored backups (files named proxmox-backup_{DATE}.tgz).", | |
36 | permissions => { check => [ 'admin', 'audit' ] }, | |
37 | proxyto => 'node', | |
38 | protected => 1, | |
39 | parameters => { | |
40 | additionalProperties => 0, | |
fb9e46a0 DM |
41 | properties => { |
42 | node => get_standard_option('pve-node'), | |
43 | }, | |
0f79c55f | 44 | }, |
426dff12 | 45 | returns => { |
0f79c55f DM |
46 | type => "array", |
47 | items => { | |
48 | type => "object", | |
49 | properties => { | |
be87858b | 50 | filename => $backup_filename_property, |
fb9e46a0 DM |
51 | size => { |
52 | description => "Size of backup file in bytes.", | |
53 | type => 'integer', | |
54 | }, | |
65e1c50d DM |
55 | timestamp => { |
56 | description => "Backup timestamp (Unix epoch).", | |
fb9e46a0 DM |
57 | type => 'integer', |
58 | }, | |
0f79c55f DM |
59 | }, |
60 | }, | |
426dff12 | 61 | links => [ { rel => 'child', href => "{filename}" } ], |
0f79c55f DM |
62 | }, |
63 | code => sub { | |
64 | my ($param) = @_; | |
65 | ||
0f79c55f DM |
66 | my $res = []; |
67 | ||
fb9e46a0 DM |
68 | PVE::Tools::dir_glob_foreach( |
69 | $backup_dir, | |
be87858b | 70 | $backup_filename_pattern, |
fb9e46a0 | 71 | sub { |
fb673414 DM |
72 | my ($filename) = @_; |
73 | ||
74 | my $path = "$backup_dir/$filename"; | |
75 | my @sa = stat($path); | |
76 | ||
77 | my $timestamp = $sa[9] // 0; # mtime | |
78 | my $size = $sa[7] // 0; # size | |
79 | ||
80 | # prefer timestamp from filename | |
81 | if ($filename =~ m/.*_([0-9A-F]+)\.tgz/) { | |
82 | $timestamp = hex($1); | |
83 | } | |
84 | ||
fb9e46a0 DM |
85 | push @$res, { |
86 | filename => $filename, | |
fb673414 DM |
87 | size => $size, |
88 | timestamp => $timestamp, | |
fb9e46a0 DM |
89 | }; |
90 | }); | |
91 | ||
0f79c55f DM |
92 | return $res; |
93 | }}); | |
94 | ||
0f79c55f DM |
95 | __PACKAGE__->register_method ({ |
96 | name => 'backup', | |
97 | path => '', | |
98 | method => 'POST', | |
99 | description => "Backup the system configuration.", | |
100 | permissions => { check => [ 'admin' ] }, | |
101 | proxyto => 'node', | |
102 | protected => 1, | |
103 | parameters => { | |
104 | additionalProperties => 0, | |
105 | properties => { | |
9a8d51a4 | 106 | node => get_standard_option('pve-node'), |
683f0231 DM |
107 | statistic => { |
108 | description => "Backup statistic databases.", | |
109 | type => 'boolean', | |
110 | optional => 1, | |
111 | default => 1, | |
112 | }, | |
0f79c55f DM |
113 | }, |
114 | }, | |
426dff12 | 115 | returns => { type => "string" }, |
0f79c55f DM |
116 | code => sub { |
117 | my ($param) = @_; | |
118 | ||
9a8d51a4 DM |
119 | my $rpcenv = PMG::RESTEnvironment->get(); |
120 | my $authuser = $rpcenv->get_user(); | |
0f79c55f | 121 | |
683f0231 DM |
122 | $param->{statistic} //= 1; |
123 | ||
f28fc420 DM |
124 | my $ctime = time(); |
125 | my (undef, undef, undef, $mday, $mon, $year) = localtime($ctime); | |
126 | my $bkfile = sprintf("pmg-backup_%04d_%02d_%02d_%08X.tgz", $year + 1900, $mon + 1, $mday, $ctime); | |
fb9e46a0 | 127 | my $filename = "${backup_dir}/$bkfile"; |
9a8d51a4 DM |
128 | |
129 | my $worker = sub { | |
130 | my $upid = shift; | |
131 | ||
65e1c50d | 132 | print "starting backup to: $filename\n"; |
9a8d51a4 | 133 | |
29e2ff48 | 134 | PMG::Backup::pmg_backup_pack($filename, $param->{statistic}); |
65e1c50d | 135 | |
9a8d51a4 DM |
136 | print "backup finished\n"; |
137 | ||
138 | return; | |
139 | }; | |
140 | ||
141 | return $rpcenv->fork_worker('backup', undef, $authuser, $worker); | |
0f79c55f DM |
142 | }}); |
143 | ||
426dff12 DM |
144 | __PACKAGE__->register_method ({ |
145 | name => 'download', | |
146 | path => '{filename}', | |
147 | method => 'GET', | |
148 | description => "Download a backup file.", | |
149 | permissions => { check => [ 'admin' ] }, | |
150 | proxyto => 'node', | |
151 | protected => 1, | |
152 | parameters => { | |
153 | additionalProperties => 0, | |
154 | properties => { | |
155 | node => get_standard_option('pve-node'), | |
156 | filename => $backup_filename_property, | |
157 | }, | |
158 | }, | |
159 | download => 1, | |
160 | returns => { type => "string" }, | |
161 | code => sub { | |
162 | my ($param) = @_; | |
163 | ||
164 | my $filename = "${backup_dir}/$param->{filename}"; | |
165 | ||
166 | return $filename; | |
167 | }}); | |
168 | ||
be87858b DM |
169 | __PACKAGE__->register_method ({ |
170 | name => 'delete', | |
171 | path => '{filename}', | |
172 | method => 'DELETE', | |
173 | description => "Delete a backup file.", | |
174 | permissions => { check => [ 'admin' ] }, | |
175 | proxyto => 'node', | |
176 | protected => 1, | |
177 | parameters => { | |
178 | additionalProperties => 0, | |
179 | properties => { | |
180 | node => get_standard_option('pve-node'), | |
181 | filename => $backup_filename_property, | |
182 | }, | |
183 | }, | |
184 | returns => { type => "null" }, | |
185 | code => sub { | |
186 | my ($param) = @_; | |
187 | ||
188 | my $filename = "${backup_dir}/$param->{filename}"; | |
189 | unlink($filename) || die "delete backup file '$filename' failed - $!\n"; | |
190 | ||
191 | return undef; | |
192 | }}); | |
0f79c55f DM |
193 | |
194 | __PACKAGE__->register_method ({ | |
195 | name => 'restore', | |
196 | path => '{filename}', | |
197 | method => 'POST', | |
198 | description => "Restore the system configuration.", | |
199 | permissions => { check => [ 'admin' ] }, | |
200 | proxyto => 'node', | |
201 | protected => 1, | |
202 | parameters => { | |
203 | additionalProperties => 0, | |
204 | properties => { | |
9a8d51a4 | 205 | node => get_standard_option('pve-node'), |
be87858b | 206 | filename => $backup_filename_property, |
0f79c55f DM |
207 | config => { |
208 | description => "Restore system configuration.", | |
209 | type => 'boolean', | |
210 | optional => 1, | |
211 | default => 0, | |
212 | }, | |
213 | database => { | |
a6d276e9 | 214 | description => "Restore the rule database. This is the default.", |
0f79c55f DM |
215 | type => 'boolean', |
216 | optional => 1, | |
217 | default => 1, | |
218 | }, | |
683f0231 DM |
219 | statistic => { |
220 | description => "Restore statistic databases. Only considered when you restore the 'database'.", | |
221 | type => 'boolean', | |
222 | optional => 1, | |
223 | default => 0, | |
224 | }, | |
0f79c55f DM |
225 | }, |
226 | }, | |
227 | returns => { type => "string" }, | |
228 | code => sub { | |
229 | my ($param) = @_; | |
230 | ||
a6d276e9 DM |
231 | my $rpcenv = PMG::RESTEnvironment->get(); |
232 | my $authuser = $rpcenv->get_user(); | |
0f79c55f | 233 | |
a6d276e9 DM |
234 | my $filename = "${backup_dir}/$param->{filename}"; |
235 | -f $filename || die "backup file '$filename' does not exist - $!\n"; | |
0f79c55f | 236 | |
a6d276e9 DM |
237 | $param->{database} //= 1; |
238 | ||
239 | die "nothing selected - please select what you want to restore (config or database?)\n" | |
240 | if !($param->{database} || $param->{config}); | |
241 | ||
242 | my $worker = sub { | |
243 | my $upid = shift; | |
244 | ||
245 | print "starting restore: $filename\n"; | |
246 | ||
247 | PMG::Backup::pmg_restore($filename, $param->{database}, | |
248 | $param->{config}, $param->{statistic}); | |
249 | print "restore finished\n"; | |
250 | ||
251 | return; | |
252 | }; | |
253 | ||
254 | return $rpcenv->fork_worker('restore', undef, $authuser, $worker); | |
0f79c55f | 255 | }}); |
9a8d51a4 DM |
256 | |
257 | 1; |