yage
76a746a53e
===================================================================== Notice the major version change which comes with breaking changes to 2.x! Reconstructs "library" functions for both python and zsh scwrypts, with changes to virtualenv naming conventions (you'll need to refresh all virtualenv with the appropriate scwrypt). --- Changes ------------------------------ - changed a naming convention across zsh scripts, particularly removing underscores where there is no need to avoid naming clash (e.g. 'zsh/lib/utils/io.zsh' renames '__STATUS' to 'STATUS') - moved clients reliant on py.lib.http to the py.lib.http module - python scripts now rely on py.lib.scwrypts.execute - updated package.json in zx scripts to include `type = module` - 'scwrypts --list' commandline argument now includes additional relevant data for each scwrypt - environment variables no longer add themselves to be staged in the '.env.template' --- New Features ------------------------- - new 'use' syntax for disjoint import within zsh scripts; took me a very long time to convince myself this would be necessary - introduced scwrypt "groups" to allow portable module creation; (i.e. ability add your own scripts from another repo!) - py.lib.scwrypts.io provides a combined IO stream for quick, hybrid use of input/output files and stdin/stdout - py.lib.fzf provides a wrapper to provide similar functionality to zsh/utils/io.zsh including fzf_(head|tail) - improved efficiency of various scwrypts; notably reducing runtime of scwrypts/environment sync - improved scwrypts CLI by adding new options for exact scwrypt matching, better filtering, and prettier/more-detailed interfaces --- New Scripts -------------------------- - py/twilio ) basic SMS integration with twilio - send-sms - py/directus ) interactive directus GET query - get-items - py/discord ) post message to discord channel or webhook - post-message
159 lines
3.7 KiB
Bash
159 lines
3.7 KiB
Bash
#####################################################################
|
|
|
|
DEPENDENCIES+=(
|
|
pg_dump
|
|
pg_restore
|
|
psql
|
|
pgcli
|
|
)
|
|
|
|
REQUIRED_ENV+=()
|
|
|
|
#####################################################################
|
|
|
|
PSQL() {
|
|
[[ ${#ARGS[@]} -eq 0 ]] && POSTGRES__SET_LOGIN_ARGS $@
|
|
|
|
eval PGPASSWORD=$_PASS psql ${_ARGS[@]}
|
|
}
|
|
|
|
#####################################################################
|
|
|
|
PG_DUMP() {
|
|
local _HOST _NAME _PORT _USER _FILE
|
|
local DATA_DIR _PASS _ARGS=()
|
|
POSTGRES__SET_LOGIN_ARGS --verbose $@
|
|
|
|
local OUTPUT_FILE="$DATA_DIR/backup.$(date '+%Y-%m-%d.%H-%M')"
|
|
|
|
STATUS "
|
|
making backup of : $_USER@$_HOST:$_PORT/$_NAME
|
|
|
|
(compressed) : '$OUTPUT_FILE.dump'
|
|
(safe-raw) : '$OUTPUT_FILE.sql'
|
|
(raw) : '$OUTPUT_FILE.raw.sql'
|
|
"
|
|
|
|
: \
|
|
&& STATUS "creating compressed backup..." \
|
|
&& eval PGPASSWORD=$_PASS pg_dump ${_ARGS[@]} --format custom --file "$OUTPUT_FILE.dump" \
|
|
&& SUCCESS "completed compressed backup" \
|
|
&& STATUS "creating raw backup..." \
|
|
&& eval PGPASSWORD=$_PASS pg_dump ${_ARGS[@]} > "$OUTPUT_FILE.raw.sql" \
|
|
&& SUCCESS "completed raw backup" \
|
|
&& STATUS "creating single-transaction raw backup..." \
|
|
&& { echo "BEGIN;"; cat "$OUTPUT_FILE.raw.sql"; echo "END;" } > "$OUTPUT_FILE.sql" \
|
|
&& SUCCESS "completed single-transaction raw backup" \
|
|
|| { ERROR "error creating backup for '$_HOST/$_NAME' (see above)"; return 1; }
|
|
}
|
|
|
|
#####################################################################
|
|
|
|
PG_RESTORE() {
|
|
local _HOST _NAME _PORT _USER
|
|
local _PASS _ARGS=()
|
|
local _FILE
|
|
POSTGRES__SET_LOGIN_ARGS $@
|
|
|
|
local INPUT_FILE=$(find "$DATA_DIR"/backup.* -type f | FZF 'select database file to restore')
|
|
|
|
[ $INPUT_FILE ] && [ -f "$INPUT_FILE" ] || {
|
|
ERROR 'no file selected or missing backup file; aborting'
|
|
REMINDER "
|
|
backups must be *.sql or *.dump files starting with the prefix 'backup.'
|
|
in the following directory:
|
|
|
|
'$DATA_DIR'
|
|
"
|
|
return 1
|
|
}
|
|
|
|
local RAW=1
|
|
[[ $INPUT_FILE =~ \\.dump$ ]] && RAW=0
|
|
|
|
STATUS "
|
|
loading backup for : $_USER@$_HOST:$_PORT/$_NAME
|
|
|
|
file : '$INPUT_FILE'
|
|
"
|
|
|
|
local EXIT_CODE
|
|
[[ $RAW -eq 1 ]] && {
|
|
REMINDER "
|
|
loading a backup from a raw sql dump may result in data loss
|
|
|
|
make sure your database is ready to accept the database file!
|
|
"
|
|
|
|
yN 'continue?' || ABORT
|
|
|
|
PSQL < "$INPUT_FILE"
|
|
EXIT_CODE=$?
|
|
}
|
|
|
|
[[ $RAW -eq 0 ]] && {
|
|
PGPASSWORD="$_PASS" pg_restore ${_ARGS[@]} \
|
|
--verbose \
|
|
--format custom \
|
|
--single-transaction \
|
|
"$INPUT_FILE"
|
|
EXIT_CODE=$?
|
|
}
|
|
|
|
[[ $EXIT_CODE -eq 0 ]] \
|
|
&& SUCCESS "finished restoring backup for '$_HOST/$_NAME'" \
|
|
|| ERROR "error restoring backup for '$_HOST/$_NAME' (see above)" \
|
|
;
|
|
|
|
return $EXIT_CODE
|
|
}
|
|
|
|
#####################################################################
|
|
|
|
POSTGRES__LOGIN_INTERACTIVE() {
|
|
local _PASS _ARGS=()
|
|
POSTGRES__SET_LOGIN_ARGS $@
|
|
|
|
STATUS "performing login : $_USER@$_HOST:$_PORT/$_NAME"
|
|
STATUS "working directory : $DATA_DIR"
|
|
|
|
eval PGPASSWORD=$_PASS pgcli ${_ARGS[@]}
|
|
}
|
|
|
|
#####################################################################
|
|
|
|
POSTGRES__SET_LOGIN_ARGS() {
|
|
while [[ $# -gt 0 ]]
|
|
do
|
|
case $1 in
|
|
--host ) _ARGS+=(-h $2); _HOST="$2"; shift 1 ;;
|
|
--name ) _ARGS+=(-d $2); _NAME="$2"; shift 1 ;;
|
|
--port ) _ARGS+=(-p $2); _PORT="$2"; shift 1 ;;
|
|
--user ) _ARGS+=(-U $2); _USER="$2"; shift 1 ;;
|
|
|
|
--pass ) _PASS="$2"; shift 1 ;;
|
|
|
|
--file ) _FILE="$2"; shift 1 ;;
|
|
|
|
* ) _ARGS+=($1) ;;
|
|
esac
|
|
shift 1
|
|
done
|
|
|
|
[ $_FILE ] && [ ! -f "$_FILE" ] && {
|
|
ERROR "no such file '$_FILE'"
|
|
exit 1
|
|
}
|
|
|
|
[ $_HOST ] && [ $_NAME ] \
|
|
&& DATA_DIR="$SCWRYPTS_DATA_PATH/db/$_HOST/$_NAME" \
|
|
|| DATA_DIR="$EXECUTION_DIR/temp-db" \
|
|
;
|
|
|
|
[ ! -d "$DATA_DIR" ] && mkdir -p "$DATA_DIR"
|
|
cd "$DATA_DIR"
|
|
|
|
return 0
|
|
}
|
|
|