Skip to content

Instantly share code, notes, and snippets.

@gchenfc
Created February 26, 2024 01:15
Show Gist options
  • Save gchenfc/78d97192ffdf10806946e466c82b4d7c to your computer and use it in GitHub Desktop.
Save gchenfc/78d97192ffdf10806946e466c82b4d7c to your computer and use it in GitHub Desktop.
Convenience shell functions to tar-encrypt (and decrypt-untar) folders with gpg (default AES256)
# See also: README.
# These are convenience functions to encrypt and decrypt files and folders.
function help() {
echo "Functions:"
echo " compress_folder (in_folder) [out_file_basename]"
echo " decompress_folder (in_file) [out_folder]"
echo "Call with -h for help."
}
function tar_files_without_dir() {
# Usage: tar_files_without_dir (in_folder) (out_file_basename)
original_dir=$(pwd)
cd "$1" || exit
tar -czvf "/tmp/$2" ./*
cd "$original_dir" || exit
}
# Function to compress a folder
# Usage: compress_folder (in_folder) [out_file_basename]
# Compresses the contents of in_folder into out_file_basename.tar.gz.gpg
# If out_file_basename is not provided, it will be the current date in YYYY-MM-DD format.
# Example: compress_folder /tmp/secret_files -> /tmp/secret_files-2021-01-01.tar.gz.gpg
function compress_folder() {
# Print help
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: compress_folder (in_folder) [out_file_basename]"
echo " Compresses the contents of in_folder into out_file_basename.tar.gz.gpg"
echo " If out_file_basename is not provided, it will be the current date in YYYY-MM-DD format."
echo "Example: compress_folder /tmp/secret_files -> /tmp/secret_files-2021-01-01.tar.gz.gpg"
return
fi
# Check input
if [ -z "$1" ]; then
echo "Error: No input folder provided. Call with -h for help."
return
else
if [ ! -d "$1" ]; then
echo "Error: Input folder does not exist. Call with -h for help."
return
fi
fi
# Set output file
if [ -z "$2" ]; then
out_file=$(date "+%Y-%m-%d")
else
out_file=$2
fi
# Compress
# tar -czvf /tmp/$out_file.tar.gz $1/*
tar_files_without_dir $1 $out_file.tar.gz
gpg -c /tmp/$out_file.tar.gz
mv /tmp/$out_file.tar.gz.gpg .
echo "Compressed and encrypted folder $1 into $(pwd)/$out_file.tar.gz.gpg"
# Cleaning up
echo "Cleaning up..."
rm /tmp/$out_file.tar.gz
echo "Done. Don't forget to delete the original folder $1."
}
# Function to decompress a file
# Usage: decompress_folder (in_file) [out_folder]
# Decompresses the contents of in_file into out_folder
# If out_folder is not provided, it will be extracted into /tmp/{basename of in_file}/.
# Example: decompress_folder 2021-01-01.tar.gz.gpg -> /tmp/2021-01-01/
function decompress_folder() {
# Print help
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: decompress_folder (in_file) [out_folder]"
echo " Decompresses the contents of in_file into out_folder"
echo " If out_folder is not provided, it will be extracted into /tmp/{basename of in_file}/."
echo "Example: decompress_folder 2021-01-01.tar.gz.gpg -> /tmp/2021-01-01/"
return
fi
# Check input
if [ -z "$1" ]; then
echo "Error: No input file provided. Call with -h for help."
return
else
if [ ! -f "$1" ]; then
echo "Error: Input file does not exist. Call with -h for help."
return
fi
fi
# Set output folder
if [ -z "$2" ]; then
if [[ $1 == *.tar.gz.gpg ]]; then
out_folder=/tmp/${1%.tar.gz.gpg}/
else
if [[ $1 == *.gpg ]]; then
out_folder=/tmp/${1%.gpg}/
else
echo "Error: Input file is not a .gpg file. Call with -h for help."
fi
fi
else
out_folder=$2
fi
# Decompress
gpg -d $1 > /tmp/$(basename $1 .gpg)
mkdir -p $out_folder
tar -xzvf /tmp/$(basename $1 .gpg) -C $out_folder
echo "Decompressed and decrypted file $1 into $out_folder"
# Cleaning up
echo "Cleaning up..."
rm /tmp/$(basename $1 .gpg)
echo "Done."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment