[wip] Default repository settings

This commit is contained in:
ljf 2022-10-25 02:09:46 +02:00
parent c6d070d682
commit 74d34b5d28
No known key found for this signature in database
4 changed files with 131 additions and 77 deletions

View file

@ -1077,10 +1077,11 @@ backup:
action_help: Restore from a local backup archive. If neither --apps or --system are given, this will restore all apps and all system parts in the archive. If only --apps if given, this will only restore apps and no system parts. Similarly, if only --system is given, this will only restore system parts and no apps. action_help: Restore from a local backup archive. If neither --apps or --system are given, this will restore all apps and all system parts in the archive. If only --apps if given, this will only restore apps and no system parts. Similarly, if only --system is given, this will only restore system parts and no apps.
api: PUT /backups/<name>/restore api: PUT /backups/<name>/restore
arguments: arguments:
repository:
help: Repository of the backup archive
name: name:
help: Name of the local backup archive help: Name of the local backup archive
-r:
full: --repository
help: Repository in which we can found the archive
--system: --system:
help: List of system parts to restore (or all if none is given) help: List of system parts to restore (or all if none is given)
nargs: "*" nargs: "*"
@ -1096,9 +1097,6 @@ backup:
action_help: List available local backup archives or list files in an archive action_help: List available local backup archives or list files in an archive
api: GET /backups api: GET /backups
arguments: arguments:
repository:
help: Repository of a backup archive
nargs: "?"
name: name:
help: Name of a backup archive help: Name of a backup archive
nargs: "?" nargs: "?"
@ -1120,10 +1118,11 @@ backup:
action_help: Show info about a local backup archive action_help: Show info about a local backup archive
api: GET /backups/<name> api: GET /backups/<name>
arguments: arguments:
repository:
help: Repository of the backup archive
name: name:
help: Name of the backup archive help: Name of the backup archive
-r:
full: --repository
help: Repository in which we can found the archive
-d: -d:
full: --with-details full: --with-details
help: Show additional backup information help: Show additional backup information
@ -1139,35 +1138,38 @@ backup:
action_help: (API only) Request to download the file action_help: (API only) Request to download the file
api: GET /backups/<name>/download api: GET /backups/<name>/download
arguments: arguments:
repository:
help: Repository of the backup archive
name: name:
help: Name of the local backup archive help: Name of the local backup archive
-r:
full: --repository
help: Repository in which we can found the archive
### backup_mount() ### backup_mount()
mount: mount:
action_help: Mount a backup archive if possible action_help: Mount a backup archive if possible
api: DELETE /backups/<name> api: DELETE /backups/<name>
arguments: arguments:
repository:
help: Repository of the backup archive
name: name:
help: Name of the backup archive help: Name of the backup archive
path: path:
help: Path where mount the archive help: Path where mount the archive
-r:
full: --repository
help: Repository in which we can found the archive
### backup_delete() ### backup_delete()
delete: delete:
action_help: Delete a backup archive action_help: Delete a backup archive
api: DELETE /backups/<name> api: DELETE /backups/<name>
arguments: arguments:
repository: name:
help: Repository of the backup archive
archive_name:
help: Name of the archive to delete help: Name of the archive to delete
extra: extra:
pattern: *pattern_backup_archive_name pattern: *pattern_backup_archive_name
nargs: "*" nargs: "*"
-r:
full: --repository
help: Repository in which we can found the archive
subcategories: subcategories:
repository: repository:
@ -1247,8 +1249,8 @@ backup:
help: Name of the backup repository to update help: Name of the backup repository to update
extra: extra:
pattern: *pattern_backup_repository_shortname pattern: *pattern_backup_repository_shortname
-d: -n:
full: --description full: --name
help: Short description of the repository help: Short description of the repository
-q: -q:
full: --quota full: --quota
@ -1328,16 +1330,16 @@ backup:
### backup_timer_create() ### backup_timer_create()
create: create:
action_help: Add a backup timer action_help: Add a backup timer
api: POST /backup/timer/<name> api: POST /backup/timer/<shortname>
arguments: arguments:
name: shortname:
help: Short prefix of the backup archives help: Short prefix of the backup archives
extra: extra:
pattern: &pattern_backup_timer_name pattern: &pattern_backup_timer_name
- !!str ^[\w\-\._]{1,50}$ - !!str ^[\w\-\._]{1,35}$
- "pattern_backup_timer_name" - "pattern_backup_timer_name"
-d: -n:
full: --description full: --name
help: Short description of the backup help: Short description of the backup
-r: -r:
full: --repositories full: --repositories
@ -1353,26 +1355,43 @@ backup:
help: Regular backup frequency (see systemd OnCalendar format) help: Regular backup frequency (see systemd OnCalendar format)
--alert: --alert:
help: Email to alert help: Email to alert
--keep-hourly: -H:
default: 0 full: --keep-hourly
--keep-daily: help: Number of hourly archives to keep
default: 7 type: int
--keep-weekly: -d:
default: 8 full: --keep-daily
--keep-monthly: help: Number of daily archives to keep
default: 8 type: int
-w:
full: --keep-weekly
help: Number of weekly archives to keep
type: int
-m:
full: --keep-monthly
help: Number of monthly archives to keep
type: int
--keep-last:
help: Number of last archives to keep
type: int
--keep-within:
help: Keep all archives within this time interval
extra:
pattern: &pattern_interval
- !!str ^\d+[Hdw]$
- "pattern_interval"
### backup_timer_update() ### backup_timer_update()
update: update:
action_help: Update a backup timer action_help: Update a backup timer
api: PUT /backup/timer/<name> api: PUT /backup/timer/<shortname>
arguments: arguments:
name: shortname:
help: Short prefix of the backup archives help: Short prefix of the backup archives
extra: extra:
pattern: *pattern_backup_timer_name pattern: *pattern_backup_timer_name
-d: -n:
full: --description full: --name
help: Short description of the backup help: Short description of the backup
-r: -r:
full: --repositories full: --repositories
@ -1388,21 +1407,38 @@ backup:
help: Regular backup frequency (see systemd OnCalendar format) help: Regular backup frequency (see systemd OnCalendar format)
--alert: --alert:
help: Email to alert help: Email to alert
--keep-hourly: -H:
default: 2 full: --keep-hourly
--keep-daily: help: Number of hourly archives to keep
default: 7 type: int
--keep-weekly: -d:
default: 8 full: --keep-daily
--keep-monthly: help: Number of daily archives to keep
default: 12 type: int
-w:
full: --keep-weekly
help: Number of weekly archives to keep
type: int
-m:
full: --keep-monthly
help: Number of monthly archives to keep
type: int
--keep-last:
help: Number of last archives to keep
type: int
--keep-within:
help: Keep all archives within this time interval
extra:
pattern: &pattern_interval
- !!str ^\d+[Hdw]$
- "pattern_interval"
### backup_timer_remove() ### backup_timer_remove()
remove: remove:
action_help: Remove a backup timer action_help: Remove a backup timer
api: DELETE /backup/timer/<name> api: DELETE /backup/timer/<shortname>
arguments: arguments:
name: shortname:
help: Short prefix of the backup archives help: Short prefix of the backup archives
extra: extra:
pattern: *pattern_backup_timer_name pattern: *pattern_backup_timer_name
@ -1410,9 +1446,9 @@ backup:
### backup_timer_info() ### backup_timer_info()
info: info:
action_help: Get info about a backup timer action_help: Get info about a backup timer
api: GET /backup/timer/<name> api: GET /backup/timer/<shortname>
arguments: arguments:
name: shortname:
help: Short prefix of the backup archives help: Short prefix of the backup archives
extra: extra:
pattern: *pattern_backup_timer_name pattern: *pattern_backup_timer_name
@ -1420,9 +1456,9 @@ backup:
### backup_timer_start() ### backup_timer_start()
start: start:
action_help: Start a backup timer action_help: Start a backup timer
api: POST /backup/timer/<name>/start api: POST /backup/timer/<shortname>/start
arguments: arguments:
name: shortname:
help: Backup timer to start help: Backup timer to start
extra: extra:
pattern: *pattern_backup_timer_name pattern: *pattern_backup_timer_name
@ -1433,9 +1469,9 @@ backup:
### backup_timer_pause() ### backup_timer_pause()
pause: pause:
action_help: Pause a backup timer action_help: Pause a backup timer
api: POST /backup/timer/<name>/pause api: POST /backup/timer/<shortname>/pause
arguments: arguments:
name: shortname:
help: Backup timer to pause help: Backup timer to pause
extra: extra:
pattern: *pattern_backup_timer_name pattern: *pattern_backup_timer_name

View file

@ -147,6 +147,10 @@ name = "Other"
[misc.backup] [misc.backup]
name = "Backup" name = "Backup"
[misc.backup.backup_default_repositories]
type = "tags"
default = ["local-borg"]
[misc.backup.backup_compress_tar_archives] [misc.backup.backup_compress_tar_archives]
type = "boolean" type = "boolean"
default = false default = false

View file

@ -1677,7 +1677,7 @@ def backup_create(
# Add backup repositories # Add backup repositories
if not repositories: if not repositories:
repositories = ["local-borg"] repositories = settings_get("misc.backup.backup_default_repositories")
# Validate there is no archive with the same name # Validate there is no archive with the same name
archives = backup_list(repositories=repositories) archives = backup_list(repositories=repositories)
@ -1775,7 +1775,7 @@ def backup_create(
} }
def backup_restore(repository, name, system=[], apps=[], force=False): def backup_restore(name, repository=None, system=[], apps=[], force=False):
""" """
Restore from a local backup archive Restore from a local backup archive
@ -1797,7 +1797,7 @@ def backup_restore(repository, name, system=[], apps=[], force=False):
apps = [] apps = []
if not repository: if not repository:
repository = "local-borg" repository = settings_get("misc.backup.backup_default_repositories")[0]
# #
# Initialize # # Initialize #
@ -1860,7 +1860,7 @@ def backup_restore(repository, name, system=[], apps=[], force=False):
return restore_manager.targets.results return restore_manager.targets.results
def backup_list(repository=None, name=None, repositories=[], with_info=False, human_readable=False): def backup_list(name=None, repositories=[], with_info=False, human_readable=False):
""" """
List available local backup archives List available local backup archives
@ -1870,10 +1870,11 @@ def backup_list(repository=None, name=None, repositories=[], with_info=False, hu
human_readable -- Print sizes in human readable format human_readable -- Print sizes in human readable format
""" """
if bool(repository) != bool(name): if not repositories:
raise YunohostError("backup_list_bad_arguments") repositories = settings_get("misc.backup.backup_default_repositories")[0]
elif repository:
repo = BackupRepository(repository) if name:
repo = BackupRepository(repositories[0])
archive = BackupArchive(repo, name) archive = BackupArchive(repo, name)
return archive.list(with_info) return archive.list(with_info)
@ -1884,23 +1885,27 @@ def backup_list(repository=None, name=None, repositories=[], with_info=False, hu
} }
def backup_download(repository, name): def backup_download(name, repository=None):
if not repository:
repository = settings_get("misc.backup.backup_default_repositories")[0]
repo = BackupRepository(repository) repo = BackupRepository(repository)
archive = BackupArchive(repo, name) archive = BackupArchive(repo, name)
return archive.download() return archive.download()
def backup_mount(repository, name, path): def backup_mount(name, path, repository=None):
if not repository:
repository = settings_get("misc.backup.backup_default_repositories")[0]
repo = BackupRepository(repository) repo = BackupRepository(repository)
archive = BackupArchive(repo, name) archive = BackupArchive(repo, name)
archive.mount(path) archive.mount(path)
def backup_info(repository, name, with_details=False, human_readable=False): def backup_info(name, repository=None, with_details=False, human_readable=False):
""" """
Get info about a local backup archive Get info about a local backup archive
@ -1910,13 +1915,15 @@ def backup_info(repository, name, with_details=False, human_readable=False):
human_readable -- Print sizes in human readable format human_readable -- Print sizes in human readable format
""" """
if not repository:
repository = settings_get("misc.backup.backup_default_repositories")[0]
repo = BackupRepository(repository) repo = BackupRepository(repository)
archive = BackupArchive(repo, name) archive = BackupArchive(repo, name)
return archive.info(with_details=with_details, human_readable=human_readable) return archive.info(with_details=with_details, human_readable=human_readable)
def backup_delete(repository, archive_name): def backup_delete(name, repository=None):
""" """
Delete a backup Delete a backup
@ -1924,9 +1931,11 @@ def backup_delete(repository, archive_name):
name -- Name of the local backup archive name -- Name of the local backup archive
""" """
for name in archive_name: if not repository:
repository = settings_get("misc.backup.backup_default_repositories")[0]
for _name in name:
repo = BackupRepository(repository) repo = BackupRepository(repository)
BackupArchive(repo, name).delete() BackupArchive(repo, _name).delete()
logger.success(m18n.n("backup_deleted")) logger.success(m18n.n("backup_deleted"))
@ -2129,7 +2138,7 @@ Group=root
self._load_current_values() self._load_current_values()
backup_create( backup_create(
prefix=f"{self.entity}_", prefix=f"{self.entity}_",
description=self.description, description=self.name,
repositories=self.repositories, repositories=self.repositories,
system=self.system, system=self.system,
apps=self.apps apps=self.apps
@ -2152,15 +2161,15 @@ def backup_timer_list(full=False):
return {"backup_timer": BackupTimer.list(full)} return {"backup_timer": BackupTimer.list(full)}
def backup_timer_info(name): def backup_timer_info(shortname):
return BackupTimer(name).info() return BackupTimer(shortname).info()
@is_unit_operation() @is_unit_operation()
def backup_timer_create( def backup_timer_create(
operation_logger, operation_logger,
shortname=None,
name=None, name=None,
description=None,
repositories=[], repositories=[],
system=[], system=[],
apps=[], apps=[],
@ -2175,8 +2184,10 @@ def backup_timer_create(
""" """
Add a backup timer Add a backup timer
""" """
if not repositories:
repositories = settings_get("misc.backup.backup_default_repositories")
args = {k: v for k, v in locals().items() if v is not None} args = {k: v for k, v in locals().items() if v is not None}
timer = BackupTimer(name, creation=True) timer = BackupTimer(shortname, creation=True)
return timer.set( return timer.set(
operation_logger=args.pop('operation_logger'), operation_logger=args.pop('operation_logger'),
args=urllib.parse.urlencode(args, doseq=True) args=urllib.parse.urlencode(args, doseq=True)
@ -2191,33 +2202,34 @@ def backup_timer_update(operation_logger, shortname, name=None,
Update a backup timer Update a backup timer
""" """
backup_timer_add(creation=False, **locals()) backup_timer_create(creation=False, **locals())
@is_unit_operation() @is_unit_operation()
def backup_timer_remove(operation_logger, name): def backup_timer_remove(operation_logger, shortname):
""" """
Remove a backup timer Remove a backup timer
""" """
BackupTimer(name).remove() BackupTimer(shortname).remove()
@is_unit_operation() @is_unit_operation()
def backup_timer_start(operation_logger, name, now=False): def backup_timer_start(operation_logger, shortname, now=False):
""" """
Start a backup timer Start a backup timer
""" """
if now: if now:
BackupTimer(name).run() BackupTimer(shortname).run()
BackupTimer(shortname).start()
BackupTimer(name).start()
@is_unit_operation() @is_unit_operation()
def backup_timer_pause(operation_logger, name): def backup_timer_pause(operation_logger, shortname):
""" """
Pause a backup timer Pause a backup timer
""" """
BackupTimer(name).stop() BackupTimer(shortname).stop()
# #

View file

@ -40,6 +40,7 @@ from moulinette.utils.network import download_text
from datetime import timedelta, datetime from datetime import timedelta, datetime
import yunohost.repositories import yunohost.repositories
from yunohost.settings import settings_get
from yunohost.utils.config import ConfigPanel from yunohost.utils.config import ConfigPanel
from yunohost.utils.error import YunohostError, YunohostValidationError from yunohost.utils.error import YunohostError, YunohostValidationError
from yunohost.utils.system import disk_usage, binary_to_human from yunohost.utils.system import disk_usage, binary_to_human
@ -114,7 +115,8 @@ class BackupRepository(ConfigPanel):
return full_repositories return full_repositories
def __init__(self, entity, config_path=None, save_path=None, creation=False): def __init__(self, entity, config_path=None, save_path=None, creation=False):
if self.entity is None:
self.entity = settings_get("misc.backup.backup_default_repositories")[0]
super().__init__(entity, config_path, save_path, creation) super().__init__(entity, config_path, save_path, creation)
self._load_current_values() self._load_current_values()