From d8609ca09efc73bf402cfa78d9d0e31f59890302 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 13 Jul 2024 23:21:22 +0200 Subject: [PATCH] started writing a small CLI tool; opening accounts dir still fails --- backup_dc_from_db_cli/cli.py | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 backup_dc_from_db_cli/cli.py diff --git a/backup_dc_from_db_cli/cli.py b/backup_dc_from_db_cli/cli.py new file mode 100644 index 0000000..ae28316 --- /dev/null +++ b/backup_dc_from_db_cli/cli.py @@ -0,0 +1,63 @@ +import argparse +import os +import logging +import datetime +import sys + +from deltachat_rpc_client import DeltaChat, Rpc + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + "--db_path", type=str, default=os.getenv("HOME", ".") + "/.config/DeltaChat/accounts", + help="Path to DeltaChat directory", + ) + parser.add_argument("--account", type=str, help="email address of the account to backup") + parser.add_argument("--output", type=str, help="output file name") + parser.add_argument( + "--passphrase", type=str, default=os.environ.get("DC_BACKUP_PASSPHRASE", ""), + help="passphrase to encrypt the backup file. environment variable: DC_BACKUP_PASSPHRASE" + ) + parser.add_argument("-v", "--verbose", action="store_true", help="print logs") + args = parser.parse_args() + + if args.verbose: + logging.basicConfig(level=logging.INFO) + + path = os.environ.get("PATH") + venv_path = sys.argv[0].strip("backup-dc-from-db-cli") + os.environ["PATH"] = path + ":" + venv_path + + with Rpc(accounts_dir=args.db_path) as rpc: + deltachat = DeltaChat(rpc) + system_info = deltachat.get_system_info() + logging.info("Running deltachat core %s", system_info.deltachat_core_version) + + if not args.account: + print("Select an account:\n") + accounts = deltachat.get_all_accounts() + i = 0 + for ac in accounts: + addr = ac.get_config('addr') + if addr == args.account: + account = ac + break + else: + print(f"{i}: {addr}") + else: + try: + account = accounts[int(input("\nWhich account do you want to backup?"))] + except: + import pdb; pdb.set_trace() + + if args.output: + output_filename = args.output + else: + timestamp = datetime.datetime.today().isoformat()[:10] + i = 0 + output_filename = f"delta-chat-backup-{timestamp}-{i}-{account.get_config('addr')}.tar" + account.export_backup(output_filename, passphrase=args.passphrase) + + +if __name__ == "__main__": + main()