Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. <?php
  2.  
  3. use Aws\S3\S3Client;
  4. use Aws\S3\Exception\S3Exception;
  5.  
  6. /* creates a compressed zip file */
  7. error_reporting(E_ALL);
  8. ini_set('display_errors', '1');
  9. ini_set('max_execution_time', 0);
  10. //require_once './sql_backup.php';
  11.  
  12.  
  13. $sqlfilename = 'database_backup_' . date('G_a_m_d_y') . '.sql';
  14.  
  15. $result = exec('mysqldump <DBNAME> --password=<PASSWORD> --user=<USERNAME> >' . $sqlfilename, $output);
  16. echo "Backup Started.. <br />";
  17.  
  18. /**
  19. * Instantiate Backup_Database and perform backup
  20. */
  21. $rootPath = realpath('../');
  22.  
  23.  
  24. $filename = date("d-m-Y", time());
  25.  
  26. $zip = new ZipArchive();
  27. $zip->open('../' . $filename . '.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
  28.  
  29.  
  30. $files = new RecursiveIteratorIterator(
  31. new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY
  32. );
  33.  
  34. foreach ($files as $name => $file) {
  35.  
  36. if (!$file->isDir()) {
  37.  
  38. $filePath = $file->getRealPath();
  39. $relativePath = substr($filePath, strlen($rootPath) + 1);
  40. $filePaths = explode(".", $filePath);
  41. $ext = end($filePaths);
  42. $exclude = array(
  43. 'editorconfig',
  44. 'gitattributes',
  45. 'gitignore',
  46. 'htaccess',
  47. 'yml',
  48. 'zip',
  49. 'psd',
  50. );
  51.  
  52. $excludeFolder = array(
  53. '.git',
  54. 'logs',
  55. 'nbproject',
  56. 'tmp',
  57. 'vendor',
  58. 'web',
  59. );
  60.  
  61. $relativePathString = explode("/", $relativePath);
  62.  
  63. if (!in_array($ext, $exclude) && !in_array($relativePathString[0], $excludeFolder)) {
  64. $zip->addFile($filePath, $relativePath);
  65. }
  66.  
  67. // Add current file to archive
  68. }
  69. }
  70. // Zip archive will be created only after closing object
  71. if ($zip->close()) {
  72. echo "Backup Created <br /> Now Uploading...";
  73.  
  74. require '../aws/aws-autoloader.php';
  75.  
  76. $s3 = new S3Client([
  77. 'version' => 'latest',
  78. 'region' => 'ap-southeast-1',
  79. 'credentials' => [
  80. 'key' => '<KEY>',
  81. 'secret' => '<SECRET'
  82. ]
  83. ]);
  84.  
  85. try {
  86.  
  87. $fileUploads = '../' . $filename . '.zip';
  88. //$fileUploads = '../README.zip';
  89. $date = new DateTime();
  90. $date->sub(new DateInterval('P30D'));
  91. $s3->deleteObject([
  92. 'Bucket' => 'savesavvi',
  93. 'Key' => $date->format('d-m-Y') . '.zip',
  94. ]);
  95.  
  96. $r = $s3->putObject([
  97. 'Bucket' => 'savesavvi',
  98. 'Key' => $filename . '.zip',
  99. 'Body' => fopen($fileUploads, 'r'),
  100. 'ACL' => 'public-read',
  101. ]);
  102.  
  103. echo $r['ObjectURL'];
  104.  
  105. if (file_exists($fileUploads)) {
  106. unlink($fileUploads);
  107. }
  108. } catch (Aws\Exception\S3Exception $e) {
  109. echo "There was an error uploading the file.\n";
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement