#!/bin/bash
#PURPOSE: This shell script is indented to decrypt the encrypted files(*.gpg files) using gpg command. we have .gpg files (encrypted) and public key of the party who encrypted the files.. we want to decrypt the files and keep a copy of encrypted files as well.
###########################
#Change the directories according to your requirement, don't change the variables :)
HOME=/home/myuser
#location of encrypted files
SRC="$HOME/ENCRYYPTED"
#location of archiving
ARCHIVE="$HOME/ARCHIVED"
#location of decrypted files
DEST="$HOME/DECRYPTED"
#public key file - to be used to decrypt
KEYFILE="$SRC/key.asc"
#Encrypted file listing - temporary use
ENC_FILES="$SRC/encrypt_files.txt"
#log file
LOGFILE="$SRC/gpg_decrypt.log"
####################################
PATH=.:/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:$PATH
############
#Functions
############
check_dirs()
{
#check dirs
if [ ! -d "$SRC" -o ! -d "$DEST" -o ! -d "$ARCHIVE" ]; then
echo "ERROR: All required directories are not available" | tee -a "$LOGFILE"
exit 1
fi
#check key file
if [ ! -r "$KEYFILE" ]; then
echo "ERROR: Key file $KEYFILE is not avaiable" | tee -a "$LOGFILE"
exit 1
fi
}
copy_files_to_dest()
{
local files
local file
local timestamp
local extn
local cpfile
#change working dir to source dir
cd "$SRC"
if [ $? -ne 0 ]; then
echo "ERROR:change to dir $SRC is not successful" | tee -a "$LOGFILE"
exit 1
fi
#copy the gpg files from SRC dir to ARCHIVE dir
files=`ls -1 *.gpg 2>/dev/null`
echo "$files" > "$ENC_FILES"
if [ -r "$ENC_FILES" ]; then
while IFS=$'\n' read -r file
do
if [ -f "$file" ]; then # if a file
timestamp=`ls -l --time-style="long-iso" "$file" |awk '{print $6 "-" $7}'|sed "s/:/-/"`
filename=`echo "$file" | awk -F . '{if (NF > 1) {print $1}'}`
extn=`echo "$file" | awk -F . '{if (NF > 1) {print $NF}'}`
cpfile="${filename}_${timestamp}.${extn}"
echo "File name : $file " | tee -a "$LOGFILE"
echo "Timestamp : $timestamp " | tee -a "$LOGFILE"
echo "Target file name : $cpfile" | tee -a "$LOGFILE"
echo "" | tee -a "$LOGFILE"
cp -p "$SRC/$file" "$ARCHIVE/$cpfile"
if [ $? -ne 0 ]; then
echo "ERROR: Copy of $file from $SRC to $ARCHIVE failed " | tee -a "$LOGFILE"
exit 1
else
echo "$file copied to $cpfile in $ARCHIVE " | tee -a "$LOGFILE"
fi
fi
done < "$ENC_FILES"
fi
}
import_public_key()
{
#import gpg public key
gpg --import --no-verbose "$KEYFILE"
if [ $? -ne 0 ]
then
echo "Error in importing public key . Check the key " | tee -a "$LOGFILE"
exit 1
fi
}
decrypt_files()
{
local file
local filename
local newfile
#decrypt files
while IFS=$'\n' read -r file
do
if [ -f "$file" ]; then # proceed if it is a file
filename=`echo "$file" | awk -F . '{if (NF > 1) {print $1}'}`
newfile="$filename.txt"
gpg --decrypt "$SRC/$file" > "$DEST/$newfile"
if [ $? -eq 0 ]; then
echo "$SRC/$file decrypted in $DEST/$newfile" | tee -a "$LOGFILE"
rm "$SRC/$file"
echo "$file removed from $SRC" | tee -a "$LOGFILE"
fi
fi
done < "$ENC_FILES"
}
del_tmp_file()
{
#delete temporary file
if [ -f "$ENC_FILES" ]
then
rm "$ENC_FILES"
fi
}
init_log()
{
NAME=`basename $0`
echo "*******Executing $NAME at $(date +"%Y-%m-%d %T") ********" | tee -a "$LOGFILE"
}
###########
#Main()
###########
check_dirs
init_log
copy_files_to_dest
import_public_key
decrypt_files
del_tmp_file
#SCRIPT ENDS here
How to use the script
#PURPOSE: This shell script is indented to decrypt the encrypted files(*.gpg files) using gpg command. we have .gpg files (encrypted) and public key of the party who encrypted the files.. we want to decrypt the files and keep a copy of encrypted files as well.
###########################
#Change the directories according to your requirement, don't change the variables :)
HOME=/home/myuser
#location of encrypted files
SRC="$HOME/ENCRYYPTED"
#location of archiving
ARCHIVE="$HOME/ARCHIVED"
#location of decrypted files
DEST="$HOME/DECRYPTED"
#public key file - to be used to decrypt
KEYFILE="$SRC/key.asc"
#Encrypted file listing - temporary use
ENC_FILES="$SRC/encrypt_files.txt"
#log file
LOGFILE="$SRC/gpg_decrypt.log"
####################################
PATH=.:/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:$PATH
############
#Functions
############
check_dirs()
{
#check dirs
if [ ! -d "$SRC" -o ! -d "$DEST" -o ! -d "$ARCHIVE" ]; then
echo "ERROR: All required directories are not available" | tee -a "$LOGFILE"
exit 1
fi
#check key file
if [ ! -r "$KEYFILE" ]; then
echo "ERROR: Key file $KEYFILE is not avaiable" | tee -a "$LOGFILE"
exit 1
fi
}
copy_files_to_dest()
{
local files
local file
local timestamp
local extn
local cpfile
#change working dir to source dir
cd "$SRC"
if [ $? -ne 0 ]; then
echo "ERROR:change to dir $SRC is not successful" | tee -a "$LOGFILE"
exit 1
fi
#copy the gpg files from SRC dir to ARCHIVE dir
files=`ls -1 *.gpg 2>/dev/null`
echo "$files" > "$ENC_FILES"
if [ -r "$ENC_FILES" ]; then
while IFS=$'\n' read -r file
do
if [ -f "$file" ]; then # if a file
timestamp=`ls -l --time-style="long-iso" "$file" |awk '{print $6 "-" $7}'|sed "s/:/-/"`
filename=`echo "$file" | awk -F . '{if (NF > 1) {print $1}'}`
extn=`echo "$file" | awk -F . '{if (NF > 1) {print $NF}'}`
cpfile="${filename}_${timestamp}.${extn}"
echo "File name : $file " | tee -a "$LOGFILE"
echo "Timestamp : $timestamp " | tee -a "$LOGFILE"
echo "Target file name : $cpfile" | tee -a "$LOGFILE"
echo "" | tee -a "$LOGFILE"
cp -p "$SRC/$file" "$ARCHIVE/$cpfile"
if [ $? -ne 0 ]; then
echo "ERROR: Copy of $file from $SRC to $ARCHIVE failed " | tee -a "$LOGFILE"
exit 1
else
echo "$file copied to $cpfile in $ARCHIVE " | tee -a "$LOGFILE"
fi
fi
done < "$ENC_FILES"
fi
}
import_public_key()
{
#import gpg public key
gpg --import --no-verbose "$KEYFILE"
if [ $? -ne 0 ]
then
echo "Error in importing public key . Check the key " | tee -a "$LOGFILE"
exit 1
fi
}
decrypt_files()
{
local file
local filename
local newfile
#decrypt files
while IFS=$'\n' read -r file
do
if [ -f "$file" ]; then # proceed if it is a file
filename=`echo "$file" | awk -F . '{if (NF > 1) {print $1}'}`
newfile="$filename.txt"
gpg --decrypt "$SRC/$file" > "$DEST/$newfile"
if [ $? -eq 0 ]; then
echo "$SRC/$file decrypted in $DEST/$newfile" | tee -a "$LOGFILE"
rm "$SRC/$file"
echo "$file removed from $SRC" | tee -a "$LOGFILE"
fi
fi
done < "$ENC_FILES"
}
del_tmp_file()
{
#delete temporary file
if [ -f "$ENC_FILES" ]
then
rm "$ENC_FILES"
fi
}
init_log()
{
NAME=`basename $0`
echo "*******Executing $NAME at $(date +"%Y-%m-%d %T") ********" | tee -a "$LOGFILE"
}
###########
#Main()
###########
check_dirs
init_log
copy_files_to_dest
import_public_key
decrypt_files
del_tmp_file
#SCRIPT ENDS here
How to use the script
- Place the encrypted *.gpg files in /home/myuser/ENCRYYPTED folder
- Place the publick key in /home/myuser/ENCRYYPTED folder. Name it as key.asc
- Place the script in any location that suits your requirement
- Run the script.
- Copy of encrypted files will go in /home/myuser/ARCHIVED folder
- Decrypted files will go in /home/myuser/DECRYPTED folder
- Logfile of the script will be in /home/myuser
No comments:
Post a Comment