From 1586ee02ff39a957b652d6e5ac1bde5dc2b7282a Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Mon, 25 Apr 2016 11:13:25 +0200 Subject: [PATCH] backup: reworked stategies; closes #909 --- src/etc/inc/rrd.inc | 45 +--------------------------------- src/etc/inc/services.inc | 13 ---------- src/etc/inc/upgrade_config.inc | 16 ++---------- src/etc/rc | 2 ++ src/etc/rc.backup_dhcpleases | 14 ++++++++--- src/etc/rc.backup_rrd | 32 ++++++++++++++++-------- 6 files changed, 38 insertions(+), 84 deletions(-) diff --git a/src/etc/inc/rrd.inc b/src/etc/inc/rrd.inc index 0ce26fa71..6c8e85065 100644 --- a/src/etc/inc/rrd.inc +++ b/src/etc/inc/rrd.inc @@ -26,46 +26,8 @@ POSSIBILITY OF SUCH DAMAGE. */ -function restore_rrd() +function create_new_rrd($rrdcreatecmd) { - global $config; - - $rrddbpath = '/var/db/rrd/'; - $rrdtool = '/usr/local/bin/rrdtool'; - - $rrdrestore = ""; - $rrdreturn = ""; - if (file_exists('/conf/rrd.tgz')) { - foreach (glob("{$rrddbpath}/*.xml") as $xml_file) { - @unlink($xml_file); - } - $_gb = exec('cd /;LANG=C /usr/bin/tar -xzf /conf/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"); - } - unset($rrdrestore); - foreach (glob("{$rrddbpath}/*.xml") as $xml_file) { - $rrd_file = preg_replace('/\.xml$/', ".rrd", $xml_file); - if (file_exists("{$rrd_file}")) { - @unlink($rrd_file); - } - $output = array(); - $status = null; - $_gb = exec("$rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status); - if ($status) { - log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}."); - continue; - } - unset($output); - @unlink($xml_file); - } - return true; - } - return false; -} - -function create_new_rrd($rrdcreatecmd) { $rrdcreateoutput = array(); $rrdcreatereturn = 0; $_gb = exec("$rrdcreatecmd 2>&1", $rrdcreateoutput, $rrdcreatereturn); @@ -77,7 +39,6 @@ function create_new_rrd($rrdcreatecmd) { return $rrdcreatereturn; } - function enable_rrd_graphing() { global $config; @@ -149,10 +110,6 @@ function enable_rrd_graphing() } chown($rrddbpath, "nobody"); - if (file_exists("/var/run/booting")) { - restore_rrd(); - } - /* db update script */ $rrdupdatesh = "#!/bin/sh\n"; $rrdupdatesh .= "\n"; diff --git a/src/etc/inc/services.inc b/src/etc/inc/services.inc index fda8162c6..49e33d763 100644 --- a/src/etc/inc/services.inc +++ b/src/etc/inc/services.inc @@ -443,19 +443,6 @@ function services_dhcpdv4_configure() return 0; } - if (file_exists("/var/run/booting")) { - /* restore the leases, if we have them */ - if (file_exists('/conf/dhcpleases.tgz')) { - $dhcprestore = ''; - $dhcpreturn = ''; - exec('cd /;LANG=C /usr/bin/tar -xzf /conf/dhcpleases.tgz 2>&1', $dhcprestore, $dhcpreturn); - $dhcprestore = implode(' ', $dhcprestore); - if ($dhcpreturn <> 0) { - log_error(sprintf(gettext('DHCP leases restore failed exited with %s, the error is: %s'), $dhcpreturn, $dhcprestore)); - } - } - } - /* Only consider DNS servers with IPv4 addresses for the IPv4 DHCP server. */ $dns_arrv4 = array(); if (!empty($config['system']['dnsserver'])) { diff --git a/src/etc/inc/upgrade_config.inc b/src/etc/inc/upgrade_config.inc index 1ee96126f..a08f36459 100644 --- a/src/etc/inc/upgrade_config.inc +++ b/src/etc/inc/upgrade_config.inc @@ -2123,12 +2123,6 @@ function upgrade_054_to_055() unset($rrdxmlarray); } 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 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='/conf' /usr/local/etc/rc.backup_rrd"); - if (file_exists("/var/run/booting")) { - echo "Updating configuration..."; - } } function upgrade_055_to_056() { @@ -2787,15 +2781,12 @@ function upgrade_080_to_081() @unlink("/tmp/{$xmldumpnew}"); } 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 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='/conf' /usr/local/etc/rc.backup_rrd"); - if (file_exists("/var/run/booting")) - echo "Updating configuration..."; + foreach($config['filter']['rule'] as & $rule) { if (isset($rule['protocol']) && !empty($rule['protocol'])) $rule['protocol'] = strtolower($rule['protocol']); } + unset($rule); } @@ -3093,9 +3084,6 @@ function upgrade_095_to_096() } 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 RRDDBPATH='{$rrddbpath}' CF_CONF_PATH='/conf' /usr/local/etc/rc.backup_rrd"); } function upgrade_096_to_097() diff --git a/src/etc/rc b/src/etc/rc index be7a061a1..65b05f554 100755 --- a/src/etc/rc +++ b/src/etc/rc @@ -185,7 +185,9 @@ echo "done." /usr/local/etc/rc.syshook early # Restore backups from previous shutdown (if any) +/usr/local/etc/rc.backup_dhcpleases restore /usr/local/etc/rc.backup_netflow restore +/usr/local/etc/rc.backup_rrd restore # let the PHP-based configuration subsystem set up the system now echo -n "Launching the init system..." diff --git a/src/etc/rc.backup_dhcpleases b/src/etc/rc.backup_dhcpleases index 40c3b7bc1..36ce3d6b5 100755 --- a/src/etc/rc.backup_dhcpleases +++ b/src/etc/rc.backup_dhcpleases @@ -1,6 +1,14 @@ #!/bin/sh -# Save the DHCP lease database to the config path. -if [ -d "/var/dhcpd/var/db" ]; then - cd / && tar -czf /conf/dhcpleases.tgz -C / var/dhcpd/var/db/ +BACKUPFILE="/conf/dhcpleases.tgz" +BACKUPDIR="/var/dhcpd/var/db" + +if [ "${1}" == "restore" ]; then + if [ -f "${BACKUPFILE}" ]; then + tar -C / -xzf "${BACKUPFILE}" + fi +else + if [ -d "${BACKUPDIR}" ]; then + tar -C / -czf "${BACKUPFILE}" "${BACKUPDIR}" + fi fi diff --git a/src/etc/rc.backup_rrd b/src/etc/rc.backup_rrd index 4426004ca..6dbc99dda 100755 --- a/src/etc/rc.backup_rrd +++ b/src/etc/rc.backup_rrd @@ -1,14 +1,26 @@ #!/bin/sh -: ${RRDDBPATH:=/var/db/rrd} -: ${CF_CONF_PATH:=/conf} +BACKUPDIR="/var/db/rrd" +BACKUPFILE="/conf/rrd.tgz" -# Save the rrd databases to the config path. -if [ -d "${RRDDBPATH}" ]; then - for rrdfile in "${RRDDBPATH}"/*.rrd ; do - xmlfile="${rrdfile%.rrd}.xml" - /usr/local/bin/rrdtool dump "$rrdfile" "$xmlfile" - done - cd / && tar -czf "${CF_CONF_PATH}"/rrd.tgz -C / "${RRDDBPATH#/}"/*.xml - rm "${RRDDBPATH}"/*.xml +if [ "${1}" == "restore" ]; then + if [ -f "${BACKUPFILE}" ]; then + rm -f "${BACKUPDIR}"/*.xml + tar -C / -xzf "${BACKUPFILE}" + for XML in "${BACKUPDIR}"/*.xml; do + RRD="${XML%.xml}.rrd" + rm -f "${RRD}" + /usr/local/bin/rrdtool restore -f "${XML}" "${RRD}" + done + rm -f "${BACKUPDIR}"/*.xml + fi +else + if [ -d "${BACKUPDIR}" ]; then + for RRD in "${BACKUPDIR}"/*.rrd; do + XML="${RRD%.rrd}.xml" + /usr/local/bin/rrdtool dump "${RRD}" "${XML}" + done + tar -C / -czf "${BACKUPFILE}" "${BACKUPDIR}"/*.xml + rm -f "${BACKUPDIR}"/*.xml + fi fi