Project

General

Profile

« Previous | Next » 

Revision b3cc5117

Added by Viktor Gurov over 4 years ago

Backup extra data fixes. Issue #11050

View differences:

src/etc/inc/config.lib.inc
202 202
	global $g;
203 203

  
204 204
	if (file_exists($file)) {
205
		/* restore rrddata/xmldata and clear appropriate data,
206
		 * see https://redmine.pfsense.org/issues/11050 */
207
		$data = file_get_contents("$file");
208
		$conf = parse_xml_config("$file", $g['xml_rootobj']);
209
		if ($conf['rrddata']) {
210
			restore_rrddata($conf);
211
			$data = clear_tagdata("rrd", $data);
212
		}
213
		foreach ($g['backuppath'] as $bk => $path) {
214
			if (!empty($conf[$bk][$bk.'data'])) {
215
				restore_xmldatafile($bk, $conf);
216
				$data = clear_tagdata($bk, $data);
217
			}
218
		}
219
		file_put_contents($file, $data);
205 220
		unlink_if_exists("{$g['tmp_path']}/config.cache");
206 221
		copy("$file", "/cf/conf/config.xml");
207 222
		//pfSense_fsync("/cf/conf/config.xml");
......
212 227
	}
213 228
}
214 229

  
230
/*
231
 *	Backup RRD/XML Data
232
 */
233

  
234
/* If the config on disk had rrddata/xmldata tags already, remove that section first.
235
 * See https://redmine.pfsense.org/issues/8994,
236
 *     https://redmine.pfsense.org/issues/10508, 
237
 *     https://redmine.pfsense.org/issues/11050 */
238
function clear_tagdata($tag = "rrd", $data) {
239
	$data = preg_replace("/[[:blank:]]*<{$tag}data>.*<\\/{$tag}data>[[:blank:]]*\n*/s", "", $data);
240
	$data = preg_replace("/[[:blank:]]*<{$tag}data\\/>[[:blank:]]*\n*/", "", $data);
241

  
242
	return $data;
243
}
244

  
245
function restore_xmldatafile($type='voucher', $conf = false) {
246
	global $config, $g;
247

  
248
	if (!$conf) {
249
		$conf = & $config;
250
	}
251

  
252
	foreach ($conf[$type]["{$type}data"]["xmldatafile"] as $file) {
253
		$basename = basename($file['filename']);
254
		$dirname = dirname($g['backuppath'][$type]);
255
		$xmldata_file = "{$dirname}/{$basename}";
256
		if (file_put_contents($xmldata_file, gzinflate(base64_decode($file['data']))) === false) {
257
			log_error(sprintf(gettext("Cannot write %s"), $xmldata_file));
258
			continue;
259
		}
260
	}
261
}
262

  
263
function restore_rrddata($conf = false) {
264
	global $config, $g, $rrdtool, $input_errors;
265

  
266
	if (!$conf) {
267
		$conf = & $config;
268
	}
269

  
270
	foreach ($conf['rrddata']['rrddatafile'] as $rrd) {
271
		if ($rrd['xmldata']) {
272
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
273
			$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
274
			if (file_put_contents($xml_file, gzinflate(base64_decode($rrd['xmldata']))) === false) {
275
				log_error(sprintf(gettext("Cannot write %s"), $xml_file));
276
				continue;
277
			}
278
			$output = array();
279
			$status = null;
280
			exec("$rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
281
			if ($status) {
282
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
283
				continue;
284
			}
285
			unlink($xml_file);
286
		} else if ($rrd['data']) {
287
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
288
			$rrd_fd = fopen($rrd_file, "w");
289
			if (!$rrd_fd) {
290
				log_error(sprintf(gettext("Cannot write %s"), $rrd_file));
291
				continue;
292
			}
293
			$data = base64_decode($rrd['data']);
294
			/* Try to decompress the data. */
295
			$dcomp = @gzinflate($data);
296
			if ($dcomp) {
297
				/* If the decompression worked, write the decompressed data */
298
				if (fwrite($rrd_fd, $dcomp) === false) {
299
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
300
					continue;
301
				}
302
			} else {
303
				/* If the decompression failed, it wasn't compressed, so write raw data */
304
				if (fwrite($rrd_fd, $data) === false) {
305
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
306
					continue;
307
				}
308
			}
309
			if (fclose($rrd_fd) === false) {
310
				log_error(sprintf(gettext("fclose %s failed"), $rrd_file));
311
				continue;
312
			}
313
		}
314
	}
315
}
316

  
215 317
/****f* config/parse_config_bootup
216 318
 * NAME
217 319
 *   parse_config_bootup - Bootup-specific configuration checks.
src/etc/inc/web/backup.inc
65 65
	return $result;
66 66
}
67 67

  
68
function restore_rrddata() {
69
	global $config, $g, $rrdtool, $input_errors;
70
	foreach ($config['rrddata']['rrddatafile'] as $rrd) {
71
		if ($rrd['xmldata']) {
72
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
73
			$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
74
			if (file_put_contents($xml_file, gzinflate(base64_decode($rrd['xmldata']))) === false) {
75
				log_error(sprintf(gettext("Cannot write %s"), $xml_file));
76
				continue;
77
			}
78
			$output = array();
79
			$status = null;
80
			exec("$rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
81
			if ($status) {
82
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
83
				continue;
84
			}
85
			unlink($xml_file);
86
		} else if ($rrd['data']) {
87
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
88
			$rrd_fd = fopen($rrd_file, "w");
89
			if (!$rrd_fd) {
90
				log_error(sprintf(gettext("Cannot write %s"), $rrd_file));
91
				continue;
92
			}
93
			$data = base64_decode($rrd['data']);
94
			/* Try to decompress the data. */
95
			$dcomp = @gzinflate($data);
96
			if ($dcomp) {
97
				/* If the decompression worked, write the decompressed data */
98
				if (fwrite($rrd_fd, $dcomp) === false) {
99
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
100
					continue;
101
				}
102
			} else {
103
				/* If the decompression failed, it wasn't compressed, so write raw data */
104
				if (fwrite($rrd_fd, $data) === false) {
105
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
106
					continue;
107
				}
108
			}
109
			if (fclose($rrd_fd) === false) {
110
				log_error(sprintf(gettext("fclose %s failed"), $rrd_file));
111
				continue;
112
			}
113
		}
114
	}
115
}
116

  
117
function restore_xmldatafile($type='voucher') {
118
	global $config, $g;
119

  
120
	foreach ($config[$type]["{$type}data"]["xmldatafile"] as $file) {
121
		$basename = basename($file['filename']);
122
		$dirname = dirname($g['backuppath'][$type]);
123
		$xmldata_file = "{$dirname}/{$basename}";
124
		if (file_put_contents($xmldata_file, gzinflate(base64_decode($file['data']))) === false) {
125
			log_error(sprintf(gettext("Cannot write %s"), $xmldata_file));
126
			continue;
127
		}
128
	}
129
}
130

  
131 68
function backup_xmldatafile($tab=false, $type='voucher') {
132 69
	global $g;
133 70

  
......
206 143
						$name = "{$post['backuparea']}-{$name}";
207 144
					} else if (array_key_exists($post['backuparea'], $g['backuppath']) && $post['backupdata']) {
208 145
						$data = backup_config_section($post['backuparea']);
146
						$data = clear_tagdata($post['backuparea'], $data);
209 147
						$dataxml = backup_xmldatafile(false, $post['backuparea']);
210 148
						$closing_tag = "</{$post['backuparea']}>";
211 149
						$data = str_replace($closing_tag, $dataxml . $closing_tag, $data);
......
217 155
					}
218 156
				}
219 157

  
220
				//unlock($lockbckp);
221

  
222
				/*
223
				 *	Backup RRD Data
224
				 */
225

  
226
				/* If the config on disk had rrddata tags already, remove that section first.
227
				 * See https://redmine.pfsense.org/issues/8994 and
228
				 *     https://redmine.pfsense.org/issues/10508 */
229
				$data = preg_replace("/[[:blank:]]*<rrddata>.*<\\/rrddata>[[:blank:]]*\n*/s", "", $data);
230
				$data = preg_replace("/[[:blank:]]*<rrddata\\/>[[:blank:]]*\n*/", "", $data);
158
				if ($post['backuparea'] != "rrddata") {
159
					$data = clear_tagdata('rrd', $data);
160
				}
231 161

  
232 162
				if (!$post['backuparea'] && $post['backupdata']) {
233 163
					foreach ($g['backuppath'] as $bk => $path) {
234 164
						if (!empty($config[$bk])) {
165
							$data = clear_tagdata($bk, $data);
235 166
							$dataxml = backup_xmldatafile(true, $bk);
236 167
							$closing_tag = "\t</{$bk}>";
237 168
							$data = str_replace($closing_tag, $dataxml . $closing_tag, $data);
......
239 170
					}
240 171
				}
241 172

  
242
				if ($post['backuparea'] !== "rrddata" && !$post['donotbackuprrd']) {
173
				if (($post['backuparea'] != "rrddata") && !$post['donotbackuprrd']) {
243 174
					$rrd_data_xml = rrd_data_xml();
244 175
					$closing_tag = "</" . $g['xml_rootobj'] . ">";
245 176
					$data = str_replace($closing_tag, $rrd_data_xml . $closing_tag, $data);
src/usr/local/www/diag_backup.php
176 176
	'backupdata',
177 177
	'Include extra data',
178 178
	'Backup extra data.',
179
	true
179
	false
180 180
))->setHelp('Backup extra data files for some services.%1$s' .
181 181
	    '%2$s%3$sCaptive Portal - Captive Portal DB and UsedMACs DB%4$s' .
182 182
	    '%3$sCaptive Portal Vouchers - Used Vouchers DB%4$s' .

Also available in: Unified diff