Feature #2123 Backup RRD files using the xml dump and restore from RRD tools

http://redmine.pfsense.org/issues/2123
This commit is contained in:
Darren Embry 2012-04-12 19:10:42 -04:00
parent 0d0cb04730
commit 8bdb687946
4 changed files with 93 additions and 47 deletions

View File

@ -47,6 +47,33 @@ function dump_rrd_to_xml($rrddatabase, $xmldumpfile) {
return($dumpret);
}
function restore_rrd() {
global $g;
$rrddbpath = "/var/db/rrd/";
$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
$rrdrestore = "";
$rrdreturn = "";
if (file_exists("{$g['cf_conf_path']}/rrd.tgz")) {
foreach (glob("{$rrddbpath}/*.xml") as $xml_file) {
unlink($xml_file);
}
exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/rrd.tgz 2>&1", $rrdrestore, $rrdreturn);
$rrdrestore = implode(" ", $rrdrestore);
if($rrdreturn != 0) {
log_error("RRD restore failed exited with $rrdreturn, the error is: $rrdrestore\n");
}
foreach (glob("{$rrddbpath}/*.xml") as $xml_file) {
$rrd_file = preg_replace('/\.xml$/', ".rrd", $xml_file);
exec("$rrdtool restore '{$xml_file}' '{$rrd_file}'");
unlink($xml_file);
}
return true;
}
return false;
}
function create_new_rrd($rrdcreatecmd) {
$rrdcreateoutput = array();
$rrdcreatereturn = 0;
@ -226,9 +253,6 @@ function enable_rrd_graphing() {
/* read the shaper config */
read_altq_config();
$rrdrestore = "";
$rrdreturn = "";
if (isset ($config['rrd']['enable'])) {
/* create directory if needed */
@ -239,14 +263,7 @@ function enable_rrd_graphing() {
if ($g['booting']) {
if ($g['platform'] != "pfSense") {
/* restore the databases, if we have one */
if (file_exists("{$g['cf_conf_path']}/rrd.tgz")) {
exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/rrd.tgz 2>&1", $rrdrestore, $rrdreturn);
$rrdrestore = implode(" ", $rrdrestore);
if($rrdreturn <> 0) {
log_error(sprintf(gettext('RRD restore failed exited with %1$s, the error is: %2$s%3$s'), $rrdreturn, $rrdrestore, "\n"));
}
}
restore_rrd();
}
}

View File

@ -1986,11 +1986,7 @@ function upgrade_054_to_055() {
$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
if ($g['platform'] != "pfSense") {
/* restore the databases, if we have one */
if (file_exists("{$g['cf_conf_path']}/rrd.tgz")) {
exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/rrd.tgz 2>&1", $rrdrestore, $rrdreturn); $rrdrestore = implode(" ", $rrdrestore);
if($rrdreturn <> 0) {
log_error("RRD restore failed exited with $rrdreturn, the error is: $rrdrestore\n");
}
if (restore_rrd()) {
/* Make sure to move the rrd backup out of the way. We will make a new one after converting. */
exec("/bin/mv {$g['cf_conf_path']}/rrd.tgz {$g['cf_conf_path']}/backup");
}
@ -2111,7 +2107,7 @@ function upgrade_054_to_055() {
enable_rrd_graphing();
/* Let's save the RRD graphs after we run enable RRD graphing */
/* The function will restore the rrd.tgz so we will save it after */
exec("cd /;LANG=C /usr/bin/tar -czf {$g['cf_conf_path']}/rrd.tgz -C / var/db/rrd/*.rrd");
exec("cd /; LANG=C NO_REMOUNT=1 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='{$g['cf_conf_path']}' /etc/rc.backup_rrd.sh");
if ($g['booting'])
echo "Updating configuration...";
}

View File

@ -1,8 +1,17 @@
#!/bin/sh
: ${RRDDBPATH:=/var/db/rrd}
: ${CF_CONF_PATH:=/cf/conf}
# Save the rrd databases to the config path.
if [ -d "/var/db/rrd" ]; then
/etc/rc.conf_mount_rw
cd / && tar -czf /cf/conf/rrd.tgz -C / var/db/rrd/*.rrd
/etc/rc.conf_mount_ro
if [ -d "${RRDDBPATH}" ]; then
[ -z "$NO_REMOUNT" ] && /etc/rc.conf_mount_rw
for rrdfile in "${RRDDBPATH}"/*.rrd ; do
xmlfile="${rrdfile%.rrd}.xml"
/usr/bin/nice -n20 /usr/local/bin/rrdtool dump "$rrdfile" "$xmlfile"
done
cd / && tar -czf "${CF_CONF_PATH}"/rrd.tgz -C / "${RRDDBPATH#/}"/*.xml
rm "${RRDDBPATH}"/*.xml
[ -z "$NO_REMOUNT" ] && /etc/rc.conf_mount_ro
fi

View File

@ -55,6 +55,33 @@ require_once("functions.inc");
require_once("filter.inc");
require_once("shaper.inc");
$rrddbpath = "/var/db/rrd";
$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
function rrd_data_xml() {
global $rrddbpath;
global $rrdtool;
$result = "\t<rrddata>\n";
$rrd_files = glob("{$rrddbpath}/*.rrd");
$xml_files = array();
foreach ($rrd_files as $rrd_file) {
$basename = basename($rrd_file);
$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
exec("$rrdtool dump '{$rrd_file}' '{$xml_file}'");
$xml_data = file_get_contents($xml_file);
unlink($xml_file);
if ($xml_data !== false) {
$result .= "\t\t<rrddatafile>\n";
$result .= "\t\t\t<filename>{$basename}</filename>\n";
$result .= "\t\t\t<xmldata>" . base64_encode(gzdeflate($xml_data)) . "</xmldata>\n";
$result .= "\t\t</rrddatafile>\n";
}
}
$result .= "\t</rrddata>\n";
return $result;
}
function add_base_packages_menu_items() {
global $g, $config;
$base_packages = explode($g['base_packages'], ",");
@ -225,21 +252,9 @@ if ($_POST) {
* Backup RRD Data
*/
if(!$_POST['donotbackuprrd']) {
$data = str_replace("</" . $g['xml_rootobj'] . ">", "\t<rrddata>", $data);
$rrd_files_var_db_rrd = explode("\n",`cd /var/db/rrd && ls *.rrd`);
foreach($rrd_files_var_db_rrd as $rrd) {
if($rrd) {
$rrd_data = file_get_contents("{$g['vardb_path']}/rrd/{$rrd}");
if($rrd_data) {
$data .= "\t\t<rrddatafile>\n";
$data .= "\t\t\t<filename>{$rrd}</filename>\n";
$data .= "\t\t\t<data>" . base64_encode(gzdeflate($rrd_data)) . "</data>\n";
$data .= "\t\t</rrddatafile>\n";
}
}
}
$data .= "\t</rrddata>\n";
$data .= "</" . $g['xml_rootobj'] . ">\n";
$rrd_data_xml = rrd_data_xml();
$closing_tag = "</" . $g['xml_rootobj'] . ">";
$data = str_replace($closing_tag, $rrd_data_xml . $closing_tag, $data);
}
$size = strlen($data);
@ -320,18 +335,27 @@ if ($_POST) {
/* extract out rrd items, unset from $config when done */
if($config['rrddata']) {
foreach($config['rrddata']['rrddatafile'] as $rrd) {
$rrd_fd = fopen("{$g['vardb_path']}/rrd/{$rrd['filename']}", "w");
$data = base64_decode($rrd['data']);
/* Try to decompress the data. */
$dcomp = @gzinflate($data);
if ($dcomp) {
/* If the decompression worked, write the decompressed data */
fwrite($rrd_fd, $dcomp);
} else {
/* If the decompression failed, it wasn't compressed, so write raw data */
fwrite($rrd_fd, $data);
if ($rrd['xmldata']) {
$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
file_put_contents($xml_file, gzinflate(base64_decode($rrd['xmldata'])));
exec("$rrdtool restore '{$xml_file}' '{$rrd_file}'");
unlink($xml_file);
}
else if ($rrd['data']) {
$rrd_fd = fopen("{$g['vardb_path']}/rrd/{$rrd['filename']}", "w");
$data = base64_decode($rrd['data']);
/* Try to decompress the data. */
$dcomp = @gzinflate($data);
if ($dcomp) {
/* If the decompression worked, write the decompressed data */
fwrite($rrd_fd, $dcomp);
} else {
/* If the decompression failed, it wasn't compressed, so write raw data */
fwrite($rrd_fd, $data);
}
fclose($rrd_fd);
}
fclose($rrd_fd);
}
unset($config['rrddata']);
unlink_if_exists("{$g['tmp_path']}/config.cache");