Created
October 17, 2023 21:46
-
-
Save sethryder/f082bb65d208a2a89d648b3c3c8e7359 to your computer and use it in GitHub Desktop.
dblab engine snapshot preprocessing example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# preprocessing.sh | |
POSTGRES_VERSION=14 | |
POSTGRES_IMAGE="postgresai/extended-postgres:${POSTGRES_VERSION}" | |
POOL_NAME=dblab_pool | |
MOUNT_DIR=/var/lib/dblab/"${POOL_NAME}" | |
# logical mode (with dataset) | |
CURRENT_DATASET=$(find "${MOUNT_DIR}" -name data -type d -exec stat -c "%Y %n" {} \; | sort -n -r | head -1 | awk -F "/" '{print $6}') | |
CLONE_PRE_PATH=$(zfs list -t filesystem | grep "${POOL_NAME}" | grep "${CURRENT_DATASET}" | head -1 | awk '{print $5}') | |
# physical mode | |
#CLONE_PRE_PATH=$(zfs list -S dblab:datastateat -t filesystem | grep "${POOL_NAME}" | grep clone_pre | tail -1 | awk '{print $5}') | |
CLONE_PRE="${CLONE_PRE_PATH##*/}" | |
# psql command | |
_PSQL="docker exec dblab_preprocess_${CLONE_PRE} psql -U postgres" | |
# functions | |
function info() { | |
echo -e "$(date '+%Y/%m/%d %H:%M:%S') preprocessing.sh: [INFO] ${1}" | |
} | |
function error() { | |
echo -e "$(date '+%Y/%m/%d %H:%M:%S') preprocessing.sh: [ERROR] ${1}" >&2 | |
exit 1 | |
} | |
# run Docker container dblab_preprocess | |
docker rm -f dblab_preprocess_"${CLONE_PRE}" >/dev/null 2>&1 | |
info "Running container: dblab_preprocess_${CLONE_PRE}" | |
if ! docker run -d --rm --name dblab_preprocess_"${CLONE_PRE}" \ | |
--label dblab_control \ | |
--volume /var/run/docker.sock:/var/run/docker.sock \ | |
--volume "${CLONE_PRE_PATH}":/var/lib/dblab:rshared \ | |
--env PGDATA=/var/lib/dblab/data \ | |
"${POSTGRES_IMAGE}" | |
then | |
error "Failed to run container \"dblab_preprocess_${CLONE_PRE}\"" | |
else | |
info "Container \"dblab_preprocess_${CLONE_PRE}\" has been running" | |
fi | |
# check the availability of Postgres | |
declare -i attempts=( 120 ) | |
while [[ "$(${_PSQL} -XAtc 'select 1' 2> /dev/null)" != "1" ]]; do | |
if (( $((attempts -= 1)) == 0 )); then | |
error "Preprocess database is not ready." | |
fi | |
sleep 1 | |
done | |
# MAIN | |
# some code here, for example: | |
# execute for all databases (except for templates) | |
DBNAME_LIST=$( | |
${_PSQL} \ | |
-XAtc " | |
select | |
datname | |
from | |
pg_database | |
where | |
not datistemplate | |
" \ | |
) | |
for db in ${DBNAME_LIST}; do | |
info "Database: ${db}" | |
# create extensions (if not already exists) | |
for extension in pg_stat_statements pg_stat_kcache pg_wait_sampling; do | |
if [[ $( | |
${_PSQL} \ | |
-d "${db}" \ | |
-XAtc " | |
select | |
extname is not null | |
from | |
pg_catalog.pg_extension | |
where | |
extname = '${extension}' | |
" \ | |
) != 't' ]]; then | |
if ! ${_PSQL} -d "${db}" -XAtc " | |
create extension ${extension} | |
" 1> /dev/null; then | |
error "Failed to create extension \"${extension}\" in the database \"${db}\"" | |
else | |
info "Extension \"${extension}\" has been created in the database \"${db}\"" | |
fi | |
else | |
info "Extension \"${extension}\" already created in the database \"${db}\"" | |
fi | |
done | |
done | |
# stop PostgreSQL | |
info "Run checkpoint command [psql -XAtc \"CHECKPOINT\"]" | |
if ! ${_PSQL} -XAtc "CHECKPOINT" &> /dev/null; then | |
error "Failed to execute a CHECKPOINT command" | |
fi | |
info "Stopping PostgreSQL instance [pg_ctl --wait --timeout=1800 -D /var/lib/dblab/data stop]" | |
if ! docker exec dblab_preprocess_"${CLONE_PRE}" \ | |
bash -c " | |
su - postgres -c \" | |
/usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_ctl \ | |
--wait \ | |
--timeout=1800 \ | |
-D /var/lib/dblab/data \ | |
stop \ | |
\" | |
" 1> /dev/null; then | |
error "Failed to stop the Postgres instance in the container \"dblab_preprocess_${CLONE_PRE}\"" | |
else | |
info "The Postgres instance has been stopped" | |
fi | |
# stop Docker container dblab_preprocess | |
info "Stopping container: dblab_preprocess_${CLONE_PRE}" | |
if ! docker stop dblab_preprocess_"${CLONE_PRE}" &> /dev/null; then | |
error "Failed to stop container \"dblab_preprocess_${CLONE_PRE}\"" | |
else | |
info "Container \"dblab_preprocess_${CLONE_PRE}\" has been stopped" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment