]>
git.proxmox.com Git - pmg-api.git/blob - PMG/Backup.pm
14 my $sa_custom_config_fn = "/etc/mail/spamassassin/custom.cf";
17 my ($dbh, $table, $ofh, $seq, $seqcol) = @_;
19 my $sth = $dbh->column_info(undef, undef, $table, undef);
21 my $attrs = $sth->fetchall_arrayref({});
24 foreach my $ref (@$attrs) {
25 push @col_arr, $ref->{COLUMN_NAME
};
30 my $cols = join (', ', @col_arr);
31 $cols || die "unable to fetch column definitions: ERROR";
33 print $ofh "COPY $table ($cols) FROM stdin;\n";
35 my $cmd = "COPY $table ($cols) TO STDOUT";
39 while ($dbh->pg_getcopydata($data) >= 0) {
45 if ($seq && $seqcol) {
46 print $ofh "SELECT setval('$seq', max($seqcol)) FROM $table;\n\n";
53 print $ofh "SET client_encoding = 'SQL_ASCII';\n";
54 print $ofh "SET check_function_bodies = false;\n\n";
56 my $dbh = PMG
::DBTools
::open_ruledb
();
58 print $ofh "BEGIN TRANSACTION;\n\n";
63 # read a consistent snapshot
64 $dbh->do("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
66 dump_table
($dbh, 'attribut', $ofh);
67 dump_table
($dbh, 'object', $ofh, 'object_id_seq', 'id');
68 dump_table
($dbh, 'objectgroup', $ofh, 'objectgroup_id_seq', 'id');
69 dump_table
($dbh, 'rule', $ofh, 'rule_id_seq', 'id');
70 dump_table
($dbh, 'rulegroup', $ofh);
71 dump_table
($dbh, 'userprefs', $ofh);
73 # we do not save the following tables: cgreylist, cmailstore, cmsreceivers, clusterinfo
77 $dbh->rollback(); # end read-only transaction
83 print $ofh "COMMIT TRANSACTION;\n\n";
89 print $ofh "SET client_encoding = 'SQL_ASCII';\n";
90 print $ofh "SET check_function_bodies = false;\n\n";
92 my $dbh = PMG
::DBTools
::open_ruledb
();
97 # read a consistent snapshot
98 $dbh->do("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
100 print $ofh "BEGIN TRANSACTION;\n\n";
102 dump_table
($dbh, 'dailystat', $ofh);
103 dump_table
($dbh, 'domainstat', $ofh);
104 dump_table
($dbh, 'virusinfo', $ofh);
105 dump_table
($dbh, 'localstat', $ofh);
107 # drop/create the index is a little bit faster (20%)
109 print $ofh "DROP INDEX cstatistic_time_index;\n\n";
110 print $ofh "ALTER TABLE cstatistic DROP CONSTRAINT cstatistic_id_key;\n\n";
111 print $ofh "ALTER TABLE cstatistic DROP CONSTRAINT cstatistic_pkey;\n\n";
112 dump_table
($dbh, 'cstatistic', $ofh, 'cstatistic_id_seq', 'id');
113 print $ofh "ALTER TABLE ONLY cstatistic ADD CONSTRAINT cstatistic_pkey PRIMARY KEY (cid, rid);\n\n";
114 print $ofh "ALTER TABLE ONLY cstatistic ADD CONSTRAINT cstatistic_id_key UNIQUE (id);\n\n";
115 print $ofh "CREATE INDEX CStatistic_Time_Index ON CStatistic (Time);\n\n";
117 print $ofh "DROP INDEX CStatistic_ID_Index;\n\n";
118 dump_table
($dbh, 'creceivers', $ofh);
119 print $ofh "CREATE INDEX CStatistic_ID_Index ON CReceivers (CStatistic_CID, CStatistic_RID);\n\n";
121 dump_table
($dbh, 'statinfo', $ofh);
123 print $ofh "COMMIT TRANSACTION;\n\n";
127 $dbh->rollback(); # end read-only transaction
135 my ($filename, $include_statistics) = @_;
138 my $dirname = "/tmp/proxbackup_$$.$time";
139 my $dbfn = "Proxmox_ruledb.sql";
140 my $statfn = "Proxmox_statdb.sql";
141 my $tarfn = "config_backup.tar";
142 my $sigfn = "proxmox_backup_v1.md5";
143 my $verfn = "version.txt";
147 my $targetdir = dirname
($filename);
148 mkdir $targetdir; # try to create target dir
150 die "unable to access target directory '$targetdir'\n";
152 # create a temporary directory
155 # dump the database first
156 my $fh = PMG
::AtomicFile-
>open("$dirname/$dbfn", "w") ||
157 die "cant open '$dirname/$dbfn' - $! :ERROR";
163 if ($include_statistics) {
164 # dump the statistic db
165 my $sfh = PMG
::AtomicFile-
>open("$dirname/$statfn", "w") ||
166 die "cant open '$dirname/$statfn' - $! :ERROR";
173 my $pkg = PMG
::pmgcfg
::package();
174 my $ver = PMG
::pmgcfg
::version
();
176 my $vfh = PMG
::AtomicFile-
>open ("$dirname/$verfn", "w") ||
177 die "cant open '$dirname/$verfn' - $! :ERROR";
181 print $vfh "product: $pkg\nversion: $ver\nbackuptime:$time:$now\n";
186 push @$extra_cfgs, $sa_custom_config_fn;
188 #push @$extra_cfgs, '/etc/postfix/tls_policy';
190 my $extradb = $include_statistics ?
$statfn : '';
192 my $extra = join(' ', @$extra_cfgs);
194 system("/bin/tar cf $dirname/$tarfn -C / " .
195 "/etc/pmg $extra>/dev/null 2>&1") == 0 ||
196 die "unable to create system configuration backup: ERROR";
198 system("cd $dirname; md5sum $tarfn $dbfn $extradb $verfn> $sigfn") == 0 ||
199 die "unable to create backup signature: ERROR";
201 system("rm -f $filename; tar czf $filename -C $dirname $verfn $sigfn $dbfn $extradb $tarfn") == 0 ||
202 die "unable to create backup archive: ERROR";
215 my ($filename, $restore_database, $restore_config, $restore_statistics) = @_;
217 my $dbname = 'Proxmox_ruledb';
220 my $dirname = "/tmp/proxrestore_$$.$time";
221 my $dbfn = "Proxmox_ruledb.sql";
222 my $statfn = "Proxmox_statdb.sql";
223 my $tarfn = "config_backup.tar";
224 my $sigfn = "proxmox_backup_v1.md5";
227 # create a temporary directory
230 system("cd $dirname; tar xzf $filename >/dev/null 2>&1") == 0 ||
231 die "unable to extract backup archive: ERROR";
233 system("cd $dirname; md5sum -c $sigfn") == 0 ||
234 die "proxmox backup signature check failed: ERROR";
236 if ($restore_config) {
237 # restore the tar file
238 mkdir "$dirname/config/";
239 system("tar xpf $dirname/$tarfn -C $dirname/config/") == 0 ||
240 die "unable to restore configuration tar archive: ERROR";
242 -d
"$dirname/config/etc/pmg" ||
243 die "backup does not contain a valid system configuration directory (/etc/pmg)\n";
244 # unlink unneeded files
245 unlink "$dirname/config/etc/pmg/cluster.conf"; # never restore cluster config
246 rmtree
"$dirname/config/etc/pmg/master";
248 # remove current config, but keep directory for INotify
249 rmtree
("/etc/pmg", { keep_root
=> 1 });
251 system("cp -a $dirname/config/etc/pmg/* /etc/pmg/") == 0 ||
252 die "unable to restore system configuration: ERROR";
254 if (-f
"$dirname/config/${sa_custom_config_fn}") {
255 my $data = PVE
::Tools
::file_get_contents
(
256 "$dirname/config/${sa_custom_config_fn}", 1024*1024);
257 PVE
::Tools
::file_set_contents
($sa_custom_config_fn, $data);
260 my $cfg = PMG
::Config-
>new();
261 my $ruledb = PMG
::RuleDB-
>new();
262 my $rulecache = PMG
::RuleCache-
>new($ruledb);
263 $cfg->rewrite_config($rulecache, 1);
266 if ($restore_database) {
267 # recreate the database
269 # stop all services accessing the database
270 PMG
::Utils
::service_wait_stopped
(40, $PMG::Utils
::db_service_list
);
272 print "Destroy existing rule database\n";
273 PMG
::DBTools
::delete_ruledb
($dbname);
275 print "Create new database\n";
276 my $dbh = PMG
::DBTools
::create_ruledb
($dbname);
278 system("cat $dirname/$dbfn|psql $dbname >/dev/null 2>&1") == 0 ||
279 die "unable to restore rule database: ERROR";
281 if ($restore_statistics) {
282 if (-f
"$dirname/$statfn") {
283 system("cat $dirname/$statfn|psql $dbname >/dev/null 2>&1") == 0 ||
284 die "unable to restore statistic database: ERROR";
288 print STDERR
"run analyze to speed up database queries\n";
289 PMG
::DBTools
::postgres_admin_cmd
('psql', { input
=> 'analyze;' }, $dbname);
291 print "Analyzing/Upgrading existing Databases...";
292 my $ruledb = PMG
::RuleDB-
>new($dbh);
293 PMG
::DBTools
::upgradedb
($ruledb);
296 # cleanup old spam/virus storage
297 PMG
::MailQueue
::create_spooldirs
(0, 1);
299 my $cfg = PMG
::Config-
>new();
300 my $rulecache = PMG
::RuleCache-
>new($ruledb);
301 $cfg->rewrite_config($rulecache, 1);
303 # and restart services as soon as possible
304 foreach my $service (reverse @$PMG::Utils
::db_service_list
) {
305 eval { PVE
::Tools
::run_command
(['systemctl', 'start', $service]); };