Created
December 30, 2020 17:35
-
-
Save russellsimpkins/37c9c75db893a53f8e545a13d8145c0f to your computer and use it in GitHub Desktop.
gpg solution to keep a secret file encrypted
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
#!/bin/bash | |
# pw | |
# author: "Russell Simpkins"<russellsimpkins@gmail.com> | |
# this function executes the following: | |
# 1: copies the encrypted file from the Cloud drive | |
# 2: decrypts the file | |
# 3: opens the file in emacs | |
# 4: cleans up the ~ file emacs creates when you edit | |
# 5: calls pwc to encrypt the file | |
function pw() { | |
dir=/mnt/c/Users/russe/Documents/necessary-evil | |
cloud=/mnt/c/gdrive | |
if [ -f ${dir}/accounts.txt ]; then | |
echo "${dir}/accounts.txt exists" | |
return | |
fi | |
if [ -f ${dir}/accounts.gpg ]; then | |
echo "${dir}/accounts.gpg exists" | |
return | |
fi | |
if [ -f ${cloud}/accounts.gpg ]; then | |
cp ${cloud}/accounts.gpg ${dir} | |
else | |
echo "missing ${cloud}/accounts.gpg" | |
return | |
fi | |
gpg --output ${dir}/accounts.txt --decrypt ${dir}/accounts.gpg | |
rm ${dir}/accounts.gpg | |
emacs ${dir}/accounts.txt | |
rm ${dir}/accounts.txt~ | |
pwc | |
} | |
# pwc | |
# author: "Russell Simpkins"<russellsimpkins@gmail.com> | |
# this function has the logic to | |
# 1. encrypt the txt file | |
# 2. removes the original unecrypted file | |
# 3. checks if there is an encrypted file on the cloud drive | |
# 3.1 If there's no encrypted file, it moves the file to the cloud drive | |
# 4. Move the existing cloud file to a date version | |
# 5. Clean up and only keep 3 backups | |
function pwc() { | |
dir=/mnt/c/Users/russe/Documents/necessary-evil | |
cloud=/mnt/c/gdrive | |
email=russellsimpkins@gmail.com | |
maxback=3 | |
if [ -f ${dir}/accounts.gpg ]; then | |
echo "${dir}/accounts.gpg exists" | |
return | |
fi | |
if [ -f ${dir}/accounts.txt ]; then | |
gpg --output ${dir}/accounts.gpg --encrypt --recipient ${email} ${dir}/accounts.txt | |
mv ${dir}/accounts.txt ${dir}/accounts.txt.$(date +%m.%d.%Y-%H:%M) | |
if [ -f ${cloud}/accounts.gpg ]; then | |
mv ${cloud}/accounts.gpg ${cloud}/accounts.gpg.$(date +%m.%d.%Y-%H:%M) | |
mv ${dir}/accounts.gpg ${cloud} | |
v=0 | |
for e in $(ls -t ${cloud}/accounts.gpg*); do | |
if [[ $v > $maxback ]]; then | |
echo "Cleaning up $e" | |
rm $e | |
fi | |
((v=$v+1)) | |
done | |
else | |
mv ${dir}/accounts.gpg ${cloud} | |
fi | |
else | |
echo "Missing ${dir}/accounts.gpg" | |
return | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment