]>
Commit | Line | Data |
---|---|---|
9a8d51a4 DM |
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 || | |
fb9e46a0 | 148 | die "unable to access target directory '$targetdir'\n"; |
9a8d51a4 DM |
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 | 1; |