dip

old school recipes for janitorial snafu.
git clone git://git.ricketyspace.net/dip.git
Log | Files | Refs

commit 4718582d51318d79d5ce1d45b5cc56416defcacb
parent dce2fcd4ab25dabb3e98b4e1804360228db4ecdf
Author: rsiddharth <s@ricketyspace.net>
Date:   Thu, 24 May 2018 22:15:46 +0000

usr: Add pgdump (from dotfiles).

Diffstat:
usr/local/bin/pgdump | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 92 insertions(+), 0 deletions(-)

diff --git a/usr/local/bin/pgdump b/usr/local/bin/pgdump @@ -0,0 +1,92 @@ +#!/usr/bin/env zsh +# +# Copyright © 2017 rsiddharth <s@ricketyspace.net> +# +# License: GPL-3.0-or-later +# +# Backup postgres databases. + +setopt shwordsplit + +dbs_file() +{ + DBS_FILE=$HOME'/.pg-dump-dbs' + + if [ ! -r $DBS_FILE ]; then + echo 'Error: ' $DBS_FILE 'not found' + exit 1 + fi +} + +dbs() +{ + DBS=$(cat $DBS_FILE) + + if [ -z "$DBS" ]; then + echo 'No databases to dump exiting' + exit 0 + fi +} + +init_user() +{ + if [ ! -z $1 ]; then + USER=$1 + fi + + if [ -z $USER ]; then + echo 'Error: USER not set' + exit 1 + fi +} + +init_dumps_dir() +{ + if [ ! -z $1 ]; then + PGDUMPS_DIR=$1 + fi + + if [ -z $PGDUMPS_DIR ]; then + echo 'Error: PGDUMPS_DIR not set' + exit 1 + fi + + # Create dumps directory if not already created. + if [ ! -d $PGDUMPS_DIR ]; then + echo 'Creating pgdumps directory ' $PGDUMPS_DIR + mkdir -p $PGDUMPS_DIR + fi + +} + +bu_dump_dir() +{ + BU_DIR=$1'.'`date +%s` + echo 'Backing up existing dump directory' $1 'to' $BU_DIR + mv $1 $BU_DIR +} + +init_dump_dir() +{ + export DUMP_DIR=$PGDUMPS_DIR'/dump-'$1 + + # If it already exists; backup it up. + if [ -d $DUMP_DIR ]; then + bu_dump_dir $DUMP_DIR + fi +} + +dump_db () +{ + pg_dump -U $USER -cC -Fd -d $1 -f $DUMP_DIR +} + +# init. stuff. +dbs_file && dbs && init_user $1 && init_dumps_dir $2 + +# do the dumps. +for db in $DBS; do + init_dump_dir $db \ + && echo 'Dumping' $db'... at' $DUMP_DIR \ + && dump_db $db +done