pass-exporter/convert_to_csv.sh

47 lines
1.4 KiB
Bash
Raw Normal View History

2021-02-19 12:08:47 +01:00
#!/bin/bash
PASS_EXECUTABLE=pass
if ! hash $PASS_EXECUTABLE 2> /dev/null; then
2021-02-19 12:08:47 +01:00
echo "Pass executable not found. This script use pass to convert gpg password wallet. Exiting."
exit
fi
if [ -z "$PASSWORD_STORE_DIR" ]; then
echo "PASSWORD_STORE_DIR is empty or invalid. Please follow pass documentation to understand how configure it."
exit
fi
BASE_NAME=passwords
CURRENT_DATE=$(date "+%Y-%m-%d")
FINAL_FILE_NAME=${BASE_NAME}_${CURRENT_DATE}.csv
if [[ -f "$FINAL_FILE_NAME" ]]; then
echo "A file with name $FINAL_FILE_NAME already exists! Please rename or delete and re-execute this script."
exit
else
touch $FINAL_FILE_NAME
echo "\"Account\";\"Password\";\"Login\"" >> $FINAL_FILE_NAME
2021-02-19 12:08:47 +01:00
fi
if [[ -f "$FINAL_FILE_NAME" ]]; then
find $PASSWORD_STORE_DIR -not -type d -iname "*.gpg" -print0 | while IFS= read -r -d $'\0' f ; do {
GPG_FILE_NAME="${f:${#PASSWORD_STORE_DIR}+1}"
GPG_FILE_NAME=${GPG_FILE_NAME/\.gpg/}
PASSWORD=$($PASS_EXECUTABLE show "$GPG_FILE_NAME" | head -n1)
2021-02-19 12:08:47 +01:00
PASSWORD=${PASSWORD/\"/\\\"}
LOGIN_KEY=$($PASS_EXECUTABLE show "$GPG_FILE_NAME" | awk '/login/ || /ID/ || /email/ {print $2}')
2021-02-19 12:08:47 +01:00
GPG_FILE_NAME=${GPG_FILE_NAME//\// > }
echo "Processing ${GPG_FILE_NAME}";
echo "\"$GPG_FILE_NAME\";\"$PASSWORD\";\"$LOGIN_KEY\"" >> $FINAL_FILE_NAME
2021-02-19 12:08:47 +01:00
}; done;
unset f;
echo "Process done!"
else
echo "Something strange! The file $FINAL_FILE_NAME does not exists!"
fi