Your Ad Here

Posted By

theroamingcoder on 03/29/11


Tagged

backup s3


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

AllKnightAccess
notturnale
jamiepeloquin


Website Backup To Amazon S3 Storage (PHP Edition)


 / Published in: PHP
 

This handy little script will backup your website's database (MySQL) and files and put them into an amazon s3 bucket.

  1. <?php
  2.  
  3. //to use this script
  4. //1) Download S3.php from http://undesigned.org.za/2007/10/22/amazon-s3-php-class
  5. //2) Place the S3.php file in the same folder as this script
  6. //3) Adjust these variables accordingly
  7.  
  8. $access_key = '';
  9. $secret_key = '';
  10. $bucket = 'backups';
  11.  
  12. $site_name = 'mysite';
  13. $site_directory = "/path/to/public_html";
  14.  
  15. $db_host= "localhost";
  16. $db_user = "";
  17. $db_pass= "";
  18. $db_name= "";
  19.  
  20. $mail_on_error = TRUE;
  21. $mail_to = "";
  22.  
  23. //4)call this script with curl or wget in a cron task
  24. //curl --silent --compressed http://example.com/s3_backup.php?key=trc_2011
  25. //-or-
  26. //wget -O - -q -t 1 http://www.example.com/s3_backup.php?key=trc_2011
  27.  
  28. //little bit o' security so script cannot be misused
  29. if($_GET['key'] == 'trc_2011'){
  30.  
  31.  
  32. require 'S3.php';
  33.  
  34. try{
  35. $s3 = new S3($access_key, $secret_key);
  36. $s3->putBucket($bucket, S3::ACL_PRIVATE);
  37.  
  38. //get current dir
  39. $cwd = getcwd();
  40.  
  41. //create database dump
  42. echo "Creating database backup...<br/>";
  43. flush();
  44. $db_backup_file = $cwd.'/'.$site_name.'.sql';
  45. exec("mysqldump --add-drop-table --user=$db_user --password=$db_pass --host=$db_host $db_name > $db_backup_file");
  46.  
  47. //store it
  48. echo "Uploading database backup...<br/>";
  49. flush();
  50. $s3->putObjectFile($db_backup_file, $bucket, $site_name.'/'.$site_name.'.sql', S3::ACL_PRIVATE);
  51.  
  52. //delete it
  53. unlink($db_backup_file);
  54.  
  55. //create filesystem backup
  56. echo "Creating filesystem backup...<br/>";
  57. flush();
  58. $fs_backup_file = $cwd.'/'.$site_name.'.tgz';
  59.  
  60. //change to site directory so .tgz will have correct paths when extracted
  61. chdir($site_directory);
  62.  
  63. //create backup
  64. exec("tar -czf $fs_backup_file *");
  65.  
  66. //change back to original directory
  67. chdir($cwd);
  68.  
  69. //store it
  70. echo "Uploading filesystem backup...<br/>";
  71. flush();
  72. $s3->putObjectFile($fs_backup_file, $bucket, $site_name.'/'.$site_name.'.tgz', S3::ACL_PRIVATE);
  73.  
  74. //delete it
  75. unlink($fs_backup_file);
  76.  
  77. echo "Backup Complete";
  78. }
  79. catch(Exception $e){
  80. echo $e->getMessage();
  81. if($mail_on_error){
  82. $subject = "Site Backup Script Error - ".$site_name;
  83. $body = "S3 Site Backup Script Failed : ".$e->getMessage();
  84. mail($mail_to, $subject, $body);
  85. }
  86. }
  87.  
  88. }
  89.  
  90. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Rincewind on March 30, 2011

Great little snippet. If I could suggest a couple of improvements:

1) It would probably be wise to compress your SQL dump file, as it'll compress incredibly well (the tar file of your filesystem could probably benefit from compression too). This could save you quite a bit of money with Amazon in the long run! 2) Encrypt your backups, as they might contain sensitive information (such as database passwords etc.). Even specifying a 7zip/zip password would work.

And finally, if you want a free way to back up your website and database to S3, you could use my service where I've done it all for you! http://www.backupmachine.com/

You need to login to post a comment.