|
| 1 | +# Naming backups |
| 2 | + |
| 3 | +If you want to customize how backups are named and "discovered", you can! |
| 4 | + |
| 5 | +The default naming scheme will be: |
| 6 | + |
| 7 | +``` |
| 8 | +{app.name}-{timestamp}-{id}.zip |
| 9 | +``` |
| 10 | + |
| 11 | +## Customizing |
| 12 | + |
| 13 | +You can customize the naming by providing your own `BackupNameResolver` implementation. |
| 14 | + |
| 15 | +This class is responsible for generating filenames and parsing files into identifiable information and required metadata in the form of `ResolvedBackupData`. |
| 16 | +So when making your own implementation, you need to make sure that your generate and parseFilename methods work togheter or it will not work. |
| 17 | + |
| 18 | +Here is an example of a custom `BackupNameResolver` implementation: |
| 19 | + |
| 20 | +```php |
| 21 | +use Carbon\CarbonImmutable; |
| 22 | +use Itiden\Backup\Contracts\BackupNameResolver; |
| 23 | +use Itiden\Backup\DataTransferObjects\ResolvedBackupData; |
| 24 | + |
| 25 | +final readonly class MyAppSpecificBackupNameResolver implements BackupNameResolver |
| 26 | +{ |
| 27 | + private const string Separator = '---'; |
| 28 | + |
| 29 | + // return a custom filename, the ".zip" extension will be added automatically if it is missing |
| 30 | + public function generateFilename(CarbonImmutable $createdAt, string $id): string |
| 31 | + { |
| 32 | + $parts = [ |
| 33 | + "some-testest-that-implies-something", |
| 34 | + $createdAt->format('Y-m-d'), |
| 35 | + $id, |
| 36 | + ]; |
| 37 | + |
| 38 | + return implode(self::Separator, $parts); |
| 39 | + } |
| 40 | + |
| 41 | + public function parseFilename(string $path): ?ResolvedBackupData |
| 42 | + { |
| 43 | + $filename = pathinfo($path, PATHINFO_FILENAME); |
| 44 | + |
| 45 | + $parts = explode(self::Separator, $filename); |
| 46 | + |
| 47 | + // if the filename cannot be parsed, return null |
| 48 | + if (count($parts) !== 3) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + [$name, $date, $identifier] = $parts; |
| 53 | + |
| 54 | + $createdAt = CarbonImmutable::createFromFormat('Y-m-d', $date); |
| 55 | + |
| 56 | + return new ResolvedBackupData( |
| 57 | + createdAt: $createdAt, |
| 58 | + id: $identifier, |
| 59 | + name: $name |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
0 commit comments