Skip to content

Instantly share code, notes, and snippets.

@azaitsev
Forked from schnapster/pg_b2_backup.sh
Last active August 10, 2022 15:26
Show Gist options
  • Select an option

  • Save azaitsev/1e5ff7c2424f231c618bd493d6c4bcc2 to your computer and use it in GitHub Desktop.

Select an option

Save azaitsev/1e5ff7c2424f231c618bd493d6c4bcc2 to your computer and use it in GitHub Desktop.

Revisions

  1. azaitsev revised this gist Aug 10, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions pg_b2_backup.sh
    Original file line number Diff line number Diff line change
    @@ -48,18 +48,18 @@ su - postgres -c "pg_dump -Fc ${DB} > ${DUMPDIR}/${FILENAME}"
    SHA1=$(sha1sum ${DUMPDIR}/${FILENAME} | sed -En "s/^([0-9a-f]{40}).*/\1/p")

    #log in to backblaze
    b2 authorize-account ${B2_ACCOUNT_ID} ${B2_APP_KEY}
    /usr/local/bin/b2 authorize-account ${B2_ACCOUNT_ID} ${B2_APP_KEY}

    # upload it
    b2 upload-file --sha1 ${SHA1} \
    /usr/local/bin/b2 upload-file --sha1 ${SHA1} \
    ${INFO} \
    --noProgress \
    ${BUCKET} \
    ${DUMPDIR}/${FILENAME} \
    ${FILENAME}

    #log out
    b2 clear-account
    /usr/local/bin/b2 clear-account


    # make sure file still exists and clean it up
  2. azaitsev revised this gist Jul 14, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions pg_b2_backup.sh
    Original file line number Diff line number Diff line change
    @@ -34,6 +34,7 @@ FILENAME=${DB}_$(date +%Y-%m-%d).dump
    INFO="--info app=${DB} --info db=${DB}"

    mkdir -p ${DUMPDIR}
    chown postgres:postgres -R ${DUMPDIR}

    # cleanup any old backups
    if [ -f "${DUMPDIR}/${FILENAME}" ]; then
  3. azaitsev revised this gist Jul 14, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pg_b2_backup.sh
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ echo "Backing up db ${DB} to bucket ${BUCKET}."

    source $3

    DUMPDIR="/tmp/${DB}/"
    DUMPDIR="/tmp/${DB}"

    #will look like: app_db_2017-12-31.dump
    FILENAME=${DB}_$(date +%Y-%m-%d).dump
  4. azaitsev revised this gist Jul 14, 2022. 1 changed file with 9 additions and 11 deletions.
    20 changes: 9 additions & 11 deletions pg_b2_backup.sh
    Original file line number Diff line number Diff line change
    @@ -3,14 +3,13 @@

    # pass 4 args:
    # - database name (needs to exist in postgres)
    # - app name (meta information)
    # - bucket name (target of the upload)
    # - file containing b2 credentials (it will be sourced and needs to set B2_ACCOUNT_ID and B2_APP_KEY)
    #
    # example: ./pg_b2_backup.sh db app backups-daily ~/b2_creds
    # example: ./pg_b2_backup.sh db backups-daily ~/b2_creds
    #
    # example for a daily cron job backing up database "db" of app "app" to b2 bucket "daily-backups":
    # 20 4 * * * cd ~/scripts && ./pg_b2_backup.sh db app daily-backups ~/b2_creds &>> ~/logs/backups.log
    # 20 4 * * * cd ~/scripts && ./pg_b2_backup.sh db daily-backups ~/b2_creds &>> ~/logs/backups.log


    set -e
    @@ -23,17 +22,16 @@ if [ "$EUID" -ne 0 ]; then
    fi

    DB=$1
    APP=$2
    BUCKET=$3
    echo "Backing up db ${DB} of app ${APP} to bucket ${BUCKET}."
    BUCKET=$2
    echo "Backing up db ${DB} to bucket ${BUCKET}."

    source $4
    source $3

    DUMPDIR="/tmp"
    DUMPDIR="/tmp/${DB}/"

    #will look like: app_db_2017-12-31.dump
    FILENAME=${APP}_${DB}_$(date +%Y-%m-%d).dump
    INFO="--info app=${APP} --info db=${DB}"
    FILENAME=${DB}_$(date +%Y-%m-%d).dump
    INFO="--info app=${DB} --info db=${DB}"

    mkdir -p ${DUMPDIR}

    @@ -43,7 +41,7 @@ if [ -f "${DUMPDIR}/${FILENAME}" ]; then
    fi

    # dump it
    su - postgres -c "pg_dump ${DB} > ${DUMPDIR}/${FILENAME}"
    su - postgres -c "pg_dump -Fc ${DB} > ${DUMPDIR}/${FILENAME}"

    # calculate sha1 sum
    SHA1=$(sha1sum ${DUMPDIR}/${FILENAME} | sed -En "s/^([0-9a-f]{40}).*/\1/p")
  5. @schnapster schnapster created this gist Jan 21, 2018.
    69 changes: 69 additions & 0 deletions pg_b2_backup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    #!/bin/bash
    # this requires the backblaze CLI tool to be installed (pip install b2)

    # pass 4 args:
    # - database name (needs to exist in postgres)
    # - app name (meta information)
    # - bucket name (target of the upload)
    # - file containing b2 credentials (it will be sourced and needs to set B2_ACCOUNT_ID and B2_APP_KEY)
    #
    # example: ./pg_b2_backup.sh db app backups-daily ~/b2_creds
    #
    # example for a daily cron job backing up database "db" of app "app" to b2 bucket "daily-backups":
    # 20 4 * * * cd ~/scripts && ./pg_b2_backup.sh db app daily-backups ~/b2_creds &>> ~/logs/backups.log


    set -e
    echo $(date)

    # Verify we are root
    if [ "$EUID" -ne 0 ]; then
    echo "Please run as root"
    exit 1
    fi

    DB=$1
    APP=$2
    BUCKET=$3
    echo "Backing up db ${DB} of app ${APP} to bucket ${BUCKET}."

    source $4

    DUMPDIR="/tmp"

    #will look like: app_db_2017-12-31.dump
    FILENAME=${APP}_${DB}_$(date +%Y-%m-%d).dump
    INFO="--info app=${APP} --info db=${DB}"

    mkdir -p ${DUMPDIR}

    # cleanup any old backups
    if [ -f "${DUMPDIR}/${FILENAME}" ]; then
    rm -f "${DUMPDIR}/${FILENAME}"
    fi

    # dump it
    su - postgres -c "pg_dump ${DB} > ${DUMPDIR}/${FILENAME}"

    # calculate sha1 sum
    SHA1=$(sha1sum ${DUMPDIR}/${FILENAME} | sed -En "s/^([0-9a-f]{40}).*/\1/p")

    #log in to backblaze
    b2 authorize-account ${B2_ACCOUNT_ID} ${B2_APP_KEY}

    # upload it
    b2 upload-file --sha1 ${SHA1} \
    ${INFO} \
    --noProgress \
    ${BUCKET} \
    ${DUMPDIR}/${FILENAME} \
    ${FILENAME}

    #log out
    b2 clear-account


    # make sure file still exists and clean it up
    if [ -f "${DUMPDIR}/${FILENAME}" ]; then
    rm -f "${DUMPDIR}/${FILENAME}"
    fi