v2.1.0
===================================================================== --- New Scripts -------------------------- zsh ) database backup/restore - db/postgres/pg_dump - db/postgres/pg_restore - aws/rds/create-backup - aws/rds/load-backup redis-cached curl commands - redis/curl youtube download - youtube/download - youtube/get-audio-clip --- Changes ------------------------------ - 'scwrypts' executable now reloads upon execution to prevent staleness - added various options to improve api/cli; see 'scwrypts --help' for more --- Bug Fixes ---------------------------- - fixed an issue with .config settings' visibility to non-zsh scripts - fixed an issue with command arguments globbing too early
This commit is contained in:
@ -2,3 +2,23 @@ _DEPENDENCIES+=()
|
||||
_REQUIRED_ENV+=()
|
||||
source ${0:a:h}/../common.zsh
|
||||
#####################################################################
|
||||
|
||||
|
||||
GET_POSTGRES_LOGIN_ARGS() {
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
--host | -h ) _HOST="$2"; shift 2 ;;
|
||||
--name | -d ) _NAME="$2"; shift 2 ;;
|
||||
--pass | -w ) _PASS="$2"; shift 2 ;;
|
||||
--port | -p ) _PORT="$2"; shift 2 ;;
|
||||
--user | -U ) _USER="$2"; shift 2 ;;
|
||||
* ) shift 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
[ ! $_HOST ] && _HOST=127.0.0.1
|
||||
[ ! $_NAME ] && _NAME=postgres
|
||||
[ ! $_PORT ] && _PORT=5432
|
||||
[ ! $_USER ] && _USER=postgres
|
||||
}
|
||||
|
@ -8,23 +8,7 @@ source ${0:a:h}/common.zsh
|
||||
|
||||
_LOGIN_POSTGRES() {
|
||||
local _HOST _NAME _PASS _PORT _USER
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
--host | -h ) _HOST="$2"; shift 2 ;;
|
||||
--name | -d ) _NAME="$2"; shift 2 ;;
|
||||
--pass | -w ) _PASS="$2"; shift 2 ;;
|
||||
--port | -p ) _PORT="$2"; shift 2 ;;
|
||||
--user | -U ) _USER="$2"; shift 2 ;;
|
||||
* ) shift 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
[ ! $_HOST ] && _HOST=127.0.0.1
|
||||
[ ! $_NAME ] && _NAME=postgres
|
||||
[ ! $_PORT ] && _PORT=5432
|
||||
[ ! $_USER ] && _USER=postgres
|
||||
GET_POSTGRES_LOGIN_ARGS $@
|
||||
|
||||
local DATA_DIR="$SCWRYPTS_DATA_PATH/db/$_HOST"
|
||||
[ ! -d $DATA_DIR ] && mkdir -p $DATA_DIR
|
||||
|
4
zsh/db/postgres/common.zsh
Normal file
4
zsh/db/postgres/common.zsh
Normal file
@ -0,0 +1,4 @@
|
||||
_DEPENDENCIES+=()
|
||||
_REQUIRED_ENV+=()
|
||||
source ${0:a:h}/../common.zsh
|
||||
#####################################################################
|
44
zsh/db/postgres/pg_dump
Executable file
44
zsh/db/postgres/pg_dump
Executable file
@ -0,0 +1,44 @@
|
||||
#!/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_dump"
|
||||
[ ! -d $DATA_DIR ] && mkdir -p $DATA_DIR
|
||||
cd $DATA_DIR
|
||||
|
||||
local OUTPUT_FILE="$DATA_DIR/$_NAME.dump"
|
||||
[ -f $OUTPUT_FILE ] && {
|
||||
local BACKUP_COUNT=$(ls "$DATA_DIR/$_NAME."*".dump" | wc -l)
|
||||
ls "$DATA_DIR/$_NAME."*".dump"
|
||||
|
||||
__INFO "discovered previous dump for '$_HOST/$_NAME'"
|
||||
__INFO "backing up previous dump to '$_NAME.$BACKUP_COUNT.dump'"
|
||||
|
||||
mv "$OUTPUT_FILE" "$DATA_DIR/$_NAME.$BACKUP_COUNT.dump"
|
||||
}
|
||||
|
||||
__STATUS "making backup of : $_USER@$_HOST:$_PORT/$_NAME"
|
||||
__STATUS "output file : $OUTPUT_FILE"
|
||||
|
||||
PGPASSWORD="$_PASS" pg_dump \
|
||||
--verbose \
|
||||
--format custom \
|
||||
--host "$_HOST" \
|
||||
--port "$_PORT" \
|
||||
--username "$_USER" \
|
||||
--dbname "$_NAME" \
|
||||
--file "$OUTPUT_FILE" \
|
||||
&& { __SUCCESS "finished backup of '$_HOST/$_NAME'"; __SUCCESS "saved to '$OUTPUT_FILE'"; } \
|
||||
|| { __ERROR "error creating backup for '$_HOST/$_NAME' (see above)"; return 1; }
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
BACKUP_POSTGRES $@
|
55
zsh/db/postgres/pg_restore
Executable file
55
zsh/db/postgres/pg_restore
Executable file
@ -0,0 +1,55 @@
|
||||
#!/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 $@
|
Reference in New Issue
Block a user