Skip to content

Commit 3613e4f

Browse files
chore(docs): improve naming docs
Fixes: #61
1 parent ced0c23 commit 3613e4f

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

docs/pages/naming-backups.md

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
11
# Naming backups
22

3-
If you want to customize how backups are named and "discovered", you can!
3+
Sometimes you need to customize the naming of your backupfiles, maybe you download them often and want to have "prettier" names for them, or maybe you want to have a specific naming scheme for your backups due to some other reason.
4+
This package provides a way to customize the naming of your backup files.
45

5-
The default naming scheme will be:
6+
You can customize the naming by providing your own `BackupNameResolver` implementation, [Read more](#making-your-own-implementation).
7+
8+
## Default naming scheme
9+
10+
The default driver for naming backups is `Itiden\Backup\GenericBackupNameResolver`, which will generate filenames like this:
611

712
```
8-
{app.name}-{timestamp}-{id}.zip
13+
{app.name}---{timestamp}---{id}.zip
914
```
1015

11-
## Customizing
16+
The reason for this format is that it is easy to parse and it contains all the information you need to identify the backup.
17+
18+
> [!INFO]
19+
> When you upload a backup, it will be renamed with with the naming scheme of the driver you are using.
20+
21+
## The inner workings
22+
23+
### `generateFilename`
1224

13-
You can customize the naming by providing your own `BackupNameResolver` implementation.
25+
Will be provided with a `CarbonImmutable` and a `string` identifier (ulid), the returned string can be a path and the `zip` extension will be appended if it isn't there already.
1426

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.
27+
> [!CAUTION]
28+
> The provided identifier MUST be included in the filename since it will be used to find the backup after creation.
29+
30+
### `parseFilename`
31+
32+
Will be provided with storages path to the file, this path is relative to the configured backup location, so it will look something like this:
33+
34+
```
35+
config('backup.destination.path')/laravel---1696156800---unique-id.zip
36+
```
37+
38+
> [!TIP]
39+
> If you only want to parse the filename, you can use `pathinfo($path, PATHINFO_FILENAME)` to get the filename.
40+
41+
It should return a `ResolvedBackupData` dto with the must-have information about the backup, or null if the data couldn't be resolved and the path should be ignored.
42+
43+
## Making your own implementation
44+
45+
When making your own `BackupNameResolver` implementation, it is important that your `generateFilename` and `parseFilename` methods work togheter or your backups might become undiscoverable.
46+
47+
> [!TIP]
48+
> The `parseFilename` method will be provided a "relative" path for each file in the configured backup location, so you can save and resolve backups in subdirectories!
1749
1850
Here is an example of a custom `BackupNameResolver` implementation:
1951

@@ -26,37 +58,46 @@ final readonly class MyAppSpecificBackupNameResolver implements BackupNameResolv
2658
{
2759
private const string Separator = '---';
2860

61+
public function __construct(
62+
private GenericBackupNameResolver $previouslyUsedBackupNameResolver,
63+
) {
64+
}
65+
2966
// return a custom filename, the ".zip" extension will be added automatically if it is missing
3067
public function generateFilename(CarbonImmutable $createdAt, string $id): string
3168
{
3269
$parts = [
33-
"some-testest-that-implies-something",
34-
$createdAt->format('Y-m-d'),
70+
"some-name-that-implies-something",
71+
$createdAt->timestamp,
3572
$id,
3673
];
3774

38-
return implode(self::Separator, $parts);
75+
// here we are storing the the backup in a directory named after the date, might be nice if you often view the backups in a file browser
76+
return $createdAt->format('Y-m-d') . '/' . implode(self::Separator, $parts);
3977
}
4078

4179
public function parseFilename(string $path): ?ResolvedBackupData
4280
{
81+
// The path in this example will be something like "statamic-backups/2023-10-01/some-name-that-implies-something---1696156800---unique-id.zip"
82+
4383
$filename = pathinfo($path, PATHINFO_FILENAME);
4484

4585
$parts = explode(self::Separator, $filename);
4686

47-
// if the filename cannot be parsed, return null
87+
// Here we can return null, or maybe use a fallback to try and parse the filename
88+
// if we don't have the expected amount of parts
89+
// or if the parts don't match our expectations
90+
// - it's up to you!
4891
if (count($parts) !== 3) {
49-
return null;
92+
return $this->previouslyUsedBackupNameResolver->parseFilename($path);
5093
}
5194

5295
[$name, $date, $identifier] = $parts;
5396

54-
$createdAt = CarbonImmutable::createFromFormat('Y-m-d', $date);
55-
5697
return new ResolvedBackupData(
57-
createdAt: $createdAt,
5898
id: $identifier,
5999
name: $name
100+
createdAt: CarbonImmutable::createFromFormat('Y-m-d', $date),
60101
);
61102
}
62103
}

0 commit comments

Comments
 (0)