Skip to content

Instantly share code, notes, and snippets.

@yuryroot
Last active October 5, 2016 13:35

Revisions

  1. yuryroot renamed this gist Jul 24, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. yuryroot revised this gist Jun 22, 2015. No changes.
  3. yuryroot revised this gist Jun 22, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,11 @@
    # * SCP_HOST
    # * SCP_DIR
    #
    # Also optional variable EMAIL_ADDRESS can be specified.
    # Also optional variable MAIL_TO can be specified.
    #
    # For example:
    # WORKING_DIR=/tmp/db_backup/ PG_USER=postgres SCP_USER=name SCP_HOST=localhost SCP_DIR=uploads ./db_backup.sh
    # EMAIL_ADDRESS=email@address.com WORKING_DIR=/tmp/db_backup/ PG_USER=postgres SCP_USER=name SCP_HOST=localhost SCP_DIR=uploads ./db_backup.sh
    # MAIL_TO=email@address.com WORKING_DIR=/tmp/db_backup/ PG_USER=postgres SCP_USER=name SCP_HOST=localhost SCP_DIR=uploads ./db_backup.sh

    TIMESTAMP=$(date +%Y_%m_%d)
    BACKUP_NAME="${TIMESTAMP}_pg_basebackup"
    @@ -32,9 +32,9 @@ exit_unless_success() {
    }

    send_mail() {
    if [ -n "$EMAIL_ADDRESS" ]; then
    log "Sending email to $EMAIL_ADDRESS..."
    mail -s "$1" $EMAIL_ADDRESS < /dev/null
    if [ -n "$MAIL_TO" ]; then
    log "Sending email to $MAIL_TO..."
    mail -s "$1" $MAIL_TO < /dev/null
    fi
    }

  4. yuryroot revised this gist Jun 19, 2015. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,11 @@
    # * SCP_HOST
    # * SCP_DIR
    #
    # Also optional variable EMAIL_ADDRESS can be specified.
    #
    # For example:
    # WORKING_DIR=/tmp/db_backup/ PG_USER=postgres SCP_USER=name SCP_HOST=localhost SCP_DIR=uploads ./db_backup.sh
    # EMAIL_ADDRESS=email@address.com WORKING_DIR=/tmp/db_backup/ PG_USER=postgres SCP_USER=name SCP_HOST=localhost SCP_DIR=uploads ./db_backup.sh

    TIMESTAMP=$(date +%Y_%m_%d)
    BACKUP_NAME="${TIMESTAMP}_pg_basebackup"
    @@ -23,10 +26,18 @@ log() {
    exit_unless_success() {
    exit_code=$?
    if [ $exit_code -ne 0 ]; then
    send_mail "[$BACKUP_NAME] An unknown error occurred while creating backup. Exit code: $exit_code"
    exit $exit_code
    fi
    }

    send_mail() {
    if [ -n "$EMAIL_ADDRESS" ]; then
    log "Sending email to $EMAIL_ADDRESS..."
    mail -s "$1" $EMAIL_ADDRESS < /dev/null
    fi
    }

    if [ ! -d "$WORKING_DIR" ]; then
    mkdir -p "$WORKING_DIR"
    exit_unless_success
    @@ -52,3 +63,6 @@ exit_unless_success

    log "Cleaning..."
    rm -rf $BACKUP_NAME*
    exit_unless_success

    send_mail "[$BACKUP_NAME] Database backup was successfully created."
  5. yuryroot revised this gist Jun 19, 2015. 1 changed file with 14 additions and 4 deletions.
    18 changes: 14 additions & 4 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,16 @@
    #!/usr/bin/env bash

    PG_USER="postgres"
    # These ENV variables should be specified:
    # * WORKING_DIR
    # * PG_USER
    # * SCP_USER
    # * SCP_HOST
    # * SCP_DIR
    #
    # For example:
    # WORKING_DIR=/tmp/db_backup/ PG_USER=postgres SCP_USER=name SCP_HOST=localhost SCP_DIR=uploads ./db_backup.sh

    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}_"
    @@ -39,6 +46,9 @@ 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
    log "Uploading to remote server..."
    scp $SPLIT_NAME_PREFIX* $SCP_USER@$SCP_HOST:$SCP_DIR
    exit_unless_success

    log "Cleaning..."
    rm -rf $BACKUP_NAME*
  6. yuryroot revised this gist Jun 19, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -38,3 +38,7 @@ 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
  7. yuryroot created this gist Jun 19, 2015.
    40 changes: 40 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    #!/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