From 901f4593d814d8fbdac8047fbc524f04fbde7798 Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Sat, 24 Feb 2024 14:00:27 +0300 Subject: [PATCH] Switch to php native methods --- commands/backup.php | 68 +++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/commands/backup.php b/commands/backup.php index 100eb97..5f6b2eb 100644 --- a/commands/backup.php +++ b/commands/backup.php @@ -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', @@ -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 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))); } ];