Automated backup script (Moved)



  • Due to code.google.com closing it doors, the automated backup script pfsense-backups is moving to GitHub. The new location is https://github.com/hydrian/pfsense-backups. Please feel free to contribute.



  • Hi Hydrian,

    I've created a simple one in PHP:

    
    // set PHP execution limits
    ini_set('max_execution_time', 60);
    set_time_limit(0);
    
    $startTime = time();
    
    getPfsConfig("https://Your.URL.com:443", "YourUser", "YourPassWord");
    
    $endTime = time();
    echo "
    
    Runtime: " . ($endTime - $startTime) . " Seconds";
    
    /**********************************************************************************************************/
    
    function getPfsConfig($Url, $User, $Pass) {
    	// Basic Config
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_DEFAULT);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    	curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_COOKIEFILE, "");   // activate Session Cookies!
    
    	// Login
    	curl_setopt($ch, CURLOPT_URL, $Url . "/diag_backup.php");
    	curl_setopt($ch, CURLOPT_POSTFIELDS, "usernamefld=" . $User . "&passwordfld=" . $Pass . "&login=Login");
    	curl_exec($ch);
    
    	// get the Config File XML
    	curl_setopt($ch, CURLOPT_POSTFIELDS, "donotbackuprrd=yes&Submit=download");
    	$cOut = curl_exec($ch);
    
    	// Logout
    	curl_setopt($ch, CURLOPT_POST, 0);
    	curl_setopt($ch, CURLOPT_HTTPGET, 1);
    	curl_setopt($ch, CURLOPT_URL, $Url . "/index.php?logout");
    	curl_exec($ch);
    	curl_close($ch);
    
    	// Check if valid Config File
        $checkXml = @simplexml_load_string($cOut);
    
        if ($checkXml) {
    		$rootPath = "data";
    		preg_match('@^https?://?([^:]+)@i', $Url, $matches);
    		$filePath = $rootPath . "/" . $matches[1] . "_" . date("Ymd-His") . ".xml";
    		file_put_contents($filePath, $cOut);
    		echo "Config File: " . $filePath . " saved.";
        } else {
    		echo "No valid XML file received!";
    	}
    }
    ?>
    
    

    Just copy the code to a file on a directory on any PHP capable server with cURL extension, create a subdirectory "data" with write permissions and edit URL and credetials.
    You then can run it by directly calling the file, or putting it in a cron job.

    Everyone feel free to use it.
    Any corrections and suggestions are welcome!

    Cheers,
    Harry