mirror of
https://github.com/infra7ti/dbtool.git
synced 2025-12-05 23:02:37 -03:00
Initial commit
This commit is contained in:
120
lib/plugins/createdb.bash
Normal file
120
lib/plugins/createdb.bash
Normal file
@@ -0,0 +1,120 @@
|
||||
#!/usr/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
# -- CreateDb Class ------------------------------------------------------------
|
||||
|
||||
createdb.__load__() {
|
||||
return 0
|
||||
}
|
||||
|
||||
createdb.run() {
|
||||
local _logdir=$(pwd)/logs
|
||||
local _pgpassfile=~/.pgpass
|
||||
declare -A pids=()
|
||||
|
||||
function __write_pgpass() {
|
||||
if [[ ! -z "${host[name]}" ]]; then
|
||||
touch ${_pgpassfile} && \
|
||||
chmod 0600 ${_pgpassfile}
|
||||
printf "%s:%s:*:%s:%s" \
|
||||
${host[db_host]} \
|
||||
${host[db_port]} \
|
||||
${host[db_user]} \
|
||||
${host[db_pass]} \
|
||||
> ${_pgpassfile}
|
||||
fi
|
||||
}
|
||||
|
||||
function __pre() {
|
||||
${ui}.debug 'text' '\n'
|
||||
mkdir -p ${_logdir}
|
||||
__write_pgpass
|
||||
echo
|
||||
}
|
||||
|
||||
function __post() {
|
||||
rm -rf ${_pgpassfile}
|
||||
}
|
||||
|
||||
function __wait_processes() {
|
||||
${ui}.info && \
|
||||
${ui}.item $"Waiting for processes to complete\n"
|
||||
${ui}.line 'compact'
|
||||
while true; do
|
||||
alive_pids=()
|
||||
for _pid in "${!pids[@]}"; do
|
||||
_db=${pids[${_pid}]}
|
||||
kill -0 "${_pid}" 2>/dev/null \
|
||||
&& _etime[${_pid}]=$(ps -o etimes= -p ${_pid} || :) \
|
||||
&& alive_pids+="${_pid} "
|
||||
${ui}.info && \
|
||||
${ui}.print '%s ' $(${ui}.color green; echo "[createdb]")
|
||||
${ui}.color && \
|
||||
${ui}.subitem $"process still running "
|
||||
${ui}.print '%s ' \
|
||||
$(${ui}.color gold; ${ui}.arrow right; ${ui}.color)
|
||||
${ui}.print "[db=${_db}, pid=${_pid}, etime=%ss]\n" \
|
||||
${_etime[${_pid}]}
|
||||
done
|
||||
[ ${#alive_pids[@]} -eq 0 ] && break
|
||||
sleep 5
|
||||
done
|
||||
${ui}.print '\n'
|
||||
${ui}.info && \
|
||||
${ui}.print '%s ' $(${ui}.color green; echo "[createdb]")
|
||||
${ui}.color && ${ui}.subitem $"all processes terminated\n"
|
||||
${ui}.line 'compact'
|
||||
}
|
||||
|
||||
function __createdb() {
|
||||
local _createdb=$(which createdb || exit -1)
|
||||
local _logfile=create-${db}-$(date +"%Y%m%d%H%M%S").log
|
||||
local _cmd="${_createdb} \
|
||||
-h ${host[db_host]} \
|
||||
-p ${host[db_port]} \
|
||||
-U ${host[db_user]} \
|
||||
${host[create_extraopts]:-} \
|
||||
-O ${host[db_user]} ${db}"
|
||||
_cmd=$(echo -ne ${_cmd})
|
||||
${ui}.debug 'line' 'compact'
|
||||
${ui}.debug && ${ui}.tab 2
|
||||
${ui}.debug 'subitem' $"Running command:\n"
|
||||
#${ui}.debug 'text' "$(${ui}.color gold)${_cmd//${_restoredir}\//} \
|
||||
${ui}.debug 'text' "$(${ui}.color gold)${_cmd} \
|
||||
$(${ui}.color)" \
|
||||
| fold -sw 60 \
|
||||
| sed "s/^/$(printf '%14s' ' ')/g"
|
||||
${ui}.debug 'text' '\n'
|
||||
${ui}.debug && ${ui}.tab 2
|
||||
${ui}.debug 'subitem' $"Process will run in backgroud [logfile=%s]\n" \
|
||||
${_logfile}
|
||||
${_cmd} > ${_logdir}/${_logfile} 2>&1 &
|
||||
pids+=([$!]=${db})
|
||||
}
|
||||
|
||||
function __run() {
|
||||
${ui}.debug 'text' '\n'
|
||||
dbs="${dblist}"
|
||||
test -f ${cfgdir}/databases/${dblist}.txt && \
|
||||
dbs=$(< ${cfgdir}/databases/${dblist}.txt);
|
||||
for db in ${dbs}; do
|
||||
${ui}.info && \
|
||||
${ui}.item $"Starting %s %s [host=%s, db=%s]\n" \
|
||||
"createdb" \
|
||||
$(${ui}.color gold; ${ui}.arrow right; ${ui}.color) \
|
||||
${host[name]} \
|
||||
${db}
|
||||
__createdb
|
||||
${ui}.debug 'text' '\n'
|
||||
done
|
||||
__wait_processes
|
||||
}
|
||||
|
||||
__pre
|
||||
__run
|
||||
__post
|
||||
}
|
||||
|
||||
createdb.__load__
|
||||
|
||||
# vim: ts=4:sw=4:sts=4:et
|
||||
120
lib/plugins/dropdb.bash
Normal file
120
lib/plugins/dropdb.bash
Normal file
@@ -0,0 +1,120 @@
|
||||
#!/usr/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
# -- DropDb Class --------------------------------------------------------------
|
||||
|
||||
dropdb.__load__() {
|
||||
return 0
|
||||
}
|
||||
|
||||
dropdb.run() {
|
||||
local _logdir=$(pwd)/logs
|
||||
local _pgpassfile=~/.pgpass
|
||||
declare -A pids=()
|
||||
|
||||
function __write_pgpass() {
|
||||
if [[ ! -z "${host[name]}" ]]; then
|
||||
touch ${_pgpassfile} && \
|
||||
chmod 0600 ${_pgpassfile}
|
||||
printf "%s:%s:*:%s:%s" \
|
||||
${host[db_host]} \
|
||||
${host[db_port]} \
|
||||
${host[db_user]} \
|
||||
${host[db_pass]} \
|
||||
> ${_pgpassfile}
|
||||
fi
|
||||
}
|
||||
|
||||
function __pre() {
|
||||
${ui}.debug 'text' '\n'
|
||||
mkdir -p ${_logdir}
|
||||
__write_pgpass
|
||||
echo
|
||||
}
|
||||
|
||||
function __post() {
|
||||
rm -rf ${_pgpassfile}
|
||||
}
|
||||
|
||||
function __wait_processes() {
|
||||
${ui}.info && \
|
||||
${ui}.item $"Waiting for processes to complete\n"
|
||||
${ui}.line 'compact'
|
||||
while true; do
|
||||
alive_pids=()
|
||||
for _pid in "${!pids[@]}"; do
|
||||
_db=${pids[${_pid}]}
|
||||
kill -0 "${_pid}" 2>/dev/null \
|
||||
&& _etime[${_pid}]=$(ps -o etimes= -p ${_pid} || :) \
|
||||
&& alive_pids+="${_pid} "
|
||||
${ui}.info && \
|
||||
${ui}.print '%s ' $(${ui}.color green; echo "[dropdb]")
|
||||
${ui}.color && \
|
||||
${ui}.subitem $"process still running "
|
||||
${ui}.print '%s ' \
|
||||
$(${ui}.color gold; ${ui}.arrow right; ${ui}.color)
|
||||
${ui}.print "[db=${_db}, pid=${_pid}, etime=%ss]\n" \
|
||||
${_etime[${_pid}]}
|
||||
done
|
||||
[ ${#alive_pids[@]} -eq 0 ] && break
|
||||
sleep 5
|
||||
done
|
||||
${ui}.print '\n'
|
||||
${ui}.info && \
|
||||
${ui}.print '%s ' $(${ui}.color green; echo "[dropdb]")
|
||||
${ui}.color && ${ui}.subitem $"all processes terminated\n"
|
||||
${ui}.line 'compact'
|
||||
}
|
||||
|
||||
function __dropdb() {
|
||||
local _dropdb=$(which dropdb || exit -1)
|
||||
local _logfile=drop-${db}-$(date +"%Y%m%d%H%M%S").log
|
||||
local _cmd="${_dropdb} \
|
||||
-h ${host[db_host]} \
|
||||
-p ${host[db_port]} \
|
||||
-U ${host[db_user]} \
|
||||
${host[drop_extraopts]:-} \
|
||||
-f --if-exists ${db}"
|
||||
_cmd=$(echo -ne ${_cmd})
|
||||
${ui}.debug 'line' 'compact'
|
||||
${ui}.debug && ${ui}.tab 2
|
||||
${ui}.debug 'subitem' $"Running command:\n"
|
||||
#${ui}.debug 'text' "$(${ui}.color gold)${_cmd//${_restoredir}\//} \
|
||||
${ui}.debug 'text' "$(${ui}.color gold)${_cmd} \
|
||||
$(${ui}.color)" \
|
||||
| fold -sw 60 \
|
||||
| sed "s/^/$(printf '%14s' ' ')/g"
|
||||
${ui}.debug 'text' '\n'
|
||||
${ui}.debug && ${ui}.tab 2
|
||||
${ui}.debug 'subitem' $"Process will run in backgroud [logfile=%s]\n" \
|
||||
${_logfile}
|
||||
${_cmd} > ${_logdir}/${_logfile} 2>&1 &
|
||||
pids+=([$!]=${db})
|
||||
}
|
||||
|
||||
function __run() {
|
||||
${ui}.debug 'text' '\n'
|
||||
dbs="${dblist}"
|
||||
test -f ${cfgdir}/databases/${dblist}.txt && \
|
||||
dbs=$(< ${cfgdir}/databases/${dblist}.txt);
|
||||
for db in ${dbs}; do
|
||||
${ui}.info && \
|
||||
${ui}.item $"Starting %s %s [host=%s, db=%s]\n" \
|
||||
"dropdb" \
|
||||
$(${ui}.color gold; ${ui}.arrow right; ${ui}.color) \
|
||||
${host[name]} \
|
||||
${db}
|
||||
__dropdb
|
||||
${ui}.debug 'text' '\n'
|
||||
done
|
||||
__wait_processes
|
||||
}
|
||||
|
||||
__pre
|
||||
__run
|
||||
__post
|
||||
}
|
||||
|
||||
dropdb.__load__
|
||||
|
||||
# vim: ts=4:sw=4:sts=4:et
|
||||
123
lib/plugins/pg_dump.bash
Normal file
123
lib/plugins/pg_dump.bash
Normal file
@@ -0,0 +1,123 @@
|
||||
#!/usr/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
# -- PGDump Class -------------------------------------------------------------
|
||||
|
||||
pg_dump.__load__() {
|
||||
return 0
|
||||
}
|
||||
|
||||
pg_dump.run() {
|
||||
local _dumpdir=$(pwd)/export/${host[name]}
|
||||
local _logdir=$(pwd)/logs
|
||||
local _pgpassfile=~/.pgpass
|
||||
declare -A pids=()
|
||||
|
||||
function __write_pgpass() {
|
||||
if [[ ! -z "${host[name]}" ]]; then
|
||||
touch ${_pgpassfile} && \
|
||||
chmod 0600 ${_pgpassfile}
|
||||
printf "%s:%s:*:%s:%s" \
|
||||
${host[db_host]} \
|
||||
${host[db_port]} \
|
||||
${host[db_user]} \
|
||||
${host[db_pass]} \
|
||||
> ${_pgpassfile}
|
||||
fi
|
||||
}
|
||||
|
||||
function __pre() {
|
||||
__write_pgpass
|
||||
mkdir -p ${_dumpdir} ${_logdir}
|
||||
echo
|
||||
}
|
||||
|
||||
function __post() {
|
||||
rm -rf ${_pgpassfile}
|
||||
}
|
||||
|
||||
function __wait_processes() {
|
||||
${ui}.info && \
|
||||
${ui}.item $"Waiting for processes to complete\n"
|
||||
${ui}.line 'compact'
|
||||
while true; do
|
||||
alive_pids=()
|
||||
for _pid in "${!pids[@]}"; do
|
||||
_db=${pids[${_pid}]}
|
||||
kill -0 "${_pid}" 2>/dev/null \
|
||||
&& _etime[${_pid}]=$(ps -o etimes= -p ${_pid} || :) \
|
||||
&& alive_pids+="${_pid} "
|
||||
${ui}.info && \
|
||||
${ui}.print '%s ' $(${ui}.color green; echo "[pg_dump]")
|
||||
${ui}.color && \
|
||||
${ui}.subitem $"process still running "
|
||||
${ui}.print '%s ' \
|
||||
$(${ui}.color gold; ${ui}.arrow right; ${ui}.color)
|
||||
${ui}.print "[db=${_db}, pid=${_pid}, etime=%ss]\n" \
|
||||
${_etime[${_pid}]}
|
||||
done
|
||||
[ ${#alive_pids[@]} -eq 0 ] && break
|
||||
sleep 5
|
||||
done
|
||||
${ui}.print '\n'
|
||||
${ui}.info && \
|
||||
${ui}.print '%s ' $(${ui}.color green; echo "[pg_dump]")
|
||||
${ui}.color && ${ui}.subitem $"all processes terminated\n"
|
||||
${ui}.line 'compact'
|
||||
}
|
||||
|
||||
function __pg_dump() {
|
||||
local _pg_dump=$(which pg_dump || exit -1)
|
||||
local _ext=${host[dump_ext]:-pgc}
|
||||
local _dumpfile=${db}-$(date +"%Y%m%d%H%M%S").${_ext}
|
||||
local _logfile=export-${db}-$(date +"%Y%m%d%H%M%S").${_ext}.log
|
||||
local _cmd="${_pg_dump} \
|
||||
-h ${host[db_host]} \
|
||||
-p ${host[db_port]} \
|
||||
-U ${host[db_user]} \
|
||||
-F ${host[dump_format]:0:1} \
|
||||
${host[dump_extraopts]:-} \
|
||||
-O -v -d ${db} \
|
||||
-f ${_dumpdir}/${_dumpfile}"
|
||||
_cmd=$(echo -ne ${_cmd})
|
||||
${ui}.debug 'line' 'compact'
|
||||
${ui}.debug && ${ui}.tab 2
|
||||
${ui}.debug 'subitem' $"Running command:\n"
|
||||
${ui}.debug 'text' "$(${ui}.color gold)${_cmd//${_dumpdir}\//} \
|
||||
$(${ui}.color)" \
|
||||
| fold -sw 60 \
|
||||
| sed "s/^/$(printf '%14s' ' ')/g"
|
||||
${ui}.debug 'text' '\n'
|
||||
${ui}.debug && ${ui}.tab 2
|
||||
${ui}.debug 'subitem' $"Process will run in backgroud [logfile=%s]\n" \
|
||||
${_logfile}
|
||||
${_cmd} > ${_logdir}/${_logfile} 2>&1 &
|
||||
pids+=([$!]=${db})
|
||||
}
|
||||
|
||||
function __run() {
|
||||
${ui}.debug 'text' '\n'
|
||||
dbs="${dblist}"
|
||||
test -f ${cfgdir}/databases/${dblist}.txt && \
|
||||
dbs=$(< ${cfgdir}/databases/${dblist}.txt);
|
||||
for db in ${dbs}; do
|
||||
${ui}.info && \
|
||||
${ui}.item $"Starting %s %s [host=%s, db=%s]\n" \
|
||||
"pg_dump" \
|
||||
$(${ui}.color gold; ${ui}.arrow right; ${ui}.color) \
|
||||
${host[name]} \
|
||||
${db}
|
||||
__pg_dump
|
||||
${ui}.debug 'text' '\n'
|
||||
done
|
||||
__wait_processes
|
||||
}
|
||||
|
||||
__pre
|
||||
__run
|
||||
__post
|
||||
}
|
||||
|
||||
pg_dump.__load__
|
||||
|
||||
# vim: ts=4:sw=4:sts=4:et
|
||||
135
lib/plugins/pg_restore.bash
Normal file
135
lib/plugins/pg_restore.bash
Normal file
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
# -- PGDump Class -------------------------------------------------------------
|
||||
|
||||
pg_restore.__load__() {
|
||||
return 0
|
||||
}
|
||||
|
||||
pg_restore.run() {
|
||||
local _restoredir=$(pwd)/import/${host[name]}
|
||||
local _logdir=$(pwd)/logs
|
||||
local _pgpassfile=~/.pgpass
|
||||
declare -A pids=()
|
||||
|
||||
function __write_pgpass() {
|
||||
if [[ ! -z "${host[name]}" ]]; then
|
||||
touch ${_pgpassfile} && \
|
||||
chmod 0600 ${_pgpassfile}
|
||||
printf "%s:%s:*:%s:%s" \
|
||||
${host[db_host]} \
|
||||
${host[db_port]} \
|
||||
${host[db_user]} \
|
||||
${host[db_pass]} \
|
||||
> ${_pgpassfile}
|
||||
fi
|
||||
}
|
||||
|
||||
function __pre() {
|
||||
${ui}.debug 'text' '\n'
|
||||
mkdir -p ${_restoredir} ${_logdir}
|
||||
dbs="${dblist}"
|
||||
test -f ${cfgdir}/databases/${dblist}.txt && \
|
||||
dbs=$(< ${cfgdir}/databases/${dblist}.txt);
|
||||
for db in ${dbs}; do
|
||||
# Check if dump file exists
|
||||
[ ! -f ${_restoredir}/${db}.${host[restore_ext]} ] && \
|
||||
${ui}.error && \
|
||||
${ui}.print $"Dump file not found: %s\n" \
|
||||
${db}.${host[restore_ext]} && \
|
||||
exit -1
|
||||
done
|
||||
__write_pgpass
|
||||
echo
|
||||
}
|
||||
|
||||
function __post() {
|
||||
rm -rf ${_pgpassfile}
|
||||
}
|
||||
|
||||
function __wait_processes() {
|
||||
${ui}.info && \
|
||||
${ui}.item $"Waiting for processes to complete\n"
|
||||
${ui}.line 'compact'
|
||||
while true; do
|
||||
alive_pids=()
|
||||
for _pid in "${!pids[@]}"; do
|
||||
_db=${pids[${_pid}]}
|
||||
kill -0 "${_pid}" 2>/dev/null \
|
||||
&& _etime[${_pid}]=$(ps -o etimes= -p ${_pid} || :) \
|
||||
&& alive_pids+="${_pid} "
|
||||
${ui}.info && \
|
||||
${ui}.print '%s ' $(${ui}.color green; echo "[pg_restore]")
|
||||
${ui}.color && \
|
||||
${ui}.subitem $"process still running "
|
||||
${ui}.print '%s ' \
|
||||
$(${ui}.color gold; ${ui}.arrow right; ${ui}.color)
|
||||
${ui}.print "[db=${_db}, pid=${_pid}, etime=%ss]\n" \
|
||||
${_etime[${_pid}]}
|
||||
done
|
||||
[ ${#alive_pids[@]} -eq 0 ] && break
|
||||
sleep 5
|
||||
done
|
||||
${ui}.print '\n'
|
||||
${ui}.info && \
|
||||
${ui}.print '%s ' $(${ui}.color green; echo "[pg_restore]")
|
||||
${ui}.color && ${ui}.subitem $"all processes terminated\n"
|
||||
${ui}.line 'compact'
|
||||
}
|
||||
|
||||
function __pg_restore() {
|
||||
local _pg_restore=$(which pg_restore || exit -1)
|
||||
local _ext=${host[restore_ext]:-pgc}
|
||||
local _restorefile=${db}.${_ext}
|
||||
local _logfile=import-${db}-$(date +"%Y%m%d%H%M%S").${_ext}.log
|
||||
local _cmd="${_pg_restore} \
|
||||
-h ${host[db_host]} \
|
||||
-p ${host[db_port]} \
|
||||
-U ${host[db_user]} \
|
||||
-F ${host[restore_format]:0:1} \
|
||||
${host[restore_extraopts]:-} \
|
||||
-O -v -d ${db} \
|
||||
${_restoredir}/${_restorefile}"
|
||||
_cmd=$(echo -ne ${_cmd})
|
||||
${ui}.debug 'line' 'compact'
|
||||
${ui}.debug && ${ui}.tab 2
|
||||
${ui}.debug 'subitem' $"Running command:\n"
|
||||
${ui}.debug 'text' "$(${ui}.color gold)${_cmd//${_restoredir}\//} \
|
||||
$(${ui}.color)" \
|
||||
| fold -sw 60 \
|
||||
| sed "s/^/$(printf '%14s' ' ')/g"
|
||||
${ui}.debug 'text' '\n'
|
||||
${ui}.debug && ${ui}.tab 2
|
||||
${ui}.debug 'subitem' $"Process will run in backgroud [logfile=%s]\n" \
|
||||
${_logfile}
|
||||
${_cmd} > ${_logdir}/${_logfile} 2>&1 &
|
||||
pids+=([$!]=${db})
|
||||
}
|
||||
|
||||
function __run() {
|
||||
${ui}.debug 'text' '\n'
|
||||
dbs="${dblist}"
|
||||
test -f ${cfgdir}/databases/${dblist}.txt && \
|
||||
dbs=$(< ${cfgdir}/databases/${dblist}.txt);
|
||||
for db in ${dbs}; do
|
||||
${ui}.info && \
|
||||
${ui}.item $"Starting %s %s [host=%s, db=%s]\n" \
|
||||
"pg_restore" \
|
||||
$(${ui}.color gold; ${ui}.arrow right; ${ui}.color) \
|
||||
${host[name]} \
|
||||
${db}
|
||||
__pg_restore
|
||||
${ui}.debug 'text' '\n'
|
||||
done
|
||||
__wait_processes
|
||||
}
|
||||
|
||||
__pre
|
||||
__run
|
||||
__post
|
||||
}
|
||||
|
||||
pg_restore.__load__
|
||||
|
||||
# vim: ts=4:sw=4:sts=4:et
|
||||
Reference in New Issue
Block a user