Last active
May 26, 2019 17:39
-
-
Save maxpoletaev/b07467109977d7e386e9dcc54e045e21 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
set -e | |
DB_NAME=$1 | |
MAX_BACKUPS=${MAX_BACKUPS:-30} | |
DB_USER=${DB_USER:-"postgres"} | |
DB_HOST=${DB_HOST:-""} | |
PGPASSWORD=$DB_PASSWORD | |
BACKUP_DIR=${BACKUP_DIR:-"backups"} | |
BACKUP_FILE=$BACKUP_DIR/$DB_NAME'_'$(date +%F_%H%M%S).pgdump | |
if [[ ! -d $BACKUP_DIR ]]; then | |
echo "Creating backup dir" | |
mkdir -p $BACKUP_DIR | |
fi | |
echo "Creating backup of $DB_NAME" | |
pg_dump\ | |
--format=custom\ | |
--host=$DB_HOST\ | |
--username=$DB_USER\ | |
--file=$BACKUP_FILE\ | |
--dbname=$DB_NAME | |
echo "Removing old backups" | |
offset=$(($MAX_BACKUPS + 1)) # tail needs N+1 offset | |
old_backups=$(find "$BACKUP_DIR" -type f -name "$DB_NAME"_* | sort -r | tail -n +$offset) | |
if [[ $old_backups != "" ]]; then | |
echo $old_backups | xargs rm | |
fi | |
echo $BACKUP_FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment