• April 23, 2024, 06:37:56 AM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

This Forum Beta is ONLY for registered owners of D-Link products in the USA for which we have created boards at this time.

Author Topic: Automatic remote backup of DSR-1000N configfile (HOW-TO)  (Read 5315 times)

Friberg

  • Level 1 Member
  • *
  • Posts: 2
Automatic remote backup of DSR-1000N configfile (HOW-TO)
« on: October 23, 2012, 02:46:30 AM »

Hello,

Just thought that I would share a way to automatically backup the current configuration of a running DSR-1000N that you have remote access to via Web-GUI.
Since I couldn't find any standard way to do this, i wrote my own PHP-script with CURL to login to the DSR and download the file.
You can find information on the DSR-1000N at: http://www.dlink.com.au/products/?pid=842.

Heres my script
--

<?php
/*
* This script is written by Robin Friberg (robin.friberg@gmail.com).
* It's free to use and modify as you want, I would like to be notified if you do any
* feature enhancements on the script (like adding another firmware support or such).
* For now the script supports configurationbackups of devices running firmware 1.04B13_WW.
*
* Database structure:
CREATE TABLE IF NOT EXISTS `enheter` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `addr` varchar(255) NOT NULL,
  `username` varchar(50) NOT NULL DEFAULT 'admin',
  `password` varchar(50) NOT NULL DEFAULT 'default',
  `lastbackup` date NOT NULL DEFAULT '0000-00-00',
  `version` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;
*
* Observe that the table isn't built for performance. Works great for me that backs up 40 units thou.
*
* This and other scripts might be found at my website: http://rm-rf.se
*
* /Robin
*/

$db_host = "localhost"; // Set database host information
$db_user = "dsrbackup"; // Set database username
$db_pass = ""; // Set database password
$db_db = "dsrbackup"; // Set database name
$config_directory = "/var/www/dsrconf/configs/"; // Where would you like to save the configurationfiles? Make sure the user whos running the script can access this folder. The script creates subdirectories and saves configurationfile to the current date.


mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_db);

$q = mysql_query("SELECT `addr`,`username`,`password` FROM `enheter`"); // WHERE `lastbackup` < CURDATE()");
while($r = mysql_fetch_assoc($q))
{
    $ip = $r['addr']; // Can be either IP-address or DNS hostname
    $url = "https://$ip/scgi-bin/platform.cgi";
    $f = file_get_contents($url);
    preg_match("/Firmware\ Version: ([0-9.A-Za-z_]*)/",$f,$matches);
    mysql_query("UPDATE `enheter` SET `version` = '".$matches[1]."' WHERE `addr` = '".$r['addr']."' LIMIT 1");
    if ( $matches[1] != "1.04B13_WW" ) // For now, only support for 1.04B13_WW, ive tried against 1.06B55 and 1.03B43. Tried to get support for 1.06B55 but it would just not seem to work.
    {
//      echo "ERROR - ".$r['addr'] . " - " .$matches[1]."\n";
        continue;
    }
    $fields_string = "";
    $useragent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"; // Faking my browser, not sure if needed.
    $post = array(  'thispage'=>"index.htm",
                    'Users.UserName'=>$r['username'],
                    'Users.Password'=>$r['password'],
                    'button.login.Users.deviceStatus'=>"Login",
                    'Login.userAgent'=>$useragent
             );
foreach($post as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

$tmpcookie = tempnam("/tmp/","dsr"); // Create a temporary cookie in /tmp/ on a linux-box.

$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_USERAGENT, $useragent);
curl_setopt($c, CURLOPT_COOKIEJAR, $tmpcookie);
curl_setopt($c, CURLOPT_COOKIEFILE, $tmpcookie);
$inlog = curl_exec($c);
curl_close($c);
if ( strpos($inlog, "<title>Error</title>") !== false || strpos($inlog, "Invalid username or password") !== false ) // Something went wrong.
{
//  echo "ERROR - ".$r['addr']."\n";
    file_put_contents("/tmp/".$r['addr']."-ERROR.log",$inlog); // Added logging
    unlink($tmpcookie);
    continue;
}
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_URL, $url."?action=backup");
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_USERAGENT, $useragent);
curl_setopt($c, CURLOPT_COOKIEJAR, $tmpcookie);
curl_setopt($c, CURLOPT_COOKIEFILE, $tmpcookie);
ob_start();
$data = curl_exec($c);
ob_end_flush();
curl_close($c);

mkdir($config_directory."/".$r['addr']."/","0777",true); // Create directory where to save the configurationfiles.
file_put_contents($config_directory."/".$r['addr']."/".date("Y-m-d").".cfg",$data);
mysql_query("UPDATE `enheter` SET `lastbackup` = NOW() WHERE `addr` = '".$r['addr']."' LIMIT 1");
// echo "OK - ".$r['addr']."\n";
}
?>
--

Save this to a file in your Linux-server and add a cronjob to run it, preferable every night.
You will have to populate the mysql-database with hostname, username and password for every unit you would like to backup.

Contact me if you have any problems or have any enhancements to the script.


Regards,
Robin

« Last Edit: January 11, 2013, 07:42:37 AM by FurryNutz »
Logged