Skip to content

Instantly share code, notes, and snippets.

@FreeWall
Created July 11, 2019 17:53
Show Gist options
  • Save FreeWall/c8fe025065724d12be60c66da3c37f57 to your computer and use it in GitHub Desktop.
Save FreeWall/c8fe025065724d12be60c66da3c37f57 to your computer and use it in GitHub Desktop.
#!/bin/bash
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
file=$1
publickey=$dir/backup-key-public.pem
if [ ! -f "$publickey" ]; then
echo "Public key $publickey not found."
exit
fi
if [ -f "$file" ]; then
dirkey=$(openssl rand -hex 16)
dirkey=encrypt_backup_$dirkey
mkdir "$dirkey"
key=$(openssl rand -hex 32)
echo $key > $dirkey/key.txt
openssl rsautl -encrypt -pubin -inkey $publickey < $dirkey/key.txt | base64 > $dirkey/encrypted-key.txt
7za a -tzip -p$key -mem=AES256 $dirkey/$(basename $file.zip) $file > /dev/null 2>&1
tar -zcvf $file.tar.gz -C $dirkey $(basename $file.zip) encrypted-key.txt --strip-components=2 --exclude "key.txt" > /dev/null 2>&1
rm -rf $dirkey
rm -f $file
else
echo "File $file not found."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment