]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/Backup.pm
1 package PMG
::API2
::Backup
;
10 use PVE
::JSONSchema
qw(get_standard_option);
14 use PMG
::RESTEnvironment
;
18 use base
qw(PVE::RESTHandler);
20 my $backup_dir = "/var/lib/pmg/backup";
21 my $backup_filename_pattern = 'pmg-backup_[0-9A-Za-z_-]+\.tgz';
23 my $backup_filename_property = {
24 description
=> "The backup file name.",
26 pattern
=> $backup_filename_pattern,
31 __PACKAGE__-
>register_method ({
35 description
=> "List all stored backups (files named proxmox-backup_{DATE}.tgz).",
36 permissions
=> { check
=> [ 'admin', 'audit' ] },
40 additionalProperties
=> 0,
42 node
=> get_standard_option
('pve-node'),
50 filename
=> $backup_filename_property,
52 description
=> "Size of backup file in bytes.",
56 description
=> "Backup timestamp (Unix epoch).",
61 links
=> [ { rel
=> 'child', href
=> "{filename}" } ],
68 PVE
::Tools
::dir_glob_foreach
(
70 $backup_filename_pattern,
74 my $path = "$backup_dir/$filename";
77 my $timestamp = $sa[9] // 0; # mtime
78 my $size = $sa[7] // 0; # size
80 # prefer timestamp from filename
81 if ($filename =~ m/.*_([0-9A-F]+)\.tgz/) {
86 filename
=> $filename,
88 timestamp
=> $timestamp,
95 __PACKAGE__-
>register_method ({
99 description
=> "Backup the system configuration.",
100 permissions
=> { check
=> [ 'admin' ] },
104 additionalProperties
=> 0,
106 node
=> get_standard_option
('pve-node'),
108 description
=> "Backup statistic databases.",
115 returns
=> { type
=> "string" },
119 my $rpcenv = PMG
::RESTEnvironment-
>get();
120 my $authuser = $rpcenv->get_user();
122 $param->{statistic
} //= 1;
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);
127 my $filename = "${backup_dir}/$bkfile";
132 print "starting backup to: $filename\n";
134 PMG
::Backup
::pmg_backup_pack
($filename, $param->{statistic
});
136 print "backup finished\n";
141 return $rpcenv->fork_worker('backup', undef, $authuser, $worker);
144 __PACKAGE__-
>register_method ({
146 path
=> '{filename}',
148 description
=> "Download a backup file.",
149 permissions
=> { check
=> [ 'admin' ] },
153 additionalProperties
=> 0,
155 node
=> get_standard_option
('pve-node'),
156 filename
=> $backup_filename_property,
160 returns
=> { type
=> "string" },
164 my $filename = "${backup_dir}/$param->{filename}";
169 __PACKAGE__-
>register_method ({
171 path
=> '{filename}',
173 description
=> "Delete a backup file.",
174 permissions
=> { check
=> [ 'admin' ] },
178 additionalProperties
=> 0,
180 node
=> get_standard_option
('pve-node'),
181 filename
=> $backup_filename_property,
184 returns
=> { type
=> "null" },
188 my $filename = "${backup_dir}/$param->{filename}";
189 unlink($filename) || die "delete backup file '$filename' failed - $!\n";
194 __PACKAGE__-
>register_method ({
196 path
=> '{filename}',
198 description
=> "Restore the system configuration.",
199 permissions
=> { check
=> [ 'admin' ] },
203 additionalProperties
=> 0,
205 PMG
::Backup
::get_restore_options
(),
206 node
=> get_standard_option
('pve-node'),
207 filename
=> $backup_filename_property,
210 returns
=> { type
=> "string" },
214 my $rpcenv = PMG
::RESTEnvironment-
>get();
215 my $authuser = $rpcenv->get_user();
217 my $filename = "${backup_dir}/$param->{filename}";
218 -f
$filename || die "backup file '$filename' does not exist - $!\n";
220 $param->{database
} //= 1;
222 die "nothing selected - please select what you want to restore (config or database?)\n"
223 if !($param->{database
} || $param->{config
});
228 print "starting restore: $filename\n";
230 PMG
::Backup
::pmg_restore
($filename, $param->{database
},
231 $param->{config
}, $param->{statistic
});
232 print "restore finished\n";
237 return $rpcenv->fork_worker('restore', undef, $authuser, $worker);