]> git.proxmox.com Git - pmg-api.git/blob - PMG/Backup.pm
PMG/API2/Backup.pm - implement restore
[pmg-api.git] / PMG / Backup.pm
1 package PMG::Backup;
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6 use File::Basename;
7 use File::Path;
8
9 use PVE::Tools;
10
11 use PMG::pmgcfg;
12 use PMG::AtomicFile;
13
14 sub dump_table {
15 my ($dbh, $table, $ofh, $seq, $seqcol) = @_;
16
17 my $sth = $dbh->column_info(undef, undef, $table, undef);
18
19 my $attrs = $sth->fetchall_arrayref({});
20
21 my @col_arr;
22 foreach my $ref (@$attrs) {
23 push @col_arr, $ref->{COLUMN_NAME};
24 }
25
26 $sth->finish();
27
28 my $cols = join (', ', @col_arr);
29 $cols || die "unable to fetch column definitions: ERROR";
30
31 print $ofh "COPY $table ($cols) FROM stdin;\n";
32
33 my $cmd = "COPY $table ($cols) TO STDOUT";
34 $dbh->do($cmd);
35
36 my $data = '';
37 while ($dbh->pg_getcopydata($data) >= 0) {
38 print $ofh $data;
39 }
40
41 print $ofh "\\.\n\n";
42
43 if ($seq && $seqcol) {
44 print $ofh "SELECT setval('$seq', max($seqcol)) FROM $table;\n\n";
45 }
46 }
47
48 sub dumpdb {
49 my ($ofh) = @_;
50
51 print $ofh "SET client_encoding = 'SQL_ASCII';\n";
52 print $ofh "SET check_function_bodies = false;\n\n";
53
54 my $dbh = PMG::DBTools::open_ruledb();
55
56 print $ofh "BEGIN TRANSACTION;\n\n";
57
58 eval {
59 $dbh->begin_work;
60
61 # read a consistent snapshot
62 $dbh->do("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
63
64 dump_table($dbh, 'attribut', $ofh);
65 dump_table($dbh, 'object', $ofh, 'object_id_seq', 'id');
66 dump_table($dbh, 'objectgroup', $ofh, 'objectgroup_id_seq', 'id');
67 dump_table($dbh, 'rule', $ofh, 'rule_id_seq', 'id');
68 dump_table($dbh, 'rulegroup', $ofh);
69 dump_table($dbh, 'userprefs', $ofh);
70
71 # we do not save the following tables: cgreylist, cmailstore, cmsreceivers, clusterinfo
72 };
73 my $err = $@;
74
75 $dbh->rollback(); # end read-only transaction
76
77 $dbh->disconnect();
78
79 die $err if $err;
80
81 print $ofh "COMMIT TRANSACTION;\n\n";
82 }
83
84 sub dumpstatdb {
85 my ($ofh) = @_;
86
87 print $ofh "SET client_encoding = 'SQL_ASCII';\n";
88 print $ofh "SET check_function_bodies = false;\n\n";
89
90 my $dbh = PMG::DBTools::open_ruledb();
91
92 eval {
93 $dbh->begin_work;
94
95 # read a consistent snapshot
96 $dbh->do("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
97
98 print $ofh "BEGIN TRANSACTION;\n\n";
99
100 dump_table($dbh, 'dailystat', $ofh);
101 dump_table($dbh, 'domainstat', $ofh);
102 dump_table($dbh, 'virusinfo', $ofh);
103 dump_table($dbh, 'localstat', $ofh);
104
105 # drop/create the index is a little bit faster (20%)
106
107 print $ofh "DROP INDEX cstatistic_time_index;\n\n";
108 print $ofh "ALTER TABLE cstatistic DROP CONSTRAINT cstatistic_id_key;\n\n";
109 print $ofh "ALTER TABLE cstatistic DROP CONSTRAINT cstatistic_pkey;\n\n";
110 dump_table($dbh, 'cstatistic', $ofh, 'cstatistic_id_seq', 'id');
111 print $ofh "ALTER TABLE ONLY cstatistic ADD CONSTRAINT cstatistic_pkey PRIMARY KEY (cid, rid);\n\n";
112 print $ofh "ALTER TABLE ONLY cstatistic ADD CONSTRAINT cstatistic_id_key UNIQUE (id);\n\n";
113 print $ofh "CREATE INDEX CStatistic_Time_Index ON CStatistic (Time);\n\n";
114
115 print $ofh "DROP INDEX CStatistic_ID_Index;\n\n";
116 dump_table($dbh, 'creceivers', $ofh);
117 print $ofh "CREATE INDEX CStatistic_ID_Index ON CReceivers (CStatistic_CID, CStatistic_RID);\n\n";
118
119 dump_table($dbh, 'statinfo', $ofh);
120
121 print $ofh "COMMIT TRANSACTION;\n\n";
122 };
123 my $err = $@;
124
125 $dbh->rollback(); # end read-only transaction
126
127 $dbh->disconnect();
128
129 die $err if $err;
130 }
131
132 sub pmg_backup {
133 my ($filename, $include_statistics) = @_;
134
135 my $time = time;
136 my $dirname = "/tmp/proxbackup_$$.$time";
137 my $dbfn = "Proxmox_ruledb.sql";
138 my $statfn = "Proxmox_statdb.sql";
139 my $tarfn = "config_backup.tar";
140 my $sigfn = "proxmox_backup_v1.md5";
141 my $verfn = "version.txt";
142
143 eval {
144
145 my $targetdir = dirname($filename);
146 mkdir $targetdir; # try to create target dir
147 -d $targetdir ||
148 die "unable to access target directory '$targetdir'\n";
149
150 # create a temporary directory
151 mkdir $dirname;
152
153 # dump the database first
154 my $fh = PMG::AtomicFile->open("$dirname/$dbfn", "w") ||
155 die "cant open '$dirname/$dbfn' - $! :ERROR";
156
157 dumpdb($fh);
158
159 $fh->close(1);
160
161 if ($include_statistics) {
162 # dump the statistic db
163 my $sfh = PMG::AtomicFile->open("$dirname/$statfn", "w") ||
164 die "cant open '$dirname/$statfn' - $! :ERROR";
165
166 dumpstatdb($sfh);
167
168 $sfh->close(1);
169 }
170
171 my $pkg = PMG::pmgcfg::package();
172 my $ver = PMG::pmgcfg::version();
173
174 my $vfh = PMG::AtomicFile->open ("$dirname/$verfn", "w") ||
175 die "cant open '$dirname/$verfn' - $! :ERROR";
176
177 $time = time;
178 my $now = localtime;
179 print $vfh "product: $pkg\nversion: $ver\nbackuptime:$time:$now\n";
180 $vfh->close(1);
181
182 my $sshfiles = -d '/root/.ssh' ? '/root/.ssh' : '';
183
184 my $extra_cfgs = '/etc/passwd /etc/group';
185
186 my $extra_fn = '/etc/shadow';
187 $extra_cfgs .= " $extra_fn" if -e $extra_fn;
188
189 $extra_fn = '/etc/gshadow';
190 $extra_cfgs .= " $extra_fn" if -e $extra_fn;
191
192 $extra_fn = '/etc/mail/spamassassin/custom.cf';
193 $extra_cfgs .= " $extra_fn" if -e $extra_fn;
194
195 #$extra_fn = '/etc/postfix/tls_policy';
196 #$extra_cfgs .= " $extra_fn" if -e $extra_fn;
197
198 my $extradb = $include_statistics ? $statfn : '';
199
200 # we do not store cluster configurations (cluster.cfg)
201
202 system("/bin/tar cf $dirname/$tarfn -C / " .
203 "/etc/pmg $sshfiles $extra_cfgs>/dev/null 2>&1") == 0 ||
204 die "unable to create system configuration backup: ERROR";
205
206 system("cd $dirname; md5sum $tarfn $dbfn $extradb $verfn> $sigfn") == 0 ||
207 die "unable to create backup signature: ERROR";
208
209 system("rm -f $filename; tar czf $filename -C $dirname $verfn $sigfn $dbfn $extradb $tarfn") == 0 ||
210 die "unable to create backup archive: ERROR";
211 };
212 my $err = $@;
213
214 rmtree $dirname;
215
216 if ($err) {
217 unlink $filename;
218 die $err;
219 }
220 }
221
222 sub pmg_restore {
223 my ($filename, $restore_database, $restore_config, $restore_statistics) = @_;
224
225 my $dbname = 'Proxmox_ruledb';
226
227 my $time = time;
228 my $dirname = "/tmp/proxrestore_$$.$time";
229 my $dbfn = "Proxmox_ruledb.sql";
230 my $statfn = "Proxmox_statdb.sql";
231 my $tarfn = "config_backup.tar";
232 my $sigfn = "proxmox_backup_v1.md5";
233
234 eval {
235 # create a temporary directory
236 mkdir $dirname;
237
238 system("cd $dirname; tar xzf $filename >/dev/null 2>&1") == 0 ||
239 die "unable to extract backup archive: ERROR";
240
241 system("cd $dirname; md5sum -c $sigfn") == 0 ||
242 die "proxmox backup signature check failed: ERROR";
243
244 if ($restore_config) {
245 # restore the tar file
246 mkdir "$dirname/config/";
247 system("tar xpf $dirname/$tarfn -C $dirname/config/") == 0 ||
248 die "unable to restore configuration tar archive: ERROR";
249
250 -d "$dirname/config/etc/pmg" ||
251 die "backup does not contain a valid system configuration directory (/etc/pmg)\n";
252 # unlink unneeded files
253 unlink "$dirname/config/etc/pmg/cluster.conf"; # never restore cluster config
254 rmtree "$dirname/config/etc/pmg/master";
255
256 # backup old config to /etc/pmg.oldremove current config
257 rmtree "/etc/pmg";
258 mkdir "/etc/pmg";
259 # copy files
260 system("cp -a $dirname/config/etc/pmg/* /etc/pmg/") == 0 ||
261 die "unable to restore system configuration: ERROR";
262
263 my $cfg = PMG::Config->new();
264 my $ruledb = PMG::RuleDB->new();
265 my $rulecache = PMG::RuleCache->new($ruledb);
266 $cfg->rewrite_config($rulecache, 1);
267 }
268
269 if ($restore_database) {
270 # recreate the database
271
272 # stop all services accessing the database
273 PMG::Utils::service_wait_stopped(40, $PMG::Utils::db_service_list);
274
275 print "Destroy existing rule database\n";
276 PMG::DBTools::delete_ruledb($dbname);
277
278 print "Create new database\n";
279 my $dbh = PMG::DBTools::create_ruledb($dbname);
280 my $ruledb = PMG::RuleDB->new($dbh);
281 PMG::DBTools::init_ruledb($ruledb);
282
283 system("cat $dirname/$dbfn|psql $dbname >/dev/null 2>&1") == 0 ||
284 die "unable to restore rule database: ERROR";
285
286 if ($restore_statistics) {
287 if (-f "$dirname/$statfn") {
288 system("cat $dirname/$statfn|psql $dbname >/dev/null 2>&1") == 0 ||
289 die "unable to restore statistic database: ERROR";
290 }
291 }
292
293 print STDERR "run analyze to speed up database queries\n";
294 PMG::DBTools::postgres_admin_cmd('psql', { input => 'analyze;' }, $dbname);
295
296 print "Analyzing/Upgrading existing Databases...";
297 PMG::DBTools::upgradedb($ruledb);
298 print "done\n";
299
300 # cleanup old spam/virus storage
301 PMG::MailQueue::create_spooldirs(0, 1);
302
303 my $cfg = PMG::Config->new();
304 my $rulecache = PMG::RuleCache->new($ruledb);
305 $cfg->rewrite_config($rulecache, 1);
306
307 # and restart services as soon as possible
308 foreach my $service (reverse @$PMG::Utils::db_service_list) {
309 eval { PVE::Tools::run_command(['systemctl', 'start', $service]); };
310 warn $@ if $@;
311 }
312 }
313 };
314 my $err = $@;
315
316 rmtree $dirname;
317
318 die $err if $err;
319 }
320
321 1;