update to use argparse and added option to only backup certain accounts

This commit is contained in:
2022-12-30 17:12:03 -05:00
parent 5ec2a77630
commit c2e14da1bd

View File

@@ -8,7 +8,7 @@ import getpass
import logging
import secrets
import base64
import optparse
import argparse
import hmac as pyhmac
import datetime
import time
@@ -303,31 +303,37 @@ def select_account(accounts, wording = "edit"):
if __name__ == "__main__":
# Build and parse arguments
parser = optparse.OptionParser(
parser = argparse.ArgumentParser(
usage="%prog [options]",
description="Run Bitwarden backup opteration. This will produce an encrypted zip/tar with exported CSV, JSON, and attachements.")
parser.add_option(
parser.add_argument(
"-a",
"--accounts",
nargs="+",
dest="accounts_to_backup",
help="Accounts to backup instead of all accounts."
)
parser.add_argument(
"-c",
"--config",
action="store_true",
dest="config",
help="Edit Bitwarden account configuration."
)
parser.add_option (
parser.add_argument (
"-v",
"--verbose",
action="store_true",
dest="debug",
help="Output debug/verbose info to the console for troubleshooting."
)
parser.add_option (
parser.add_argument (
"--no-encryption",
action="store_true",
dest="no_encrypt",
help="Will only zip up export and will NOT encrypt anything."
)
opts, args = parser.parse_args(sys.argv[1:])
opts = parser.parse_args()
os_detected = platform.system()
script_directory = os.path.dirname(os.path.realpath(__file__))
@@ -368,9 +374,7 @@ if __name__ == "__main__":
print("SRM/sdelete.exe ({}) cannot be found. Please make sure it is installed and executable.".format(secure_delete_executable))
client = client.ProxyKmipClient(config_file=pykmip_client_config_file)
client.open()
#print(encrypt(client, "test"))
client.open()
if opts.config:
while True:
@@ -442,7 +446,19 @@ if __name__ == "__main__":
print("No configuration file found. Please run {} -c to configure your accounts.".format(script_name))
sys.exit(-1)
accounts = read_config_file(secrets_config_file)
emails = list(accounts)
emails_from_config_file = list(accounts)
if opts.accounts_to_backup:
emails_to_backup = list()
for email in opts.accounts_to_backup:
if email in emails_from_config_file:
emails_to_backup.append(email)
if len(emails_to_backup) > 0:
emails = emails_to_backup
else:
logger.error("None of the emails passed in are in the config file.")
sys.exit(1)
else:
emails = emails_from_config_file
for email in emails:
vault_password = accounts[email]['account_vault_password']
os.environ["BW_CLIENTID"] = accounts[email]['account_api_client_id']
@@ -541,6 +557,6 @@ if __name__ == "__main__":
logger.debug((subprocess.run([bitwarden_cli_executable, 'logout'], capture_output=True).stdout).decode())
del accounts
del emails
del emails_from_config_file
client.close()
sys.exit(0)