You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.6KB

  1. <?php
  2. /**
  3. * Compression and backup dir
  4. */
  5. define("COMPRESS", 'ZIP'); // GZ for .gz, ZIP for .zip and anything other for no file backup
  6. define("BACKUP_DIR", 'backup');
  7. /**
  8. * Delete Backups after X days
  9. */
  10. define("DELETE_BACKUPS", 5);
  11. /**
  12. * Exclude directorys from backup
  13. *
  14. */
  15. const EXCLUDE_DIRS = [
  16. 'uploads',
  17. 'backup',
  18. 'backups',
  19. 'sicherungen',
  20. 'old',
  21. 'tmp',
  22. 'media',
  23. 'php-sharedhost-backup',
  24. 'wp-admin',
  25. 'wp-includes',
  26. 'wp-content'
  27. ];
  28. /**
  29. * Define database parameters here
  30. */
  31. define("DB_USER", 'your_username');
  32. define("DB_PASSWORD", 'your_password');
  33. define("DB_NAME", 'your_db_name');
  34. define("DB_HOST", 'localhost');
  35. define("TABLES", '*'); // Full backup
  36. //define("TABLES", 'table1 table2 table3'); // Partial backup
  37. define("CHARSET", 'utf8');
  38. define("GZIP_BACKUP_FILE", true); // Set to false if you want plain SQL backup files (not gzipped)
  39. // Make sure the script can handle large folders/files
  40. ini_set('max_execution_time', 900);
  41. ini_set('memory_limit', '2048M');
  42. // Set script max execution time
  43. set_time_limit(900);
  44. // Database Backup Skript from https://github.com/daniloaz/myphp-backup
  45. require_once('myphp-backup/myphp-backup.php');
  46. // Function to Recursively Backup Files & Folders to ZIP-File
  47. /*
  48. * PHP: Recursively Backup Files & Folders to ZIP-File
  49. * MIT-License - 2012-2018 Marvin Menzerath
  50. */
  51. function zipData($source, $destination) {
  52. if (extension_loaded('zip')) {
  53. if (file_exists($source)) {
  54. $zip = new ZipArchive();
  55. if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
  56. $source = realpath($source);
  57. if (is_dir($source)) {
  58. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
  59. foreach ($files as $file) {
  60. $file = realpath($file);
  61. // Logic to exclude dirs
  62. $cont_foreach = 0;
  63. foreach (EXCLUDE_DIRS as $excl_dir){
  64. if (strpos($file, $excl_dir) !== FALSE) {
  65. $cont_foreach = 1;
  66. }
  67. }
  68. if($cont_foreach){
  69. continue;
  70. }
  71. if (is_dir($file)) {
  72. $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
  73. } else if (is_file($file)) {
  74. $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
  75. }
  76. }
  77. } else if (is_file($source)) {
  78. $zip->addFromString(basename($source), file_get_contents($source));
  79. }
  80. }
  81. return $zip->close();
  82. }
  83. }
  84. return false;
  85. }
  86. /**
  87. * Instantiate Backup_Database and perform backup
  88. */
  89. $backupDatabase = new Backup_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  90. $backupDatabase->backupTables(TABLES, BACKUP_DIR) ? 'OK' : 'KO';
  91. // Delete old Database Backup files
  92. $date = date("Ymd", strtotime("-".DELETE_BACKUPS." day"));
  93. foreach(glob(BACKUP_DIR.'/'.'myphp-backup-'.DB_NAME.'-'.$date.'*') as $f) {
  94. unlink($f);
  95. }
  96. // Backup file name
  97. $bckfile = BACKUP_DIR.'/'.date('Y-m-d').'-files.tar';
  98. // Delete old Filesystem Backup files
  99. $date = date("Y-m-d", strtotime("-".DELETE_BACKUPS." day"));
  100. foreach(glob(BACKUP_DIR.'/'.$date.'-files.*') as $f) {
  101. unlink($f);
  102. }
  103. // .zip compression
  104. if(COMPRESS == 'ZIP'){
  105. zipData('../', $bckfile);
  106. }
  107. // .gz compression
  108. elseif(COMPRESS == 'GZ'){
  109. // Phar
  110. $phar = new PharData($bckfile);
  111. // add all files
  112. $phar->buildFromDirectory('../');
  113. //$phar->compress(Phar::GZ);
  114. file_put_contents($bckfile.'.gz' , gzencode(file_get_contents($bckfile)));
  115. }