aboutsummaryrefslogtreecommitdiff
path: root/cli/db-restore.php
blob: 8e4ffcc12457db57f774c49fae31faf1c8898438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env php
<?php
declare(strict_types=1);
require(__DIR__ . '/_cli.php');

performRequirementCheck(FreshRSS_Context::systemConf()->db['type'] ?? '');

$cliOptions = new class extends CliOptionsParser {
	public bool $deleteBackup;
	public bool $forceOverwrite;

	public function __construct() {
		$this->addOption('deleteBackup', (new CliOption('delete-backup'))->withValueNone());
		$this->addOption('forceOverwrite', (new CliOption('force-overwrite'))->withValueNone());
		parent::__construct();
	}
};

if (!empty($cliOptions->errors)) {
	fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
}

FreshRSS_Context::initSystem(true);
Minz_User::change(Minz_User::INTERNAL_USER);
$ok = false;
try {
	$error = initDb();
	if ($error != '') {
		$_SESSION['bd_error'] = $error;
	} else {
		$ok = true;
	}
} catch (Exception $ex) {
	$_SESSION['bd_error'] = $ex->getMessage();
}
if (!$ok) {
	fail('FreshRSS database error: ' . (is_string($_SESSION['bd_error'] ?? null) ? $_SESSION['bd_error'] : 'Unknown error'));
}

foreach (listUsers() as $username) {
	$username = cliInitUser($username);
	$filename = DATA_PATH . "/users/{$username}/backup.sqlite";
	if (!file_exists($filename)) {
		fwrite(STDERR, "FreshRSS SQLite backup not found for user “{$username}”!\n");
		$ok = false;
		continue;
	}

	echo 'FreshRSS restore database from SQLite for user “', $username, "”…\n";

	$databaseDAO = FreshRSS_Factory::createDatabaseDAO($username);
	$ok &= $databaseDAO->dbCopy($filename, FreshRSS_DatabaseDAO::SQLITE_IMPORT, clearFirst: $cliOptions->forceOverwrite);
	if ($ok) {
		if ($cliOptions->deleteBackup) {
			unlink($filename);
		}
	} else {
		fwrite(STDERR, "FreshRSS database already exists for user “{$username}”!\n");
		fwrite(STDERR, "If you would like to clear the user database first, use the option --force-overwrite\n");
	}
	invalidateHttpCache($username);
}

done((bool)$ok);