dotfiles

config files and scripts at HOME.
git clone git://git.ricketyspace.net/dotfiles.git
Log | Files | Refs

commit e4f62a2c97791d4883db04620acddba3993f27c6
parent 3310f12fda6a6d547657e196f43650949a1c57b6
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon, 25 Dec 2017 16:44:02 +0000

Add .bin/pgdump

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

diff --git a/.bin/pgdump b/.bin/pgdump @@ -0,0 +1,92 @@ +#!/usr/bin/env zsh +# +# Copyright © 2017 rsiddharth <s@ricketyspace.net> +# +# Licensed under the GNU General Public License version 3 or higher. +# +# 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