Last active
October 5, 2016 13:35
-
-
Save yuryroot/7bb89a286f0b840c6ee5 to your computer and use it in GitHub Desktop.
Bash script for backup Postgresql cluster
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
#!/usr/bin/env bash | |
PG_USER="postgres" | |
TIMESTAMP=$(date +%Y_%m_%d) | |
WORKING_DIR="/home/usgene/db_backup" | |
BACKUP_NAME="${TIMESTAMP}_pg_basebackup" | |
ARCHIVE_NAME="$BACKUP_NAME.tar.gz" | |
SPLIT_NAME_PREFIX="${ARCHIVE_NAME}_" | |
SPLIT_SIZE="10G" | |
log() { | |
echo "[$(date)] $1" | |
} | |
exit_unless_success() { | |
exit_code=$? | |
if [ $exit_code -ne 0 ]; then | |
exit $exit_code | |
fi | |
} | |
if [ ! -d "$WORKING_DIR" ]; then | |
mkdir -p "$WORKING_DIR" | |
exit_unless_success | |
fi | |
cd $WORKING_DIR | |
log "Creating basebackup..." | |
pg_basebackup -U $PG_USER --pgdata=$BACKUP_NAME --xlog-method=stream | |
exit_unless_success | |
log "Compressing backup..." | |
tar -czf $ARCHIVE_NAME $BACKUP_NAME | |
exit_unless_success | |
log "Splitting archive by parts of $SPLIT_SIZE..." | |
split -b $SPLIT_SIZE $ARCHIVE_NAME $SPLIT_NAME_PREFIX | |
exit_unless_success | |
log "Removing uncompressed basebackup..." | |
rm -rf $BACKUP_NAME | |
exit_unless_success |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment