Skip to content

Commit

Permalink
merge: added apy backup
Browse files Browse the repository at this point in the history
refer: #102, #103
  • Loading branch information
lervag committed Nov 1, 2024
2 parents 3be3ecd + 5d3d936 commit 4e69e9d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/apyanki/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from apyanki.config import cfg, cfg_file
from apyanki.console import console
from apyanki.note import Note
from apyanki.utilities import suppress_stdout

CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]}

Expand Down Expand Up @@ -529,6 +530,38 @@ def reposition(position: int, query: str) -> None:
a.modified = True


@main.command()
@click.argument(
"target-file", type=click.Path(exists=False, resolve_path=True, path_type=Path)
)
@click.option(
"-m", "--include-media", is_flag=True, help="Include media files in backup."
)
@click.option(
"-l",
"--legacy",
is_flag=True,
help="Support older Anki versions (slower/larger files)",
)
def backup(target_file: Path, include_media: bool, legacy: bool) -> None:
"""Backup Anki database to specified target file."""
with Anki(**cfg) as a:
target_filename = str(target_file)

if not target_filename.endswith(".colpkg"):
console.print("[yellow]Warning: Target should have .colpkg extension!")
raise click.Abort()

if target_file.exists():
console.print("[yellow]Warning: Target file already exists!")
console.print(f"[yellow] {target_file}")
if not console.confirm("Do you want to overwrite it?"):
raise click.Abort()

with suppress_stdout():
a.col.export_collection_package(target_filename, include_media, legacy)


if __name__ == "__main__":
# pylint: disable=no-value-for-parameter
main()

0 comments on commit 4e69e9d

Please sign in to comment.