-
-
Notifications
You must be signed in to change notification settings - Fork 764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize storage and speed when making an encrypted backup #1825
base: v8
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Spatie\Backup\Tasks\Backup; | ||
|
||
class Encryption | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this class to |
||
{ | ||
protected string $password; | ||
|
||
protected int $method; | ||
|
||
public function __construct(string $password, int $method) | ||
{ | ||
$this->password = $password; | ||
|
||
$this->method = $method; | ||
} | ||
|
||
public function getPassword(): string | ||
{ | ||
return $this->password; | ||
} | ||
|
||
public function getMethod(): int | ||
{ | ||
return $this->method; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ class Zip | |
|
||
protected string $pathToZip; | ||
|
||
protected ?Encryption $encryption = null; | ||
|
||
public static function createForManifest(Manifest $manifest, string $pathToZip): self | ||
{ | ||
$relativePath = config('backup.backup.source.files.relative_path') ? | ||
|
@@ -27,6 +29,8 @@ public static function createForManifest(Manifest $manifest, string $pathToZip): | |
$zip->add($file, self::determineNameOfFileInZip($file, $pathToZip, $relativePath)); | ||
} | ||
|
||
$zip->encrypt(); | ||
|
||
$zip->close(); | ||
|
||
return $zip; | ||
|
@@ -87,6 +91,11 @@ public function close(): void | |
$this->zipFile->close(); | ||
} | ||
|
||
public function setPassword(string $password): void | ||
{ | ||
$this->zipFile->setPassword($password); | ||
} | ||
|
||
public function add(string|iterable $files, ?string $nameInZip = null): self | ||
{ | ||
if (is_array($files)) { | ||
|
@@ -126,4 +135,44 @@ public function count(): int | |
{ | ||
return $this->fileCount; | ||
} | ||
|
||
public function encrypt() | ||
{ | ||
$this->loadEncryption(); | ||
|
||
if ($this->getEncryption()) { | ||
$this->setPassword($this->getEncryption()->getPassword()); | ||
|
||
foreach (range(0, $this->zipFile->numFiles - 1) as $i) { | ||
$this->zipFile->setEncryptionIndex($i, $this->getEncryption()->getMethod()); | ||
} | ||
} | ||
} | ||
|
||
public function getEncryption(): ?Encryption | ||
{ | ||
return $this->encryption; | ||
} | ||
|
||
public function loadEncryption() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move all of this logic to a new static method |
||
{ | ||
$password = config('backup.backup.password'); | ||
$method = config('backup.backup.encryption'); | ||
|
||
if ($method === 'default') { | ||
$method = defined("\ZipArchive::EM_AES_256") | ||
? ZipArchive::EM_AES_256 | ||
: null; | ||
} | ||
|
||
if ($password === null) { | ||
return false; | ||
} | ||
|
||
if (! is_int($method)) { | ||
return false; | ||
} | ||
|
||
$this->encryption = new Encryption($password, $method); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should only send notifications if
$this->notifications
is set totrue