Add remote borg backup support (#4804)

Signed-off-by: Tim Diels <tim@diels.me>
Signed-off-by: Simon L. <szaimen@e.mail.de>
Co-authored-by: Simon L. <szaimen@e.mail.de>
This commit is contained in:
Tim Diels 2024-11-07 22:19:56 +01:00 committed by GitHub
parent 34a264d945
commit 3eeda1ea91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 402 additions and 159 deletions

View file

@ -473,6 +473,7 @@
"image": "nextcloud/aio-borgbackup",
"init": true,
"environment": [
"BORG_REMOTE_REPO=%BORGBACKUP_REMOTE_REPO%",
"BORG_PASSWORD=%BORGBACKUP_PASSWORD%",
"BORG_MODE=%BORGBACKUP_MODE%",
"SELECTED_RESTORE_TIME=%SELECTED_RESTORE_TIME%",

View file

@ -86,6 +86,8 @@ $app->get('/containers', function (Request $request, Response $response, array $
'domain' => $configurationManager->GetDomain(),
'apache_port' => $configurationManager->GetApachePort(),
'borg_backup_host_location' => $configurationManager->GetBorgBackupHostLocation(),
'borg_remote_repo' => $configurationManager->GetBorgRemoteRepo(),
'borg_public_key' => $configurationManager->GetBorgPublicKey(),
'nextcloud_password' => $configurationManager->GetAndGenerateSecret('NEXTCLOUD_PASSWORD'),
'containers' => (new \AIO\ContainerDefinitionFetcher($container->get(\AIO\Data\ConfigurationManager::class), $container))->FetchDefinition(),
'borgbackup_password' => $configurationManager->GetAndGenerateSecret('BORGBACKUP_PASSWORD'),

View file

@ -28,15 +28,17 @@ readonly class ConfigurationController {
$this->configurationManager->ChangeMasterPassword($currentMasterPassword, $newMasterPassword);
}
if (isset($request->getParsedBody()['borg_backup_host_location'])) {
if (isset($request->getParsedBody()['borg_backup_host_location']) || isset($request->getParsedBody()['borg_remote_repo'])) {
$location = $request->getParsedBody()['borg_backup_host_location'] ?? '';
$this->configurationManager->SetBorgBackupHostLocation($location);
$borgRemoteRepo = $request->getParsedBody()['borg_remote_repo'] ?? '';
$this->configurationManager->SetBorgLocationVars($location, $borgRemoteRepo);
}
if (isset($request->getParsedBody()['borg_restore_host_location']) || isset($request->getParsedBody()['borg_restore_password'])) {
if (isset($request->getParsedBody()['borg_restore_host_location']) || isset($request->getParsedBody()['borg_restore_remote_repo']) || isset($request->getParsedBody()['borg_restore_password'])) {
$restoreLocation = $request->getParsedBody()['borg_restore_host_location'] ?? '';
$borgRemoteRepo = $request->getParsedBody()['borg_restore_remote_repo'] ?? '';
$borgPassword = $request->getParsedBody()['borg_restore_password'] ?? '';
$this->configurationManager->SetBorgRestoreHostLocationAndPassword($restoreLocation, $borgPassword);
$this->configurationManager->SetBorgRestoreLocationVarsAndPassword($restoreLocation, $borgRemoteRepo, $borgPassword);
}
if (isset($request->getParsedBody()['daily_backup_time'])) {
@ -132,8 +134,8 @@ readonly class ConfigurationController {
$this->configurationManager->SetCollaboraDictionaries($collaboraDictionaries);
}
if (isset($request->getParsedBody()['delete_borg_backup_host_location'])) {
$this->configurationManager->DeleteBorgBackupHostLocation();
if (isset($request->getParsedBody()['delete_borg_backup_location_vars'])) {
$this->configurationManager->DeleteBorgBackupLocationVars();
}
return $response->withStatus(201)->withHeader('Location', '/');

View file

@ -439,48 +439,61 @@ class ConfigurationManager
/**
* @throws InvalidSettingConfigurationException
*/
public function SetBorgBackupHostLocation(string $location) : void {
$isValidPath = false;
if (str_starts_with($location, '/') && !str_ends_with($location, '/')) {
$isValidPath = true;
} elseif ($location === 'nextcloud_aio_backupdir') {
$isValidPath = true;
}
if (!$isValidPath) {
throw new InvalidSettingConfigurationException("The path must start with '/', and must not end with '/'!");
}
public function SetBorgLocationVars(string $location, string $repo) : void {
$this->ValidateBorgLocationVars($location, $repo);
$config = $this->GetConfig();
$config['borg_backup_host_location'] = $location;
$config['borg_remote_repo'] = $repo;
$this->WriteConfig($config);
}
public function DeleteBorgBackupHostLocation() : void {
$config = $this->GetConfig();
$config['borg_backup_host_location'] = '';
$this->WriteConfig($config);
}
/**
* @throws InvalidSettingConfigurationException
*/
public function SetBorgRestoreHostLocationAndPassword(string $location, string $password) : void {
if ($location === '') {
throw new InvalidSettingConfigurationException("Please enter a path!");
private function ValidateBorgLocationVars(string $location, string $repo) : void {
if ($location === '' && $repo === '') {
throw new InvalidSettingConfigurationException("Please enter a path or a remote repo url!");
} elseif ($location !== '' && $repo !== '') {
throw new InvalidSettingConfigurationException("Location and remote repo url are mutually exclusive!");
}
$isValidPath = false;
if (str_starts_with($location, '/') && !str_ends_with($location, '/')) {
$isValidPath = true;
} elseif ($location === 'nextcloud_aio_backupdir') {
$isValidPath = true;
}
if ($location !== '') {
$isValidPath = false;
if (str_starts_with($location, '/') && !str_ends_with($location, '/')) {
$isValidPath = true;
} elseif ($location === 'nextcloud_aio_backupdir') {
$isValidPath = true;
}
if (!$isValidPath) {
throw new InvalidSettingConfigurationException("The path must start with '/', and must not end with '/'!");
if (!$isValidPath) {
throw new InvalidSettingConfigurationException("The path must start with '/', and must not end with '/'!");
}
} else {
$this->ValidateBorgRemoteRepo($repo);
}
}
private function ValidateBorgRemoteRepo(string $repo) : void {
$commonMsg = "For valid urls, see the remote examples at https://borgbackup.readthedocs.io/en/stable/usage/general.html#repository-urls";
if ($repo === "") {
// Ok, remote repo is optional
} elseif (!str_contains($repo, "@")) {
throw new InvalidSettingConfigurationException("The remote repo must contain '@'. $commonMsg");
} elseif (!str_contains($repo, ":")) {
throw new InvalidSettingConfigurationException("The remote repo must contain ':'. $commonMsg");
}
}
public function DeleteBorgBackupLocationVars() : void {
$config = $this->GetConfig();
$config['borg_backup_host_location'] = '';
$config['borg_remote_repo'] = '';
$this->WriteConfig($config);
}
/**
* @throws InvalidSettingConfigurationException
*/
public function SetBorgRestoreLocationVarsAndPassword(string $location, string $repo, string $password) : void {
$this->ValidateBorgLocationVars($location, $repo);
if ($password === '') {
throw new InvalidSettingConfigurationException("Please enter the password!");
@ -488,6 +501,7 @@ class ConfigurationManager
$config = $this->GetConfig();
$config['borg_backup_host_location'] = $location;
$config['borg_remote_repo'] = $repo;
$config['borg_restore_password'] = $password;
$config['instance_restore_attempt'] = 1;
$this->WriteConfig($config);
@ -582,6 +596,23 @@ class ConfigurationManager
return $config['borg_backup_host_location'];
}
public function GetBorgRemoteRepo() : string {
$config = $this->GetConfig();
if(!isset($config['borg_remote_repo'])) {
$config['borg_remote_repo'] = '';
}
return $config['borg_remote_repo'];
}
public function GetBorgPublicKey() : string {
if (!file_exists(DataConst::GetBackupPublicKey())) {
return "";
}
return trim(file_get_contents(DataConst::GetBackupPublicKey()));
}
public function GetBorgRestorePassword() : string {
$config = $this->GetConfig();
if(!isset($config['borg_restore_password'])) {

View file

@ -23,6 +23,10 @@ class DataConst {
return self::GetDataDirectory() . '/configuration.json';
}
public static function GetBackupPublicKey() : string {
return self::GetDataDirectory() . '/id_borg.pub';
}
public static function GetBackupSecretFile() : string {
return self::GetDataDirectory() . '/backupsecret';
}

View file

@ -265,6 +265,8 @@ readonly class DockerActionManager {
$replacements[1] = $this->configurationManager->GetBaseDN();
} elseif ($out[1] === 'AIO_TOKEN') {
$replacements[1] = $this->configurationManager->GetToken();
} elseif ($out[1] === 'BORGBACKUP_REMOTE_REPO') {
$replacements[1] = $this->configurationManager->GetBorgRemoteRepo();
} elseif ($out[1] === 'BORGBACKUP_MODE') {
$replacements[1] = $this->configurationManager->GetBackupMode();
} elseif ($out[1] === 'AIO_URL') {

View file

@ -25,6 +25,7 @@
{# timezone-prefill #}
<script type="text/javascript" src="timezone.js"></script>
{% set hasBackupLocation = borg_backup_host_location or borg_remote_repo %}
{% set isAnyRunning = false %}
{% set isAnyRestarting = false %}
{% set isWatchtowerRunning = false %}
@ -90,7 +91,7 @@
<input type="submit" value="Update mastercontainer" />
</form>
{% else %}
{% if borg_backup_host_location == '' and borg_restore_password == '' %}
{% if not hasBackupLocation %}
<p>The official Nextcloud installation method. Nextcloud All-in-One provides easy deployment and maintenance with most features included in this one Nextcloud instance.</p>
<p>You can either create a new AIO instance or restore a former AIO instance from backup. See the two sections below.</p>
{{ include('includes/aio-config.twig') }}
@ -130,7 +131,7 @@
{% endif %}
{% if is_instance_restore_attempt == false %}
{% if borg_backup_host_location != '' and borg_restore_password != '' %}
{% if hasBackupLocation %}
{% if borg_backup_mode in ['test', 'check'] %}
{% if backup_exit_code > 0 %}
<p><span class="status error"></span> Last {{ borg_backup_mode }} failed! (<a href="/api/docker/logs?id=nextcloud-aio-borgbackup" target="_blank" rel="noopener">Logs</a>)</p>
@ -179,11 +180,23 @@
{% endif %}
{% endif %}
{% if borg_backup_host_location == '' or borg_restore_password == '' or borg_backup_mode not in ['test', 'check', ''] or backup_exit_code > 0 %}
<p>Please enter the location of the backup archive on your host and the encryption password of the backup archive below:</p>
{% if not hasBackupLocation or borg_backup_mode not in ['test', 'check', ''] or backup_exit_code > 0 %}
{% if borg_remote_repo and backup_exit_code > 0 %}
<p>
You may still need to authorize this pubkey on your borg remote:<br><strong>{{ borg_public_key }}</strong><br>
To try again, resubmit your location and rerun the test.
</p>
{% endif %}
<p>
Please enter the location of the backup archive on your host or a
<a href="https://borgbackup.readthedocs.io/en/stable/usage/general.html#repository-urls">remote borg repo url</a>
if stored remotely; and the encryption password of the backup archive below:
</p>
<form method="POST" action="/api/configuration" class="xhr">
<input type="text" name="borg_restore_host_location" value="{{borg_backup_host_location}}" placeholder="/mnt/backup"/>
<input type="text" name="borg_restore_password" value="{{borg_restore_password}}" placeholder="encryption password"/>
<label>Local backup location</label> <input type="text" name="borg_restore_host_location" value="{{borg_backup_host_location}}" placeholder="/mnt/backup"/><br>
<label>Remote borg repo</label> <input type="text" name="borg_restore_remote_repo" value="{{borg_remote_repo}}" placeholder="user@host:/path/to/repo"/><br>
<label>Borg passphrase</label> <input type="text" name="borg_restore_password" value="{{borg_restore_password}}" placeholder="encryption password"/><br>
<input type="hidden" name="{{csrf.keys.name}}" value="{{csrf.name}}">
<input type="hidden" name="{{csrf.keys.value}}" value="{{csrf.value}}">
<input type="submit" value="Submit location and encryption password" />
@ -220,19 +233,19 @@
{% if domain != "" %}
{% if isAnyRunning == true %}
{% if isApacheStarting != true %}
{% if borg_backup_host_location != '' %}
{% if hasBackupLocation %}
<details>
<summary>Click here to reveal the initial Nextcloud credentials</summary>
{% endif %}
<p>Initial Nextcloud username: <strong>admin</strong></p>
{% if borg_backup_host_location != '' %}
{% if hasBackupLocation %}
{# nextcloud_password needs to be duplicated due to a bug in Firefox. See https://github.com/nextcloud/all-in-one/issues/638. #}
<p>Initial Nextcloud password: <strong>{{ nextcloud_password }}</strong></p></details>
{% else %}
<p>Initial Nextcloud password: <strong>{{ nextcloud_password }}</strong></p>
{% endif %}
<p><a href="https://{{ domain }}" class="button" target="_blank" rel="noopener">Open your Nextcloud ↗</a></p>
{% if borg_backup_host_location == '' %}
{% if not hasBackupLocation %}
<p>If your Nextcloud does not open when clicking the button above, see <strong><a href="https://github.com/nextcloud/all-in-one/discussions/2105">this documentation</a></strong></p>
{% endif %}
{% else %}
@ -371,11 +384,16 @@
<h2>Backup and restore</h2>
<p>The backup section is disabled via environmental variable.</p>
{% else %}
{% if is_backup_container_running == false and borg_backup_host_location == "" and isApacheStarting != true %}
{% if is_backup_container_running == false and not hasBackupLocation and isApacheStarting != true %}
<h2>Backup and restore</h2>
<p>Please enter the directory path below where backups will be created on the host system. It's best to choose a location on a separate drive and not on your root drive.</p>
<p>
To store backups remotely instead, fill in the
<a href="https://borgbackup.readthedocs.io/en/stable/usage/general.html#repository-urls">remote borg repo url</a>.
</p>
<form method="POST" action="/api/configuration" class="xhr">
<input type="text" name="borg_backup_host_location" placeholder="/mnt/backup"/>
<label>Local backup location</label> <input type="text" name="borg_backup_host_location" placeholder="/mnt/backup"/><br>
<label>Remote borg repo</label> <input type="text" name="borg_remote_repo" placeholder="user@host:/path/to/repo"/><br>
<input type="hidden" name="{{csrf.keys.name}}" value="{{csrf.name}}">
<input type="hidden" name="{{csrf.keys.value}}" value="{{csrf.value}}">
<input type="submit" value="Submit backup location" />
@ -386,7 +404,7 @@
{% if is_backup_section_enabled == true %}
{% if borg_backup_host_location != "" %}
{% if hasBackupLocation %}
{% if is_backup_container_running == false %}
<h2>Backup and restore</h2>
{% if backup_exit_code > 0 %}
@ -404,9 +422,19 @@
</details>
{% endif %}
{% if has_backup_run_once == false %}
<p>The initial backup was not successful.</p>
{% if borg_remote_repo %}
<p>
You may still need to authorize this pubkey on your borg remote:<br><strong>{{ borg_public_key }}</strong><br>
To try again, click <strong>Create backup</strong>.
</p>
{% endif %}
<p>You may change the backup path again since the initial backup was not successful. After submitting the new value, you need to click on <strong>Create Backup</strong> to test the new value.</p>
<form method="POST" action="/api/configuration" class="xhr">
<input type="text" value="{{borg_backup_host_location}}" name="borg_backup_host_location" placeholder="/mnt/backup" />
<label>Local backup location</label> <input type="text" name="borg_backup_host_location" placeholder="/mnt/backup"/><br>
<label>Remote borg repo</label> <input type="text" name="borg_remote_repo" placeholder="user@host:/path/to/repo"/><br>
<input type="hidden" name="{{csrf.keys.name}}" value="{{csrf.name}}">
<input type="hidden" name="{{csrf.keys.value}}" value="{{csrf.value}}">
<input type="submit" value="Set backup location again" />
@ -432,7 +460,17 @@
<p>All important data from your Nextcloud AIO instance such as the database, your files and the mastercontainer's configuration files, will be backed up.</p>
<p>The backup uses a tool called <a href="https://github.com/borgbackup/borg#what-is-borgbackup"><strong>BorgBackup</strong></a>, a well-known server backup tool that efficiently backs up your files and encrypts them on the fly.</p>
<p>By using this tool, backups are incremental, differential, compressed and encrypted so only the first backup will take a while. Further backups should be fast as only changes are taken into account.</p>
<p>Backups will be created in the following directory on the host: <strong>{{ borg_backup_host_location }}/borg</strong></p>
{% if borg_remote_repo != '' %}
<p>
Backups get created remotely at:<br>
<strong>{{ borg_remote_repo }}</strong>
{% if has_backup_run_once == true %}
<br/>Your borg ssh public key is:<br><strong>{{ borg_public_key }}</strong>
{% endif %}
</p>
{% else %}
<p>Backups will be created in the following directory on the host: <strong>{{ borg_backup_host_location }}/borg</strong></p>
{% endif %}
<p>Be aware that this solution does not backup files and folders that are mounted into Nextcloud using the external storage app, but you can add further Docker volumes and host paths that you want to back up after the initial backup is done.</p>
<p>For information about backup retention, see <strong><a href="https://github.com/nextcloud/all-in-one#how-to-adjust-borgs-retention-policy">this</a></strong>.</p>
<p>Daily backups can be enabled after the initial backup is done. Enabling this also allows you to enable an option to update all containers, Nextcloud, and its apps automatically.</p>
@ -448,10 +486,14 @@
</form>
{% if has_backup_run_once == false %}
<h3>Reset backup host location</h3>
<p>If the configured backup host location <strong>{{ borg_backup_host_location }}</strong> is wrong, you can reset it by clicking on the button below.</p>
<h3>Reset backup location</h3>
<p>
If the configured backup host location <strong>{{ borg_backup_host_location }}</strong>
{% if borg_remote_repo %}or the remote repo <strong>{{ borg_remote_repo }}</strong> is wrong{% endif %},
you can reset it by clicking on the button below.
</p>
<form method="POST" action="/api/configuration" class="xhr">
<input type="hidden" name="delete_borg_backup_host_location" value="yes"/>
<input type="hidden" name="delete_borg_backup_location_vars" value="yes"/>
<input type="hidden" name="{{csrf.keys.name}}" value="{{csrf.name}}">
<input type="hidden" name="{{csrf.keys.value}}" value="{{csrf.value}}">
<input type="submit" value="Reset backup location" />