Skip to content

Instantly share code, notes, and snippets.

View mrsarm's full-sized avatar
🏠
Working from home

Mariano Ruiz mrsarm

🏠
Working from home
View GitHub Profile
@mrsarm
mrsarm / utils-db-postgresql.sql
Created November 22, 2022 15:38
Postgres DB utils SQL scripts
-- DB size in bytes
SELECT pg_size_pretty( pg_database_size('dbname') );
-- Table size in bytes
SELECT pg_size_pretty( pg_total_relation_size('tablename') );
-- All table sizes sorted by size desc
SELECT table_name, pg_size_pretty( pg_relation_size(quote_ident(table_name)) )
FROM information_schema.tables
@mrsarm
mrsarm / docker-compose-services.sh
Created October 17, 2022 12:32
docker-compose-services: list all services found in the docker-compose.yml file
#!/usr/bin/env bash
if [ "$1" == "-h" -o "$1" == "--help" ]; then
echo 'docker-compose-services: list all services found in the docker-compose.yml file'
exit
fi
cat docker-compose.y*ml | grep -oP '(?<=^ )(\w|-)+(?=:)'
@mrsarm
mrsarm / taillog.sh
Last active May 19, 2023 15:51
taillog: tail FILE in --follow mode highlighting with colors the severity
#!/usr/bin/env bash
#
# taillog: tail FILE highlighting with colors the severity.
#
# Based in the script found at https://serverfault.com/a/1022806/256983
#
# TODO: check whether the syntax "\<MESSAGE\>" offers better compatibility than "\bMESSAGE\b" to match whole words only (Mac OS)
if [ "$1" = "-h" -o "$1" = "--help" ]
then
@mrsarm
mrsarm / psql-output-json.sql
Created January 17, 2022 21:12
psql-output-json.sql: Output SQL results from Psql into a JSON file (JSON Line file)
# \t\a\o /tmp/out.json
--Tuples only is off.
--Output format is aligned.
SELECT row_to_json(r) FROM my_table AS r;
# \t\a\o
--Tuples only is off.
--Output format is aligned.
@mrsarm
mrsarm / restore-backup-pg.sh
Created May 6, 2021 14:52
restore-backup.sh: Restore Postgres backups from Docker volume
#!/usr/bin/env sh
DB="pg-latest_data"
DB_BCK="pg-latest_data-backup-$(date --iso-8601)"
#DB_BCK="pg-latest_data-backup-SNAPSHOT"
echo "Restoring backup of '$DB' database with backup '$DB_BCK' ..."
echo
docker-compose down
docker volume rm $DB
@mrsarm
mrsarm / docker-sh
Last active July 16, 2022 20:52
docker-sh: start a shell session with the container passed
#!/usr/bin/env bash
if [ "$#" -ne 1 -o "$1" == "-h" -o "$1" == "--help" ]; then
echo 'docker-sh: start a shell session with the container passed'
echo
echo "Use: docker-sh CONTAINER_NAME"
echo
echo "NOTE: partial names can be used, but the session"
echo " is started only if one container match"
exit 2
@mrsarm
mrsarm / docker-volume-sh
Last active March 18, 2021 12:32
docker-volume-sh: start a shell session with the volume mounted at /data
#!/usr/bin/env bash
if [ "$#" -ne 1 -o "$1" == "-h" -o "$1" == "--help" ]; then
echo "docker-volume-sh: start a shell session with the volume mounted at /data"
echo
echo "Use: docker-volume-sh VOLUME"
echo
echo "NOTE: if VOLUME does not exist, docker creates it first"
echo
exit 2
@mrsarm
mrsarm / docker-volume-cp
Last active March 18, 2021 12:33
docker-volume-cp script: copy one Docker volume content to a new one
#!/usr/bin/env sh
if [ "$#" -ne 2 ]; then
echo "docker-volume-cp: copy one Docker volume content to another"
echo
echo "Use: docker-volume-cp VOL_SOURCE VOL_DEST"
echo
echo "NOTE: if VOL_DEST does not exist, docker-volume-cp creates it first"
exit 2
fi
@mrsarm
mrsarm / docker-compose-couchdb.yml
Last active March 18, 2021 12:37
docker-compose.yml CouchDB latest: launch a CouchDB server with a volume attached to persist data across sessions
couchdb-latest:
image: couchdb
environment:
- COUCHDB_USER=admin
- COUCHDB_PASSWORD=couch
ports:
- "5984:5984"
volumes:
- couchdb-data-latest:/opt/couchdb/data
- couchdb-conf-latest:/opt/couchdb/etc/local.d
@mrsarm
mrsarm / bash_prompt_custom.sh
Created June 15, 2020 22:09
bash_prompt_custom.sh Bash prompt config (add to your ~/.bashrc)
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
c_reset='\e[0m'
c_git_clean='\e[0;32m'
c_git_dirty='\e[0;31m'
else
c_reset=
c_git_clean=
c_git_dirty=
fi