-
Notifications
You must be signed in to change notification settings - Fork 2
/
recover.sh
executable file
·92 lines (73 loc) · 1.98 KB
/
recover.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash
if [[ "$(uname)" == "Darwin" ]]; then
# different command for macos
cli_path="/Applications/KeePassXC.app/Contents/MacOS/keepassxc-cli"
sed_cmd="sed -i ''"
else
cli_path="keepassxc-cli"
sed_cmd="sed -i"
fi
function askForExport {
#
# Ask the user to add the XML files.
#
echo "In KeeWeb, go to the file settings and select \"Save to...\", then select \"XML\"."
echo "Save the XML to the following temporary directory: $dir"
echo "Please don't save it anywhere else to avoid security issues. The XML files are not encrypted."
if command -v xdg-open &> /dev/null
then
xdg-open $dir &> /dev/null
fi
echo "After placing all your exported XML files, press Enter to continue."
read -n1
}
function patchXml {
file=$1
function removeSelfClosingElement {
$sed_cmd "s/<$1\s*\/>//g" "$file"
}
removeSelfClosingElement "EnableSearching"
removeSelfClosingElement "EnableAutoType"
removeSelfClosingElement "MaintenanceHistoryDays"
removeSelfClosingElement "MasterKeyChange\w*"
}
function importXml {
echo "Exporting $1 to KDBX: $2"
$cli_path import -p "$1" "$2" && echo "OK"
}
function patchAll {
# Patch all files in the temp directory.
for file in *.xml
do
[ -e "$file" ] || continue
patchXml "$file"
importXml "$file" "$file.kdbx"
done
rm *.xml
echo
echo "The files have been successfully fixed. Move the KDBX files to somewhere safe and press ENTER."
echo "The temporary directory '$dir' will be removed."
read -n 1
}
# Advanced usage
if [ $# -eq 1 ]
then
patchXml "$1"
importXml "$1" "$1.kdbx"
exit 0
fi
# Basic usage
# Create a temporary directory.
dir=`mktemp -d`
function cleanup {
echo "Removing '$dir'"
rm -rf "$dir"
}
if [ ! -e "$dir" ]; then
>&2 echo "Unable to create a temporary directory to store the XML files."
exit 1
fi
trap cleanup EXIT
cd "$dir"
askForExport
patchAll