Browse Source

Exclude dirs with ZIP and delete old filesystem backups

master
Christoph Miksche 2 years ago
parent
commit
ada533d936
1 changed files with 39 additions and 2 deletions
  1. +39
    -2
      backup.php

+ 39
- 2
backup.php View File

@@ -3,12 +3,29 @@
/**
* Compression and backup dir
*/
define("COMPRESS", 'GZ'); // GZ for .gz, ZIP for .zip and anything other for no file backup
define("COMPRESS", 'ZIP'); // GZ for .gz, ZIP for .zip and anything other for no file backup
define("BACKUP_DIR", 'backup');
/**
* Delete Backups after X days
*/
define("DELETE_BACKUPS", 5);
/**
* Exclude directorys from backup
*
*/
const EXCLUDE_DIRS = [
'uploads',
'backup',
'backups',
'sicherungen',
'old',
'tmp',
'media',
'php-sharedhost-backup',
'wp-admin',
'wp-includes',
'wp-content'
];
/**
* Define database parameters here
*/
@@ -45,6 +62,20 @@ function zipData($source, $destination) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = realpath($file);
// Logic to exclude dirs
$cont_foreach = 0;
foreach (EXCLUDE_DIRS as $excl_dir){
if (strpos($file, $excl_dir) !== FALSE) {
$cont_foreach = 1;
}
}
if($cont_foreach){
continue;
}

if (is_dir($file)) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
} else if (is_file($file)) {
@@ -68,7 +99,7 @@ function zipData($source, $destination) {
$backupDatabase = new Backup_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$backupDatabase->backupTables(TABLES, BACKUP_DIR) ? 'OK' : 'KO';

// Delete old Backup files
// Delete old Database Backup files
$date = date("Ymd", strtotime("-".DELETE_BACKUPS." day"));
foreach(glob(BACKUP_DIR.'/'.'myphp-backup-'.DB_NAME.'-'.$date.'*') as $f) {
unlink($f);
@@ -77,6 +108,12 @@ foreach(glob(BACKUP_DIR.'/'.'myphp-backup-'.DB_NAME.'-'.$date.'*') as $f) {
// Backup file name
$bckfile = BACKUP_DIR.'/'.date('Y-m-d').'-files.tar';

// Delete old Filesystem Backup files
$date = date("Y-m-d", strtotime("-".DELETE_BACKUPS." day"));
foreach(glob(BACKUP_DIR.'/'.$date.'-files.*') as $f) {
unlink($f);
}

// .zip compression
if(COMPRESS == 'ZIP'){
zipData('../', $bckfile);


Loading…
Cancel
Save