>>  Site Map >>  Forums >>  PHP+MySql

Forum module - topics in forum:



PHP+MySql - Coding mã nguồn mở rât thông dụng để làm web, và hệ thống cơ sỏ dữ liệu rất mạnh duợc ưa chuộng



Tự động backup lưu trữ cả trang web theo định kì và gủi mail th

vAutomated Site Backups
Here is a php script that will create a compressed backup of your website files, creating a different file each day for a month (then start over) so you have 30 days of backups. It will email you with a backup confirmation. Run it every night using cron to kick it off. You should download the backup files from the server on a regular basis.

- - Start Script Here - -
Code: :
<?php
$emailaddress = "XXXXXX@yourdomain.com";
$target = "/home/".get_current_user()."/backup.".date(d).".tar.gz";
if (file_exists($target)) unlink($target);
system("tar --create --preserve --gzip  --file=".$target." ~/public_html ~/mail",$result);
$size = filesize($target);
switch ($size) {
  case ($size>=1048576): $size = round($size/1048576) . " MB"; break;
  case ($size>=1024);    $size = round($size/1024) . " KB"; break;
  default:               $size = $size . " bytes"; break;
}
$message = "The website backup has been run.\n\n";
$message .= "The return code was: " . $result . "\n\n";
$message .= "The file path is: " . $target . "\n\n";
$message .= "Size of the backup: " . $size . "\n\n";
$message .= "Server time of the backup: " . date(" F d h:ia") . "\n\n";
mail($emailaddress, "Website Backup Message" , $message, "From: Website <>");
?>
- - End Script Here - -

If you want to exclude a directory, add the --exclude parm to the tar command as shown in the example below (which will exlcude the public_html/audio directory):
system("tar --create --preserve --gzip --file=".$target." --exclude ~/public_html/audio ~/public_html ~/mail",$result);

Please note that this script is very resource intensive. Do not run it more than once a day, and do not run it at peak times. If you have a large site the script may timeout depending on the server settings, but the backup may still be created.

Quote: :
* Before using cron, run the script from your browser to make sure it works as anticipated.
* Set a cron job to run the script once a day (at an odd time - like 4:23am - to minimize server load issues).
* This example backs up the public_html and mail directories.
* The backup file (named backup.XX.tar.gz) will be created in your root directory (above public_html).





Search from ALEXA


put your ads here