setName('backup:run')
->setDescription('Back up this Nextcloud (database, config, data) to a location now')
->addArgument('target', InputArgument::OPTIONAL, 'Location name or id (default: all locations)')
->addOption('label', 'l', InputOption::VALUE_REQUIRED, 'Label for the snapshot', '')
->addOption('background', 'b', InputOption::VALUE_NONE, 'Only start the run; background jobs finish it')
->setHelp(<<<'HELP'
Starts a backup (or continues one that is already running for that location) and runs it to
the end in this process. Unchanged files are not read again; only new data is uploaded.
Examples:
occ backup:run back up to every location
occ backup:run offsite -l "before upgrade"
occ backup:run offsite --background
HELP);
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$name = $input->getArgument('target');
try {
$targets = $name === null ? $this->targets->list() : [$this->targets->get($name)];
} catch (\Throwable) {
$output->writeln('No such location.');
return 1;
}
if ($targets === []) {
$output->writeln('No backup locations yet (occ backup:target:add).');
return 1;
}
$failed = 0;
foreach ($targets as $t) {
try {
$run = $this->backups->start($t, (string)$input->getOption('label'));
} catch (\Throwable $e) {
$output->writeln('' . $t->getName() . ': ' . $e->getMessage() . '');
$failed++;
continue;
}
$output->writeln(sprintf('%s: run %d, snapshot %s', $t->getName(), $run->getId(), $run->getSnapshotId()));
if ($input->getOption('background')) {
continue;
}
$start = microtime(true);
$run = $this->backups->runToCompletion($run, function (Run $r) use ($output, $start) {
$s = json_decode($r->getStats() ?? '{}', true);
$f = $s['files'] ?? [];
$output->writeln(sprintf(' %5.0fs %-6s files %d (read %d, unchanged %d), uploaded %.1f MiB',
microtime(true) - $start, $r->getPhase(), $f['files'] ?? 0, $f['read'] ?? 0, $f['reused'] ?? 0, ($f['uploaded'] ?? 0) / 1048576));
});
if ($run->getStatus() === Run::DONE) {
$output->writeln(sprintf(' done in %.0fs', microtime(true) - $start));
$f = json_decode($run->getStats() ?? '{}', true)['files'] ?? [];
if (($f['unreadable'] ?? 0) > 0) {
$output->writeln(sprintf(' warning: %d unreadable files or folders are missing from this backup (see the server log)', $f['unreadable']));
}
if (($f['badNames'] ?? 0) > 0) {
$output->writeln(sprintf(' warning: %d files or folders are missing from this backup because their name is not supported; rename them and the next backup includes them (see the server log)', $f['badNames']));
}
if (($f['changing'] ?? 0) > 0) {
$output->writeln(sprintf(' warning: %d files kept changing and were saved as read (see the server log)', $f['changing']));
}
} else {
$output->writeln(' ' . $run->getStatus() . ': ' . $run->getError() . '');
$failed++;
}
}
return $failed === 0 ? 0 : 1;
}
}