Project

General

Profile

« Previous | Next » 

Revision b3cc5117

Added by Viktor Gurov almost 5 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.

Also available in: Unified diff