Mail eingeführt

This commit is contained in:
Robin Kloppe 2012-09-26 22:07:38 +02:00
parent 50062bec8a
commit eb107ead4c

View file

@ -1,38 +1,55 @@
<?php <?php
/** /**
* test.php; mininimalistic class webdav_client testing script. * backup.php; minimalistic webdav backup script.
* *
* This script shows the basic use of the methods implemented in the webdav_client class. * This script does a backup of a specified dir with
* methods implemented in the webdav_client class.
* *
* @author Christian Juerges <christian.juerges@xwave.ch>, Xwave GmbH, Josefstr. 92, 8005 Zuerich - Switzerland. * @author Mainboarder mainboarder.de
* @copyright (C) 2003/2004, Christian Juerges
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @package test *
*/ *
/* * Planned features:
$Id: test.php,v 1.4 2004/08/18 14:11:04 chris Exp $ * - encryption before transfering
$Author: chris $ * - autodelete after x days
$Date: 2004/08/18 14:11:04 $ * - just one mail with errors
$Revision: 1.4 $ * - external settings file
*
*/ * Full paths are needed whenever you use this file es cronjob. So a local user executes the script
* and a relative path will fail.
*/
if (!class_exists('webdav_client')) { if (!class_exists('webdav_client')) {
require('/full/path/to/class_webdav_client.php'); require('/full/path/to/class_webdav_client.php');
} }
$wdc = new webdav_client(); $wdc = new webdav_client();
$wdc->set_server('webdav.example.com');
$wdc->set_ssl(1); /**
$wdc->set_port(443); * Here are your specific settings needed
$wdc->set_user('user'); */
$wdc->set_pass('password'); $wdc->set_server('webdav.example.com'); // to which server should be connected
$wdc->set_user('username'); // your username to authenticate
$wdc->set_pass('password'); // your password to authenticate with the server
$pathLoc = '/path/to/local/backups/'; // which dir do you wish to backup
$pathExt = '/path/to/external/backup/collection/'; // where should this backup be transfered to
$mail = false; // is mailsystem activated (php sendmail required)
$mailTo = 'admin@gmail.com'; // where should output go to
$wdc->set_ssl(1); // is ssl required?
$wdc->set_port(443); // on which port does the webdav server listen (ssl mostly 443, unencryptet 80)
// use HTTP/1.1 // use HTTP/1.1
$wdc->set_protocol(1); $wdc->set_protocol(1); // which version should be used
// enable debugging // enable debugging
$wdc->set_debug(false); $wdc->set_debug(false); // debugging on or off?
/**
* End of settings
*/
// we try to connect
if (!$wdc->open()) { if (!$wdc->open()) {
print 'Error: could not open server connection'; print 'Error: could not open server connection';
exit; exit;
@ -43,45 +60,64 @@ if (!$wdc->check_webdav()) {
print 'Error: server does not support webdav or user/password may be wrong'; print 'Error: server does not support webdav or user/password may be wrong';
exit; exit;
} }
?>
Dateitransfer // create folder with date in scheme backup_31.01.2012-23.59
<?php
$pfadLoc = '/local/full/path/backups/';
$pfadExt = '/external/full/path/backups/';
//create folder with date
$date = date('d.m.Y-H.i',time()); $date = date('d.m.Y-H.i',time());
$pfadHeute = $pfadExt.'backup_'.$date.'/'; $pathTod = $pathExt.'backup_'.$date.'/';
$httpStatusColl = $wdc->mkcol($pathTod);
print 'Todays Folder <backup_'.$date.'> webdav status: '.$httpStatusColl.'
$httpStatus = $wdc->mkcol($pfadHeute);
print 'Todays Folder creating: '.$httpStatus.'
'; ';
//put a file to webdav collection print 'Datatransfer
if (is_dir($pfadLoc)) { ';
if ($dh = opendir($pfadLoc)) {
while (($file = readdir($dh)) !== false) {
$filename = $pfadLoc.$file;
if($file == '.' || $file == '..'){ // put a file to webdav collection
if (is_dir($pathLoc)) {
if ($dh = opendir($pathLoc)) {
$i = 0; // set counter to zero
while (($file = readdir($dh)) !== false) { // as long as there are files
$filename = $pathLoc.$file;
if($file == '.' || $file == '..'){ // do nothing with dirs like . and ..
$file=''; $file='';
} else { } else {
echo $file ."\n"; echo $file ."\n"; // where we are at the moment
$handle = fopen ($filename, 'r'); $handle = fopen ($filename, 'r');
$contents = fread ($handle, filesize ($filename)); $contents = fread ($handle, filesize ($filename));
fclose ($handle); fclose ($handle);
$target_path = $pfadHeute.$file; $target_path = $pathTod.$file;
$http_status = $wdc->put($target_path,$contents); $http_status = $wdc->put($target_path,$contents);
print 'webdav server returns ' . $http_status .' print 'webdav server returns ' . $http_status .'
'; '; // did it run as expected
if($http_status != 201) { // if not write it down
$errorCode[$i] = $http_status;
$errorFile[$i] = $file;
$i++; // set counter +1
}
} }
} }
closedir($dh); closedir($dh);
if($mail===true && !empty($errorCode)) { // create mail if activated and errorcodes are available
$i=0; // set counter to zero again
foreach($errorCode as &$code) { // now create textsnippets
$webdavErrCode .= $webdavErrCode . ' for file ' . $errorFile[$i] . '\n';
$i++;
}
mail($mailTo, 'WebDAV Backup check needed', 'Hi,
please inspect your todays backup, there was a statuscode not 201 (ok), so there might be an error.
The status which was given back was: '.$webdavErrCode.'.
Kind regards,
your Backupcron.'); // send mail now
}
} }
} }
$wdc->close(); $wdc->close(); // finish and goodbye
flush(); flush();
?>