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