Skip to content

Commit

Permalink
Switch to php native methods
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora committed Feb 24, 2024
1 parent 4ee05fb commit 901f459
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions commands/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
declare(strict_types = 1);

use Kirby\CLI\CLI;
use Kirby\Filesystem\Dir;
use Kirby\Toolkit\Str;

return [
'description' => 'Creates backup of application files',
Expand All @@ -14,44 +12,60 @@
]
],
'command' => static function (CLI $cli): void {
if (class_exists('ZipArchive') === false) {
throw new Exception('ZipArchive library could not be found');
}

$root = $cli->argOrPrompt(
'root',
'Which root should be backup? (press <Enter> to backup the entire kirby application)',
false
);
$root = empty($root) === true ? 'index' : $root;
$targetPath = $cli->kirby()->root($root);
$rootPath = $cli->kirby()->root($root);

if ($targetPath === null) {
if ($rootPath === null) {
throw new Exception('Invalid root entered: ' . $root);
}

$kirbyPath = $cli->kirby()->root('index');
$backupPath = $kirbyPath . '/backup';
$backupFile = $backupPath . '/' . $root . '-' . date('Y-m-d-His') . '.zip';
$relativeBackupFile = Str::after($backupFile, $kirbyPath);
$relativeTargetPath = trim(Str::after($targetPath, $kirbyPath), '/');

// execution commands list
$commands = [
// navigates to the target directory to ignore parent folders in zip file
'cd ' . escapeshellarg($targetPath) . ';',
// sets backup file path
'zip -r ' . escapeshellarg($backupFile),
// sets target backup directory
escapeshellarg($root === 'index' ? '*' : ('./' . $relativeTargetPath . '/*'))
];

// exclude backup directory from the root for only index root
if ($root === 'index') {
$commands[] = '-x ' . escapeshellarg('backup/*');
}
$kirbyPath = $cli->kirby()->root('index');
$backupPath = $kirbyPath . '/backup';
$backupFile = $backupPath . '/' . $root . '-' . date('Y-m-d-His') . '.zip';

// create backup directory before the process
Dir::make($backupPath);
mkdir($backupPath);

exec(implode(' ', $commands));
$zip = new ZipArchive();
if ($zip->open($backupFile, ZipArchive::CREATE) !== true) {
throw new Exception('Failed to create backup file');
}

$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator(
$rootPath,
FilesystemIterator::SKIP_DOTS
),
fn ($file) => $file->isFile() || in_array($file->getBaseName(), ['.git', 'backup']) === false
)
);

foreach ($files as $file) {
// skip directories, will be added automatically
if ($file->isDir() === false) {
// get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);

// add current file to archive
$zip->addFile($filePath, $relativePath);
}
}

if ($zip->close() === false) {
throw new Exception('There was a problem writing the backup file');
}

$cli->success('The backup has been created: ' . $relativeBackupFile);
$cli->success('The backup has been created: ' . substr($backupFile, strlen($kirbyPath)));
}
];

0 comments on commit 901f459

Please sign in to comment.