Create custom backups from your website using...
来源:百度文库 编辑:神马文学网 时间:2024/07/04 23:36:40
Create custom backups from your website using cURL
Author: Olaf Filed under:PHP scripts,Tutorials
Wednesday
Jul 30,2008
These days I needed a script to backup only a part of a customers website using a CRON. Most of the control panels I know allow only a complete website backup and this is not what I needed. While plaaning the script, I thought about a solution for webmaster without full SSH access to their hosting account. A typical situation could be:
A shared hosting account that allows only backups for the whole site incl. database, emails and other settings No administration rights via SSH A FTP host for the storage of the the backup files Support for cURL and a default PHP5 configuration (sorry no more code for PHP4).
First of all we need to create a variable to define the local source directory on your website where you like to start the backup from:
define('BASEPATH', $_SERVER['DOCUMENT_ROOT'].’/');
The next code is to create a new class including the constructor function and some variables used in that class:
class Curl_ftp_backup {
public $basePath, $errors, $ftp_server, $ftp_user_pw , $msg, $email;
public function __construct($ftp_server, $ftp_userpw, $email_adr) {
$this->basePath = BASEPATH;
$this->errors = '';
$this->ftp_server = $ftp_server;
$this->ftp_user_pw = $ftp_userpw;
$this->email = $email_adr;
$this->msg = '';
}
Our class will store file and directories on the remote FTP server, the next function will create a directory if it not already exists:
private function curl_create_ftp_directory($name, $curr_dir = '') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->ftp_server);
curl_setopt($ch, CURLOPT_USERPWD, $this->ftp_user_pw);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$cmd = array();
if ($curr_dir != '') $cmd[] = ‘CWD ‘.$curr_dir;
$cmd[] = ‘MKD ‘.$name;
curl_setopt($ch, CURLOPT_POSTQUOTE, $cmd);
curl_exec ($ch);
return;
}
The following function will open a file and uploads the data to specified directory on the target server. For each file that is not stored the error number is returned, this information is used later to build up an error string.
private function curl_put_file($path) {
$ch = curl_init();
$fp = fopen($this->basePath.$path, "r");
curl_setopt($ch, CURLOPT_URL, $this->ftp_server.$path);
curl_setopt($ch, CURLOPT_USERPWD, $this->ftp_user_pw);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($this->basePath.$path));
curl_exec ($ch);
$error = curl_errno($ch);
curl_close ($ch);
if ($error != 0) $this->errors .= $path.PHP_EOL;
return;
}
Now we need a function that will loop through the specified base directory and will switch between directories and files; the previous functions will create the directory or upload the file.
public function backupfiles($directory) {
if ($handle = opendir($this->basePath.$directory)) {
$this->curl_create_ftp_directory($directory);
while (false !== ($file = readdir($handle))) {
if (is_file($this->basePath.$directory.$file) && $file != '.htaccess') {
$this->curl_put_file($directory.$file);
} elseif ($file != '.' && $file != '..' && is_dir($this->basePath.$directory.$file)) {
$this->backupfiles($directory.$file.'/', $directory);
}
}
closedir($handle);
if ($this->errors != '') {
$this->msg = 'Missing files: '.PHP_EOL.$this->errors;
} else {
$this->msg = 'FTP upload successful.';
}
} else {
$this->msg = 'Can\'t open local directory.';
}
}
public function send_emailmsg() {
if ($this->msg != '') {
$body = PHP_EOL.'Backup result:'.PHP_EOL.PHP_EOL.$this->msg;
mail($this->email, 'Your Backup on '.date('Y-m-d H:i:s'), $body);
}
}
At last there is a tiny function that will send you some short email message about the backup result. You can call the class in your PHP script:
$cb = new Curl_ftp_backup('ftp://ftp.yourserver.com/source_folder/', 'user:password', 'your@email.com');
$cb->backupfiles('includes/');
$cb->send_emailmsg();
Thats all, now you need to create a CRON job for the file that will execute the backup. If your hosting provider doesn’t provide CRON jobs, just google for “free cron jobs”.
Disclaimer:
We tested the script “successful” on two modern Linux web servers with ~100 files between 10kb and 300MB. If you have a backup with a lot of files you should think about a different solution. With every file or directory a new CURL function is called, this could result in a high server load. We are not responsible for any damage and/or data loss as the result off using this script.
Click here to download the PHP class script.
Author: Olaf Filed under:PHP scripts,Tutorials
Wednesday
Jul 30,2008
These days I needed a script to backup only a part of a customers website using a CRON. Most of the control panels I know allow only a complete website backup and this is not what I needed. While plaaning the script, I thought about a solution for webmaster without full SSH access to their hosting account. A typical situation could be:
A shared hosting account that allows only backups for the whole site incl. database, emails and other settings No administration rights via SSH A FTP host for the storage of the the backup files Support for cURL and a default PHP5 configuration (sorry no more code for PHP4).
First of all we need to create a variable to define the local source directory on your website where you like to start the backup from:
define('BASEPATH', $_SERVER['DOCUMENT_ROOT'].’/');
The next code is to create a new class including the constructor function and some variables used in that class:
class Curl_ftp_backup {
public $basePath, $errors, $ftp_server, $ftp_user_pw , $msg, $email;
public function __construct($ftp_server, $ftp_userpw, $email_adr) {
$this->basePath = BASEPATH;
$this->errors = '';
$this->ftp_server = $ftp_server;
$this->ftp_user_pw = $ftp_userpw;
$this->email = $email_adr;
$this->msg = '';
}
Our class will store file and directories on the remote FTP server, the next function will create a directory if it not already exists:
private function curl_create_ftp_directory($name, $curr_dir = '') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->ftp_server);
curl_setopt($ch, CURLOPT_USERPWD, $this->ftp_user_pw);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$cmd = array();
if ($curr_dir != '') $cmd[] = ‘CWD ‘.$curr_dir;
$cmd[] = ‘MKD ‘.$name;
curl_setopt($ch, CURLOPT_POSTQUOTE, $cmd);
curl_exec ($ch);
return;
}
The following function will open a file and uploads the data to specified directory on the target server. For each file that is not stored the error number is returned, this information is used later to build up an error string.
private function curl_put_file($path) {
$ch = curl_init();
$fp = fopen($this->basePath.$path, "r");
curl_setopt($ch, CURLOPT_URL, $this->ftp_server.$path);
curl_setopt($ch, CURLOPT_USERPWD, $this->ftp_user_pw);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($this->basePath.$path));
curl_exec ($ch);
$error = curl_errno($ch);
curl_close ($ch);
if ($error != 0) $this->errors .= $path.PHP_EOL;
return;
}
Now we need a function that will loop through the specified base directory and will switch between directories and files; the previous functions will create the directory or upload the file.
public function backupfiles($directory) {
if ($handle = opendir($this->basePath.$directory)) {
$this->curl_create_ftp_directory($directory);
while (false !== ($file = readdir($handle))) {
if (is_file($this->basePath.$directory.$file) && $file != '.htaccess') {
$this->curl_put_file($directory.$file);
} elseif ($file != '.' && $file != '..' && is_dir($this->basePath.$directory.$file)) {
$this->backupfiles($directory.$file.'/', $directory);
}
}
closedir($handle);
if ($this->errors != '') {
$this->msg = 'Missing files: '.PHP_EOL.$this->errors;
} else {
$this->msg = 'FTP upload successful.';
}
} else {
$this->msg = 'Can\'t open local directory.';
}
}
public function send_emailmsg() {
if ($this->msg != '') {
$body = PHP_EOL.'Backup result:'.PHP_EOL.PHP_EOL.$this->msg;
mail($this->email, 'Your Backup on '.date('Y-m-d H:i:s'), $body);
}
}
At last there is a tiny function that will send you some short email message about the backup result. You can call the class in your PHP script:
$cb = new Curl_ftp_backup('ftp://ftp.yourserver.com/source_folder/', 'user:password', 'your@email.com');
$cb->backupfiles('includes/');
$cb->send_emailmsg();
Thats all, now you need to create a CRON job for the file that will execute the backup. If your hosting provider doesn’t provide CRON jobs, just google for “free cron jobs”.
Disclaimer:
We tested the script “successful” on two modern Linux web servers with ~100 files between 10kb and 300MB. If you have a backup with a lot of files you should think about a different solution. With every file or directory a new CURL function is called, this could result in a high server load. We are not responsible for any damage and/or data loss as the result off using this script.
Click here to download the PHP class script.
Create custom backups from your website using...
Steps to Create a Web 2.0 Marketing Friendly Website
Using Java to Handle Custom WSDL Data Types
Access your Outlook application using PHP
10 Things That Will Make Or Break Your Website
10 Ways To Create Content For Your Weblog
how to create a MapTilePack on your own
WEBSITE
website
How To Create A RSS Feed From Any Web Page
Create a map from a published Google Spreadsheet
Using SPGridView to Display Your Data in WSS 3.0
FeedDirect - Promote & Profit From Your Conte...
uLFS: Your Manageable and Reusable Linux From...
Use eVB to create a scrollable forms on your Pocket PC
Using a Doc/View exported from a dynamically ...
30 SEO Tips in 30 Days to turn your website SEO ready - Jagadeesh M | SEO Tips, Tricks PodCast Blog
ELJonline: Using CompactFlash Cards in Your Embedded Linux System Jay Sissom ?
Using Web 2.0 to reinvent your business for the economic downturn | Enterprise Web 2.0 | ZDNet.com
The windmills of your mind from the thomas cr...
Using SNMP to Find a Port Number from a MAC Addres
Using a Doc/View exported from a dynamically loaded DLL (SDI)
website way
music website