diff --git a/src/Backends/Jellyfin/Action/Backup.php b/src/Backends/Jellyfin/Action/Backup.php index 3647e88f..b7f79cc3 100644 --- a/src/Backends/Jellyfin/Action/Backup.php +++ b/src/Backends/Jellyfin/Action/Backup.php @@ -9,11 +9,14 @@ use App\Backends\Common\GuidInterface as iGuid; use App\Backends\Jellyfin\JellyfinClient as JFC; use App\Libs\Entity\StateInterface as iState; use App\Libs\Mappers\ImportInterface as iImport; +use App\Libs\Options; use SplFileObject; use Throwable; class Backup extends Import { + private const JSON_FLAGS = JSON_INVALID_UTF8_IGNORE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; + protected function process( Context $context, iGuid $guid, @@ -132,12 +135,9 @@ class Backup extends Import } } - $writer->fwrite( - PHP_EOL . json_encode( - $arr, - JSON_INVALID_UTF8_IGNORE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE - ) . ',' - ); + if (false === (bool)ag($opts, Options::DRY_RUN, false)) { + $writer->fwrite(PHP_EOL . json_encode($arr, self::JSON_FLAGS) . ','); + } } catch (Throwable $e) { $this->logger->error( 'Unhandled exception was thrown during handling of [%(backend)] [%(library.title)] [%(item.title)] backup.', diff --git a/src/Backends/Plex/Action/Backup.php b/src/Backends/Plex/Action/Backup.php index 43147eb2..ca809ac7 100644 --- a/src/Backends/Plex/Action/Backup.php +++ b/src/Backends/Plex/Action/Backup.php @@ -9,11 +9,14 @@ use App\Backends\Common\GuidInterface as iGuid; use App\Backends\Plex\PlexClient; use App\Libs\Entity\StateInterface as iState; use App\Libs\Mappers\ImportInterface as iImport; +use App\Libs\Options; use SplFileObject; use Throwable; final class Backup extends Import { + private const JSON_FLAGS = JSON_INVALID_UTF8_IGNORE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; + protected function process( Context $context, iGuid $guid, @@ -135,12 +138,9 @@ final class Backup extends Import } } - $writer->fwrite( - PHP_EOL . json_encode( - $arr, - JSON_INVALID_UTF8_IGNORE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE - ) . ',' - ); + if (false === (bool)ag($opts, Options::DRY_RUN, false)) { + $writer->fwrite(PHP_EOL . json_encode($arr, self::JSON_FLAGS) . ','); + } } catch (Throwable $e) { $this->logger->error( 'Unhandled exception was thrown during handling of [%(backend)] [%(library.title)] [%(item.title)] backup.', diff --git a/src/Commands/State/BackupCommand.php b/src/Commands/State/BackupCommand.php index 0894ae04..3074f8b8 100644 --- a/src/Commands/State/BackupCommand.php +++ b/src/Commands/State/BackupCommand.php @@ -37,9 +37,6 @@ class BackupCommand extends Command protected function configure(): void { - $cmdContext = trim(commandContext()); - $backupDir = after(Config::get('path') . '/backup/', ROOT_PATH); - $this->setName(self::ROUTE) ->setDescription('Backup backends play state.') ->addOption( @@ -65,51 +62,59 @@ class BackupCommand extends Command InputOption::VALUE_REQUIRED, 'Full path backup file. Will only be used if backup list is 1' ) - ->addOption('servers-filter', null, InputOption::VALUE_OPTIONAL, '[DEPRECATED] Select backends.', '') ->setHelp( - <<portable backup of your backends play state that can be used to restore any supported backend type. + r( + <<[ Important info ] ------------------- + Generate portable backup of your backends play state that can be used to restore any supported backend type. -The command will only work on backends that has import enabled. + ------------------ + [ Important info ] + ------------------ -Backups generated without [-k, --keep] flag are subject to be REMOVED during system:prune run. -To keep permanent copy of your backups you can use the [-k, --keep] flag. For example: + The command will only work on backends that has import enabled. + + Backups generated without [-k, --keep] flag are subject to be REMOVED during system:prune run. + To keep permanent copy of your backups you can use the [-k, --keep] flag. For example: -{$cmdContext} state:backup --keep [--select-backends my_backend] + {cmd} {route} --keep [--select-backends backend_name] -Backups generated with --keep flag will not contain a date and will be named [{backend}.json] where automated backups -will be named [{backend}.{date}.json] + Backups generated with [-k, --keep] flag will not contain a date and will be named [backend_name.json] + where automated backups will be named [backend_name.00000000{date}.json] -If a backup already exists using the same filename, it will be overwritten. + If filename already exists, it will be overwritten. -------- -[ FAQ ] -------- + ------- + [ FAQ ] + ------- -# Where are my backups stored? + # Where are my backups stored? -By defualt we store backups at {$backupDir} + By default, we store backups at [{backupDir}]. -# Why the externals ids are not exactly the same from backend? + # Why the external ids are not exactly the same from backend? -By defualt we enhance the data from the backend to allow the backup to be usuable by all if your backends, -The expanded externals ids make the data more portable, However, if you do not wish to have this enabled. You can -Disable it via the flag [--no-enhance]. We recommand to keep this option enabled. + By default we enhance the data from the backend to allow the backup to be usable by all if your backends, + The expanded external ids make the data more portable, However, if you do not wish to have this enabled. You can + disable it via [--no-enhance] flag. We recommend to keep this option enabled. -# I want different file name for my backup? + # I want different file name for my backup? -Backup names are something tricky, however it's possible to choose the backup filename if the total number -of backed up backends are 1. So, in essence you have to combine two flags [-s, --select-backends] and [--file]. + Backup names are something tricky, however it's possible to choose the backup filename if the total number + of backed up backends are 1. So, in essence you have to combine two flags [-s, --select-backends] and [--file]. -For example, to backup [my_backend] backend data to [/tmp/my_backend.json] do the following: + For example, to backup [backend_name] backend data to [/tmp/backend_name.json] do the following: -{$cmdContext} state:backup --select-backends my_backend --file /tmp/my_backend.json + {cmd} {route} --select-backends backend_name --file /tmp/my_backend.json -HELP + HELP, + [ + 'cmd' => trim(commandContext()), + 'route' => self::ROUTE, + 'backupDir' => after(Config::get('path') . '/backup', ROOT_PATH), + + ] + ) ); } @@ -125,21 +130,9 @@ HELP Config::save('servers', Yaml::parseFile($this->checkCustomBackendsFile($config))); } - $selectBackends = (string)$input->getOption('select-backends'); - $serversFilter = (string)$input->getOption('servers-filter'); - - if (!empty($serversFilter)) { - $this->logger->warning( - 'The [--servers-filter] flag is deprecated and will be removed in v1.0. Use [--select-backends].' - ); - if (empty($selectBackends)) { - $selectBackends = $serversFilter; - } - } - $list = []; - $selected = explode(',', $selectBackends); - $isCustom = !empty($selectBackends) && count($selected) >= 1; + $selected = explode(',', (string)$input->getOption('select-backends')); + $isCustom = !empty($selected) && count($selected) >= 1; $supported = Config::get('supported', []); $mapperOpts = []; @@ -162,7 +155,7 @@ HELP $type = strtolower(ag($backend, 'type', 'unknown')); if ($isCustom && $input->getOption('exclude') === in_array($backendName, $selected)) { - $this->logger->info('SYSTEM: Ignoring [%(backend)] as requested by servers filter flag.', [ + $this->logger->info('SYSTEM: Ignoring [%(backend)] as requested by select backends flag.', [ 'backend' => $backendName, ]); continue; @@ -260,24 +253,30 @@ HELP $fileName = $file; } - $fileName = r($fileName, [ - 'backend' => ag($backend, 'name'), - 'date' => makeDate()->format('Ymd'), - ]); + if (false === $input->getOption('dry-run')) { + $fileName = r($fileName, [ + 'backend' => ag($backend, 'name'), + 'date' => makeDate()->format('Ymd'), + ]); - if (!file_exists($fileName)) { - touch($fileName); + if (!file_exists($fileName)) { + touch($fileName); + } + + $backend['fp'] = new SplFileObject($fileName, 'wb+'); + $backend['fp']->fwrite('['); } - $backend['fp'] = new SplFileObject($fileName, 'wb+'); - - $backend['fp']->fwrite('['); - array_push( $queue, - ...$backend['class']->backup($this->mapper, $backend['fp'], [ - 'no_enhance' => true === $input->getOption('no-enhance') - ]) + ...$backend['class']->backup( + $this->mapper, + $backend['fp'] ?? new SplFileObject('php://stdout', 'wb+'), + [ + 'no_enhance' => true === $input->getOption('no-enhance'), + Options::DRY_RUN => (bool)$input->getOption('dry-run'), + ] + ) ); } @@ -314,7 +313,9 @@ HELP continue; } - if (true === ($backend['fp'] instanceof SplFileObject)) { + assert($backend['fp'] instanceof SplFileObject); + + if (false === $input->getOption('dry-run')) { $backend['fp']->fseek(-1, SEEK_END); $backend['fp']->fwrite(PHP_EOL . ']'); }