diff --git a/bitwardenBackup.py b/bitwardenBackup.py index ba6b8c5..7e6afad 100644 --- a/bitwardenBackup.py +++ b/bitwardenBackup.py @@ -219,9 +219,6 @@ def decrypt_v1(array): except Exception as e: logger.error(e) -def does_file_exist(filepath): - return os.path.exists(filepath) - def new_account_details(): print("Requesting account details to add to config.") account_email_address = input("Please enter Bitwarden account email address: ") @@ -368,7 +365,7 @@ if __name__ == "__main__": if opts.config: while True: - if not does_file_exist(secrets_config_file): + if not os.path.exists(secrets_config_file): print("No Bitwarden accounts found, do you want to make a new one?") print(" ") print("n) New account") @@ -432,7 +429,7 @@ if __name__ == "__main__": print("This value must be one of the following characters: e, n, d, q.") #json.loads((subprocess.check_output(['bw.exe','status'])).decode())['status'] - if not does_file_exist(secrets_config_file): + if not os.path.exists(secrets_config_file): 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) @@ -463,20 +460,27 @@ if __name__ == "__main__": file_name = 'Bitwarden {} Export {}'.format(email, datetime_string) while True: logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--output', os.path.join(working_directory, '{}.csv'.format(file_name)) , '--format', 'csv'], capture_output=True).stdout).decode()) - file_size = os.path.getsize('{}.csv'.format(file_name)) + time.sleep(1) + file_size = os.path.getsize(os.path.join(working_directory, '{}.csv'.format(file_name))) if (file_size > 0): break else: logger.info("CSV export did not run correctly, running export again") + if os.path.exists(os.path.join(working_directory, '{}.csv'.format(file_name))): + os.remove(os.path.join(working_directory, '{}.csv'.format(file_name))) + time.sleep(5) logger.debug("Exporting vault to JSON") - file_name = 'Bitwarden {} Export {}'.format(email, datetime_string) while True: logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--output', os.path.join(working_directory, '{}.json'.format(file_name)), '--format', 'json'], capture_output=True).stdout).decode()) - file_size = os.path.getsize('{}.json'.format(file_name)) + time.sleep(1) + file_size = os.path.getsize(os.path.join(working_directory, '{}.json'.format(file_name))) if (file_size > 56): break else: logger.info("JSON export did not run correctly, running export again") + if os.path.exists(os.path.join(working_directory, '{}.json'.format(file_name))): + os.remove(os.path.join(working_directory, '{}.json'.format(file_name))) + time.sleep(5) # looking for Organizations # look for organizations @@ -490,20 +494,24 @@ if __name__ == "__main__": logger.debug("Exporting organization vault to CSV") file_name = 'Bitwarden Organization {} Export {}'.format(organization['name'], datetime_string) while True: - logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--organizationid', '{}'.format(organization['id']), '--output', os.path.join(working_directory, '{}.csv'.format(file_name)) , '--format', 'csv'], capture_output=True).stdout).decode()) - file_size = os.path.getsize('{}.csv'.format(file_name)) + logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--organizationid', '{}'.format(organization['id']), '--output', os.path.join(working_directory, '{}.csv'.format(file_name)), '--format', 'csv'], capture_output=True).stdout).decode()) + time.sleep(1) + file_size = os.path.getsize(os.path.join(working_directory, '{}.csv'.format(file_name))) if (file_size > 0): break else: logger.info("CSV export did not run correctly, running export again") + time.sleep(5) logger.debug("Exporting organization vault to JSON") while True: logger.debug((subprocess.run([bitwarden_cli_executable, 'export', '--organizationid', '{}'.format(organization['id']), '--output', os.path.join(working_directory, '{}.json'.format(file_name)), '--format', 'json'], capture_output=True).stdout).decode()) - file_size = os.path.getsize('{}.json'.format(file_name)) + time.sleep(1) + file_size = os.path.getsize(os.path.join(working_directory, '{}.json'.format(file_name))) if (file_size > 56): break else: logger.info("JSON export did not run correctly, running export again") + time.sleep(5) logger.info("Downlading attachments...") bitwarden_items = json.loads(((subprocess.run([bitwarden_cli_executable, 'list', 'items'], capture_output=True)).stdout).decode())