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

View file

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

View file

@ -1677,7 +1677,7 @@ def backup_create(
# Add backup repositories
if not repositories:
repositories = ["local-borg"]
repositories = settings_get("misc.backup.backup_default_repositories")
# Validate there is no archive with the same name
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
@ -1797,7 +1797,7 @@ def backup_restore(repository, name, system=[], apps=[], force=False):
apps = []
if not repository:
repository = "local-borg"
repository = settings_get("misc.backup.backup_default_repositories")[0]
#
# Initialize #
@ -1860,7 +1860,7 @@ def backup_restore(repository, name, system=[], apps=[], force=False):
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
@ -1870,10 +1870,11 @@ def backup_list(repository=None, name=None, repositories=[], with_info=False, hu
human_readable -- Print sizes in human readable format
"""
if bool(repository) != bool(name):
raise YunohostError("backup_list_bad_arguments")
elif repository:
repo = BackupRepository(repository)
if not repositories:
repositories = settings_get("misc.backup.backup_default_repositories")[0]
if name:
repo = BackupRepository(repositories[0])
archive = BackupArchive(repo, name)
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)
archive = BackupArchive(repo, name)
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)
archive = BackupArchive(repo, name)
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
@ -1910,13 +1915,15 @@ def backup_info(repository, name, with_details=False, human_readable=False):
human_readable -- Print sizes in human readable format
"""
if not repository:
repository = settings_get("misc.backup.backup_default_repositories")[0]
repo = BackupRepository(repository)
archive = BackupArchive(repo, name)
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
@ -1924,9 +1931,11 @@ def backup_delete(repository, archive_name):
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)
BackupArchive(repo, name).delete()
BackupArchive(repo, _name).delete()
logger.success(m18n.n("backup_deleted"))
@ -2129,7 +2138,7 @@ Group=root
self._load_current_values()
backup_create(
prefix=f"{self.entity}_",
description=self.description,
description=self.name,
repositories=self.repositories,
system=self.system,
apps=self.apps
@ -2152,15 +2161,15 @@ def backup_timer_list(full=False):
return {"backup_timer": BackupTimer.list(full)}
def backup_timer_info(name):
return BackupTimer(name).info()
def backup_timer_info(shortname):
return BackupTimer(shortname).info()
@is_unit_operation()
def backup_timer_create(
operation_logger,
shortname=None,
name=None,
description=None,
repositories=[],
system=[],
apps=[],
@ -2175,8 +2184,10 @@ def backup_timer_create(
"""
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}
timer = BackupTimer(name, creation=True)
timer = BackupTimer(shortname, creation=True)
return timer.set(
operation_logger=args.pop('operation_logger'),
args=urllib.parse.urlencode(args, doseq=True)
@ -2191,33 +2202,34 @@ def backup_timer_update(operation_logger, shortname, name=None,
Update a backup timer
"""
backup_timer_add(creation=False, **locals())
backup_timer_create(creation=False, **locals())
@is_unit_operation()
def backup_timer_remove(operation_logger, name):
def backup_timer_remove(operation_logger, shortname):
"""
Remove a backup timer
"""
BackupTimer(name).remove()
BackupTimer(shortname).remove()
@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
"""
if now:
BackupTimer(name).run()
BackupTimer(shortname).run()
BackupTimer(shortname).start()
BackupTimer(name).start()
@is_unit_operation()
def backup_timer_pause(operation_logger, name):
def backup_timer_pause(operation_logger, shortname):
"""
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
import yunohost.repositories
from yunohost.settings import settings_get
from yunohost.utils.config import ConfigPanel
from yunohost.utils.error import YunohostError, YunohostValidationError
from yunohost.utils.system import disk_usage, binary_to_human
@ -114,7 +115,8 @@ class BackupRepository(ConfigPanel):
return full_repositories
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)
self._load_current_values()