scwrypts/zsh/db/postgres/pg_restore

56 lines
1.3 KiB
Plaintext
Raw Normal View History

#!/bin/zsh
_DEPENDENCIES+=(
pg_dump
)
_REQUIRED_ENV+=()
source ${0:a:h}/common.zsh
#####################################################################
BACKUP_POSTGRES() {
local _HOST _NAME _PASS _PORT _USER
GET_POSTGRES_LOGIN_ARGS $@
local DATA_DIR="$SCWRYPTS_DATA_PATH/db/$_HOST/$_NAME/pg_restore"
[ ! -d $DATA_DIR ] && mkdir -p $DATA_DIR
cd $DATA_DIR
local INPUT_FILE="$DATA_DIR/$_NAME.dump"
[ ! -f $INPUT_FILE ] && {
local DUMP="$(dirname $DATA_DIR)/pg_dump/$_NAME.dump"
__STATUS $DUMP
ls $DUMP
[ -f "$DUMP" ] && {
__SUCCESS "discovered previous scwrypts dump"
__SUCCESS "$DUMP"
__Yn 'restore from this backup?' && INPUT_FILE="$DUMP"
}
[ ! -f "$INPUT_FILE" ] && {
__STATUS 'place backup in the following location:'
__STATUS "$INPUT_FILE"
}
while [ ! -f $INPUT_FILE ]; do sleep 1; done
}
__STATUS "backup file : $DATA_DIR"
__STATUS "database : $_USER@$_HOST:$_PORT/$_NAME"
PGPASSWORD="$_PASS" pg_restore \
--verbose \
--single-transaction \
--format custom \
--host "$_HOST" \
--port "$_PORT" \
--username "$_USER" \
--dbname "$_NAME" \
"$INPUT_FILE" \
&& { __SUCCESS "finished restoring backup for '$_HOST/$_NAME'"; } \
|| { __ERROR "error restoring backup for '$_HOST/$_NAME' (see above)"; return 1; }
}
#####################################################################
BACKUP_POSTGRES $@