Last active
October 29, 2023 00:12
-
-
Save monperrus/5f3ca5ac9aa59159b33770701c0d5dc7 to your computer and use it in GitHub Desktop.
Export KeePass folders and passwords to Nextcloud Passwords backup JSON format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Export KeePass/Keepass2/KeePassXC folders and passwords to Nextcloud Passwords backup JSON format | |
# | |
# The file can then be imported in Nextcloud "Backup and Restore" >> "Restore or import" >> Choose Format "Database backup" | |
# | |
# Prerequisites | |
# -------------- | |
# pip3 install pykeepass | |
# | |
# Usage | |
# ----- | |
# python3 export-keepass-to-nextcloud-password-json.py passfile.kbdx | |
# | |
# Author: Martin Monperrus | |
# URL: https://gist.github.com/monperrus/5f3ca5ac9aa59159b33770701c0d5dc7 | |
# License: Public domain | |
from pykeepass import PyKeePass | |
import getpass | |
import sys | |
import json | |
# load database | |
kp = PyKeePass(sys.argv[1], password=getpass.getpass()) | |
def group_to_bw_folder_json(folder): | |
parent = "00000000-0000-0000-0000-000000000000" | |
if folder.parentgroup and folder.parentgroup.name != "Root": parent = str(folder.parentgroup.uuid) | |
return {"id": str(folder.uuid),"label": folder.name,"parent":parent} | |
def entry_to_bw_json(entry): | |
if entry.title == None: entry.title = "" | |
if entry.username == None: entry.username = "" | |
if entry.password == None: entry.password = "" | |
if entry.notes and len(entry.notes)>4096: print("warning, notes too long for Nextcloud passwords", entry.title) | |
folder = "00000000-0000-0000-0000-000000000000" | |
if entry.group and entry.group.name != "Root": folder = str(entry.group.uuid) | |
return { | |
"id": str(entry.uuid), | |
"label": entry.title, | |
"url": entry.url, | |
"folder": folder, | |
"username":entry.username, | |
"password": entry.password, | |
"notes": entry.notes, | |
"customFields":[]# required by the importer | |
} | |
def export(kp): | |
items = [] | |
for i in kp.entries: | |
items.append(entry_to_bw_json(i)) | |
folders = [] | |
for g in kp.groups: | |
folders.append(group_to_bw_folder_json(g)) | |
with open('/tmp/nextcloud-passwords-backup.json','w') as f: | |
json.dump({ | |
"encrypted": False, | |
"version": 3, | |
"folders": folders, | |
"passwords": items | |
},f, indent=2) | |
export(kp) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment