2012-09-26 20:06:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-09-26 20:07:38 +00:00
|
|
|
* backup.php; minimalistic webdav backup script.
|
2012-09-26 20:06:31 +00:00
|
|
|
*
|
2012-09-26 20:07:38 +00:00
|
|
|
* This script does a backup of a specified dir with
|
|
|
|
* methods implemented in the webdav_client class.
|
2012-09-26 20:06:31 +00:00
|
|
|
*
|
2012-09-26 20:07:38 +00:00
|
|
|
* @author Mainboarder mainboarder.de
|
2012-09-26 20:06:31 +00:00
|
|
|
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
|
2012-09-26 20:07:38 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Planned features:
|
|
|
|
* - encryption before transfering
|
|
|
|
* - autodelete after x days
|
|
|
|
* - just one mail with errors
|
|
|
|
* - 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.
|
|
|
|
*/
|
2012-09-26 20:06:31 +00:00
|
|
|
|
|
|
|
if (!class_exists('webdav_client')) {
|
|
|
|
require('/full/path/to/class_webdav_client.php');
|
2012-09-26 20:07:38 +00:00
|
|
|
}
|
2012-09-26 20:06:31 +00:00
|
|
|
$wdc = new webdav_client();
|
2012-09-26 20:07:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Here are your specific settings needed
|
|
|
|
*/
|
|
|
|
$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)
|
|
|
|
|
2012-09-26 20:06:31 +00:00
|
|
|
// use HTTP/1.1
|
2012-09-26 20:07:38 +00:00
|
|
|
$wdc->set_protocol(1); // which version should be used
|
2012-09-26 20:06:31 +00:00
|
|
|
|
2012-09-26 20:07:38 +00:00
|
|
|
// enable debugging
|
|
|
|
$wdc->set_debug(false); // debugging on or off?
|
|
|
|
/**
|
|
|
|
* End of settings
|
|
|
|
*/
|
2012-09-26 20:06:31 +00:00
|
|
|
|
2012-09-26 20:07:38 +00:00
|
|
|
// we try to connect
|
2012-09-26 20:06:31 +00:00
|
|
|
if (!$wdc->open()) {
|
|
|
|
print 'Error: could not open server connection';
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if server supports webdav rfc 2518
|
|
|
|
if (!$wdc->check_webdav()) {
|
|
|
|
print 'Error: server does not support webdav or user/password may be wrong';
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2012-09-26 20:07:38 +00:00
|
|
|
// create folder with date in scheme backup_31.01.2012-23.59
|
2012-09-26 20:06:31 +00:00
|
|
|
$date = date('d.m.Y-H.i',time());
|
2012-09-26 20:07:38 +00:00
|
|
|
$pathTod = $pathExt.'backup_'.$date.'/';
|
|
|
|
|
|
|
|
$httpStatusColl = $wdc->mkcol($pathTod);
|
|
|
|
print 'Todays Folder <backup_'.$date.'> webdav status: '.$httpStatusColl.'
|
2012-09-26 20:06:31 +00:00
|
|
|
|
|
|
|
';
|
|
|
|
|
2012-09-26 20:07:38 +00:00
|
|
|
print 'Datatransfer
|
|
|
|
';
|
2012-09-26 20:06:31 +00:00
|
|
|
|
2012-09-26 20:07:38 +00:00
|
|
|
// 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 ..
|
2012-09-26 20:06:31 +00:00
|
|
|
$file='';
|
|
|
|
} else {
|
2012-09-26 20:07:38 +00:00
|
|
|
echo $file ."\n"; // where we are at the moment
|
2012-09-26 20:06:31 +00:00
|
|
|
|
|
|
|
$handle = fopen ($filename, 'r');
|
|
|
|
$contents = fread ($handle, filesize ($filename));
|
|
|
|
fclose ($handle);
|
2012-09-26 20:07:38 +00:00
|
|
|
$target_path = $pathTod.$file;
|
2012-09-26 20:06:31 +00:00
|
|
|
$http_status = $wdc->put($target_path,$contents);
|
|
|
|
print 'webdav server returns ' . $http_status .'
|
2012-09-26 20:07:38 +00:00
|
|
|
'; // 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
|
|
|
|
}
|
2012-09-26 20:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dh);
|
2012-09-26 20:07:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2012-09-26 20:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-26 20:07:38 +00:00
|
|
|
$wdc->close(); // finish and goodbye
|
2012-09-26 20:06:31 +00:00
|
|
|
flush();
|