Skip to content

Commit 3227837

Browse files
Merge pull request #40 from move-elevator/feat/mittwald-backup-exclude
feat: exclude regenerable cache directories from hosting backups
2 parents dd2c51b + 332197c commit 3227837

5 files changed

Lines changed: 70 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Read the [documentation](docs/TYPO3.md) for detailed installation instructions a
6565
- [Development](docs/DEV.md)
6666
- [Debug helper](docs/DEBUG.md)
6767
- [Requirements](docs/REQUIREMENTS.md)
68+
- [Backup exclusion](docs/BACKUP.md)
6869

6970

7071
## 💛 Acknowledgements

autoload.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
require_once(__DIR__ . '/deployer/sync/config/set.php');
2929
require_once(__DIR__ . '/deployer/sync/task/database_backup.php');
3030

31+
/*
32+
* backup
33+
*/
34+
require_once(__DIR__ . '/deployer/backup/config/set.php');
35+
require_once(__DIR__ . '/deployer/backup/task/backup_exclude_cache.php');
36+
3137
/*
3238
* security
3339
*/

deployer/backup/config/set.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Deployer;
4+
5+
/**
6+
* Release-local directories that are fully regenerated on every deploy (composer/npm install,
7+
* cache warmup) and therefore don't need to be part of a hosting backup.
8+
*/
9+
set('backup_exclude_dirs', [
10+
'vendor',
11+
'var/cache',
12+
]);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Deployer;
4+
5+
// Cache Directory Tagging Specification (https://bford.info/cachedir/), honored by
6+
// Mittwald's hosting backups as well as tools like rsync/borg/restic --exclude-caches.
7+
const CACHEDIR_TAG_SIGNATURE = 'Signature: 8a477f597d28d172789f06886806bc55';
8+
9+
task('backup:exclude_cache', function () {
10+
foreach (get('backup_exclude_dirs') as $dir) {
11+
$dir = trim($dir, '/');
12+
13+
if ('' === $dir || in_array('..', explode('/', $dir), true) || in_array('.', explode('/', $dir), true)) {
14+
warning("Skipping invalid backup_exclude_dirs entry: \"$dir\"");
15+
continue;
16+
}
17+
18+
if (!test("[ -d '{{ release_path }}/$dir' ]")) {
19+
continue;
20+
}
21+
22+
run("echo '" . CACHEDIR_TAG_SIGNATURE . "' > '{{ release_path }}/$dir/CACHEDIR.TAG'");
23+
debug("Tagged {{ release_path }}/$dir as excluded from backups (CACHEDIR.TAG)");
24+
}
25+
})
26+
->desc('Tag regenerable cache directories to exclude them from hosting backups')
27+
;
28+
29+
before('deploy:symlink', 'backup:exclude_cache');

docs/BACKUP.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Backup exclusion
2+
3+
Excludes regenerable, release-local directories (composer/npm build artifacts, framework caches) from hosting backups by tagging them with a `CACHEDIR.TAG` file, following the [Cache Directory Tagging Specification](https://bford.info/cachedir/). This is honored by [Mittwald's automatic backups](https://developer.mittwald.de/de/docs/v2/guides/operations/backup-exclude/) and by common backup/archive tools (`rsync`, `borg`, `restic`, ... with `--exclude-caches`).
4+
5+
## General
6+
7+
The `backup:exclude_cache` task runs automatically before `deploy:symlink`, tagging every directory listed in `backup_exclude_dirs` (relative to `{{release_path}}`) that exists in the current release.
8+
9+
The default settings can be found within the [set.php](../deployer/backup/config/set.php) file.
10+
11+
```php
12+
set('backup_exclude_dirs', [
13+
'vendor',
14+
'var/cache',
15+
]);
16+
```
17+
18+
Adjust the list per project, e.g. to add framework-specific temp directories, or set it to `[]` to disable the task entirely.
19+
20+
## Caution
21+
22+
Directories tagged this way are excluded from backups entirely and **cannot be restored**, not even by the hoster. Only list directories whose content is fully regenerated by the deploy process (`composer install`, cache warmup) — never shared or writable directories such as `fileadmin`, `uploads`, or `var/log`.

0 commit comments

Comments
 (0)