Last active
May 3, 2021 16:35
-
-
Save jahrmando/c3cc03033557d1f427af48282f6940be to your computer and use it in GitHub Desktop.
PostgreSQL backups
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 | |
## usage | |
# default: ./postgresql_backup.sh | |
# load envs | |
# Create a file .backup_envs | |
# DB_NAME=mydb | |
# DB_HOST=myhost | |
# DB_PORT=5432 | |
# DB_USER=my_user | |
# DB_PASS="supersecretstring" | |
source .backup_envs | |
# | |
TIMESTAMP=$(date +'%F-%H_%M') | |
DUMP_PATH=backups | |
grep "$DB_HOST:$DB_PORT:$DB_NAME:$DB_USER:$DB_PASS" $HOME/.pgpass | |
if [ $? != '0' ]; then | |
echo "Add credentials on pgpass" | |
echo "$DB_HOST:$DB_PORT:$DB_NAME:$DB_USER:$DB_PASS" >> $HOME/.pgpass | |
fi | |
mkdir -v $DUMP_PATH > /dev/null | |
# Generate backups | |
pg_dump -U $DB_USER -h $DB_HOST -p $DB_PORT -F c -b -v -f "$DUMP_PATH/$DB_NAME-$TIMESTAMP.sql" $DB_NAME | |
# Compress backup | |
tar -zcvf $DUMP_PATH/backup-$DB_NAME-$TIMESTAMP.tar.gz $DUMP_PATH/$DB_NAME-$TIMESTAMP.sql | |
# Delete backup | |
rm -vf $DUMP_PATH/$DB_NAME-$TIMESTAMP.sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment