]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/PBSConfig.pm
fix #3154: backup: add 'include-statistics' to pbs
[pmg-api.git] / src / PMG / PBSConfig.pm
1 package PMG::PBSConfig;
2
3 # section config implementation for PBS integration in PMG
4
5 use strict;
6 use warnings;
7
8 use PVE::Tools qw(extract_param);
9 use PVE::SectionConfig;
10 use PVE::JSONSchema qw(get_standard_option);
11 use PVE::PBSClient;
12
13 use base qw(PVE::SectionConfig);
14
15 my $inotify_file_id = 'pmg-pbs.conf';
16 my $secret_dir = '/etc/pmg/pbs';
17 my $config_filename = "${secret_dir}/pbs.conf";
18
19 my %prune_option = (
20 optional => 1,
21 type => 'integer', minimum => '0',
22 format_description => 'N',
23 );
24
25 my %prune_properties = (
26 'keep-last' => {
27 %prune_option,
28 description => 'Keep the last <N> backups.',
29 },
30 'keep-hourly' => {
31 %prune_option,
32 description => 'Keep backups for the last <N> different hours. If there is'
33 .' more than one backup for a single hour, only the latest one is kept.'
34 },
35 'keep-daily' => {
36 %prune_option,
37 description => 'Keep backups for the last <N> different days. If there is'
38 .' more than one backup for a single day, only the latest one is kept.'
39 },
40 'keep-weekly' => {
41 %prune_option,
42 description => 'Keep backups for the last <N> different weeks. If there is'
43 .'more than one backup for a single week, only the latest one is kept.'
44 },
45 'keep-monthly' => {
46 %prune_option,
47 description => 'Keep backups for the last <N> different months. If there is'
48 .' more than one backup for a single month, only the latest one is kept.'
49 },
50 'keep-yearly' => {
51 %prune_option,
52 description => 'Keep backups for the last <N> different years. If there is'
53 .' more than one backup for a single year, only the latest one is kept.'
54 },
55 );
56
57 my $defaultData = {
58 propertyList => {
59 type => { description => "Section type." },
60 remote => {
61 description => "Proxmox Backup Server ID.",
62 type => 'string', format => 'pve-configid',
63 },
64 },
65 };
66
67 sub properties {
68 return {
69 datastore => {
70 description => "Proxmox Backup Server datastore name.",
71 type => 'string',
72 },
73 server => {
74 description => "Proxmox Backup Server address.",
75 type => 'string', format => 'address',
76 maxLength => 256,
77 },
78 disable => {
79 description => "Flag to disable (deactivate) the entry.",
80 type => 'boolean',
81 optional => 1,
82 },
83 password => {
84 description => "Password or API token secret for the user on the"
85 ." Proxmox Backup Server.",
86 type => 'string',
87 optional => 1,
88 },
89 username => get_standard_option('pmg-email-address', {
90 description => "Username or API token ID on the Proxmox Backup Server"
91 }),
92 fingerprint => get_standard_option('fingerprint-sha256'),
93 'include-statistics' => {
94 description => "Include statistics in scheduled backups",
95 type => 'boolean',
96 optional => 1,
97 },
98 %prune_properties,
99 };
100 }
101
102 sub options {
103 return {
104 server => {},
105 datastore => {},
106 disable => { optional => 1 },
107 username => { optional => 1 },
108 password => { optional => 1 },
109 fingerprint => { optional => 1 },
110 'include-statistics' => { optional => 1 },
111 'keep-last' => { optional => 1 },
112 'keep-hourly' => { optional => 1 },
113 'keep-daily' => { optional => 1 },
114 'keep-weekly' => { optional => 1 },
115 'keep-monthly' => { optional => 1 },
116 'keep-yearly' => { optional => 1 },
117 };
118 }
119
120 sub type {
121 return 'pbs';
122 }
123
124 sub private {
125 return $defaultData;
126 }
127
128 sub prune_options {
129 my ($self, $remote) = @_;
130
131 my $remote_cfg = $self->{ids}->{$remote};
132
133 my $res = {};
134 my $pruning_setup;
135 foreach my $keep_opt (keys %prune_properties) {
136 if (defined($remote_cfg->{$keep_opt})) {
137 $pruning_setup = 1;
138 $res->{$keep_opt} = $remote_cfg->{$keep_opt};
139 }
140 }
141 return $pruning_setup ? $res : undef;
142 }
143
144 sub new {
145 my ($type) = @_;
146
147 my $class = ref($type) || $type;
148
149 my $cfg = PVE::INotify::read_file($inotify_file_id);
150
151 $cfg->{secret_dir} = $secret_dir;
152
153 return bless $cfg, $class;
154 }
155
156 sub write {
157 my ($self) = @_;
158
159 PVE::INotify::write_file($inotify_file_id, $self);
160 }
161
162 sub lock_config {
163 my ($code, $errmsg) = @_;
164
165 my $lockfile = "/var/lock/pmgpbsconfig.lck";
166
167 my $p = PVE::Tools::lock_file($lockfile, undef, $code);
168 if (my $err = $@) {
169 $errmsg ? die "$errmsg: $err" : die $err;
170 }
171 }
172
173 __PACKAGE__->register();
174 __PACKAGE__->init();
175
176 sub read_pmg_pbs_conf {
177 my ($filename, $fh) = @_;
178
179 local $/ = undef; # slurp mode
180
181 my $raw = defined($fh) ? <$fh> : '';
182
183 return __PACKAGE__->parse_config($filename, $raw);
184 }
185
186 sub write_pmg_pbs_conf {
187 my ($filename, $fh, $cfg) = @_;
188
189 my $raw = __PACKAGE__->write_config($filename, $cfg);
190
191 PVE::Tools::safe_print($filename, $fh, $raw);
192 }
193
194 PVE::INotify::register_file(
195 $inotify_file_id,
196 $config_filename,
197 \&read_pmg_pbs_conf,
198 \&write_pmg_pbs_conf,
199 undef,
200 always_call_parser => 1
201 );
202
203 1;