Created
June 22, 2021 13:11
-
-
Save teidesu/99ff82114fed53ff59624288827ef8b1 to your computer and use it in GitHub Desktop.
small script for automating postgres database backup and sending it to telegram
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 | |
# To generate public key: | |
# openssl req -x509 -nodes -days 1000000 -newkey rsa:4096 -keyout backup_key.pem\ | |
# -subj "/C=US/ST=Illinois/L=Chicago/O=IT/CN=www.example.com" \ | |
# -out backup_key.pem.pub | |
# | |
# To decrypt: | |
# openssl smime -decrypt -in some.bin -binary -inform DEM -inkey your.pem | bzcat > some.pg | |
BACKUP_FILE=`mktemp`.pg | |
RSA_PUBLIC='your.pem' | |
echo -e "\e[93m[i] Creating backup $BACKUP_FILE using pg_dump\e[0m" | |
if pg_dump -Fc -Z9 -f $BACKUP_FILE -U DATABASE_USER DATABASE_NAME; then | |
echo -e "\e[93m[i] Encrypting backup\e[0m" | |
bzip2 $BACKUP_FILE | |
openssl smime -encrypt -aes256 -binary -outform DEM -in $BACKUP_FILE.bz2 -out $BACKUP_FILE $RSA_PUBLIC | |
echo -e "\e[92m[i] Finished!\e[0m" | |
else | |
echo -e "\e[91m[x] Error!\e[0m" | |
exit 1 | |
fi | |
TG_TOKEN='...' | |
TG_CHANNEL='...' | |
echo -e "\e[93m[i] Uploading backup to Telegram\e[0m" | |
curl -X POST "https://api.telegram.org/bot$TG_TOKEN/sendDocument" \ | |
-F "chat_id=$TG_CHANNEL" \ | |
-F "document=@$BACKUP_FILE" \ | |
-F "caption=$(date)" > /dev/null | |
echo -e "\e[92m[i] OK!\e[0m" | |
rm $BACKUP_FILE | |
rm $BACKUP_FILE.bz2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment