Skip to content

Instantly share code, notes, and snippets.

@andriilive
Last active September 6, 2024 02:55
Show Gist options
  • Save andriilive/558ef3fea3157fae80562272f219cbdd to your computer and use it in GitHub Desktop.
Save andriilive/558ef3fea3157fae80562272f219cbdd to your computer and use it in GitHub Desktop.
BASH CONFIG
# ~/.bashrc: executed by bash(1) for non-login shells, see /usr/share/doc/bash/examples/startup-files
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# LOADS COMMON BASH ENV
[ -s "$HOME/.bashrc/0_common.sh" ] && \. "$HOME/.bashrc/0_common.sh"
# LOADS ENVIORMENT VARS IF PRESENTED
if [ -f "$HOME/.env" ]; then
set -a
source "$HOME/.env"
set +a
export GH_TOKEN=$GH_TOKEN_USER_ANDRII
fi
# LOADS DOTENV-VAULT VARS
[ -s "$HOME/.bash_aliases" ] && \. "$HOME/.bash_aliases"
[ -s "$HOME/.bashrc/1_alias_common.sh" ] && \. "$HOME/.bashrc/1_alias_common.sh"
# MAC OS SPECIFIC ALIASES
if [[ "$(uname -s)" == "Darwin" ]]; then
[ -s "$HOME/.bashrc/1_alias_osx.sh" ] && \. "$HOME/.bashrc/1_alias_osx.sh"
fi
# IF YOU'RE USING BAT AND MICRO
[ -s "$HOME/.bashrc/1_alias_bat_micro.sh" ] && \. "$HOME/.bashrc/1_alias_bat_micro.sh" # IF YOU'RE USING BATCAT AND MICRO
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
mesg n 2> /dev/null || true
# ~/.bashrc/0_common.sh
# https://gist.github.com/andriilive/558ef3fea3157fae80562272f219cbdd
# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
export LS_OPTIONS='--color=auto'
else
export LS_OPTIONS=''
fi
alias dir='dir $LS_OPTIONS'
alias vdir='vdir $LS_OPTIONS'
alias grep='grep $LS_OPTIONS'
alias fgrep='fgrep $LS_OPTIONS'
alias egrep='egrep $LS_OPTIONS'
alias ls='ls $LS_OPTIONS -lha'
# ~/.bashrc/1_alias_bat_micro.sh
# https://gist.github.com/andriilive/558ef3fea3157fae80562272f219cbdd
##### REWRITES ##############
##### MICRO ##############
alias nano_raw='nano'
alias nano='micro'
##### BAT ##############
alias cat_raw='cat'
alias bat='batcat'
alias cat='bat'
# ~/.bashrc/1_alias_common.sh
# https://gist.github.com/andriilive/558ef3fea3157fae80562272f219cbdd
alias sign='npm pkg set author=$AUTHOR'
# ~/.bashrc/1_alias_osx.sh
# https://gist.github.com/andriilive/558ef3fea3157fae80562272f219cbdd
##### OSX ###################
##### REWRITES ##############
alias l='ls -lha'
alias ls='ls -lha'
alias nano='micro'
alias md='glow'
alias batcat='cat'
alias cat='bat'
##### PHP ###################
alias php_cli='php'
alias wp_cli='wp'
alias php='valet php'
alias composer='valet composer'
alias wp='php /opt/homebrew/bin/wp'
# alias mysql='/opt/homebrew/opt/mariadb@10.5/bin/mysql'
# alias -- c='valet composer' / alias -- p='valet php' ??
##### APPS ###################
alias ps='open -a PhpStorm'
alias warp='open -a Warp'
alias gha='open -a GitHub\ Desktop'
# alias ws='open -a WebStorm'
# alias idea='open -na "/Applications/IntelliJ IDEA.app" --args "$@"'
#### OTHER ###################
# Warp SSSH (no wrap https://docs.warp.dev/features/ssh#implementation)
# alias sssh='/usr/bin/ssh'
# https://github.com/sbarex/QLMarkdown#command-line-interface
# if [[ $(which qlmarkdown_cli) ]]; then
alias md_html='qlmarkdown_cli'
# ~/.bashrc/2_fn_common.sh
# https://gist.github.com/andriilive/558ef3fea3157fae80562272f219cbdd
# str sanitize, e.g. Andriis-MacBook-Pro -> andriismacbookpro
function str_sanitize() {
if [ -z "$1" ]; then
echo 'Error: No argument provided. Usage: str_sanitize "string"'
return 1 # Return a non-zero status to indicate failure
fi
echo "$1" | tr -dc '[:alnum:]' | tr '[:upper:]' '[:lower:]'
}
# timestamp, e.g. 2023_11_30__02_24
function time_stamp() {
echo `date "+%Y_%m_%d__%H_%M"`
}
# stamp with device name, e.g. andriismacbookpro__2023_11_30__02_24
function stamp() {
echo "$(str_sanitize `hostname -s`)__$(time_stamp)"
}
#!/bin/bash
function echoY
{
FLAG=$1
shift
echo -e "\033[38;5;148m$FLAG\033[39m$@"
}
function echoB()
{
FLAG=$1
shift
echo -e "\033[1;4;94m$FLAG\033[0m$@"
}
function echoG
{
FLAG=$1
shift
echo -e "\033[32m$FLAG\033[0m$@"
}
function echoBOLD
{
FLAG=$1
shift
echo -e "\033[1m$FLAG\033[0m$@"
}
function echoCYAN
{
FLAG=$1
shift
echo -e "\033[1;36m$FLAG\033[0m$@"
}
RAM=$(free -m | awk 'NR==2{printf "%s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }')
LOAD=$(uptime | awk -F'[a-z]:' '{ print $2}')
DISK=$(df -h | awk '$NF=="/"{printf "%d/%dGB (%s)\n", $3,$2,$5}')
CPU=$(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}')
if [ -e /sys/devices/virtual/dmi/id/product_uuid ] && [[ "$(sudo cat /sys/devices/virtual/dmi/id/product_uuid | cut -c 1-3)" =~ (EC2|ec2) ]]; then
MYIP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
elif [ "$(sudo dmidecode -s bios-vendor)" = 'Google' ]; then
MYIP=$(curl -s -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip)
elif [ "$(dmidecode -s system-product-name | cut -c 1-7)" = 'Alibaba' ]; then
MYIP=$(curl -s http://100.100.100.200/latest/meta-data/eipv4)
elif [ "$(dmidecode -s system-manufacturer)" = 'Microsoft Corporation' ];then
MYIP=$(curl -s http://checkip.amazonaws.com || printf "0.0.0.0")
else
MYIP=$(curl -s http://checkip.amazonaws.com || printf "0.0.0.0")
fi
if [ "$(dmidecode -s bios-vendor)" = 'Vultr' ];then
HMUBUNTU=''
elif [ -e /home/ubuntu ]; then
HMUBUNTU='/home/ubuntu/'
elif [ "$(dmidecode -s system-manufacturer)" = 'Microsoft Corporation' ];then
HMUBUNTU='/root/'
else
HMUBUNTU=''
fi
MYDOMAIN="digitalandy.eu"
MYHOST="$(hostname).$MYDOMAIN"
DB_PASSWORD_ROOT=$(sudo cat /root/.db_password)
GIST_BASE="https://gist.githubusercontent.com/andriilive/558ef3fea3157fae80562272f219cbdd"
clear
cat <<EOF
Welcome to $(echoCYAN "$MYHOST")
Get started docs: $(echoBOLD "$GIST_BASE")
Ufw docs: $(echoBOLD "https://www.digitalocean.com/community/tutorials/ufw-essentials-common-firewall-rules-and-commands")
IP: $(echo "$MYIP")
Sys: $(echo "$LOAD")
Load: $(echoG "$CPU") CPU | $(echoG "$RAM") RAM
Disk usage: $(echoG "$DISK")
* Whitelist IP's with $(echoBOLD "ufw")
$(echoY "ufw status && ufw allow from 193.165.0.11")
* Download executible from gist:
FILE="90-system" OUT="/etc/update-motd.d/$FILE" wget -O "$OUT" "$GIST_BASE/raw/$FILE" --quiet && chmod u+x "$OUT" && chmod 755 "$OUT" && chown root:root "$OUT"
$(echoG "*****************************************************************")
$(echoG "******* POSTGRES ************************************************")
$(psql --version) | $(echoBOLD "https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546")
Login command: $(echoY "sudo -u postgres psql")
Superuser with password for connections at $(echoG "127.0.0.1:5432"):
$(echoBOLD "DB_PASSWORD")="$DB_PASSWORD_ROOT"
$(echoBOLD "DB_USER")="root"
$(echoCYAN "TablePlus"): $(echo "postgresql+ssh://root@$MYHOST/root:$DB_PASSWORD_ROOT@127.0.0.1:5432")
$(echoG "POSTGRES -- NEW DATABASE ****************************************")
DB_PASSWORD=_\$(LC_CTYPE=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c16)" && echo "$DB_PASSWORD"
sudo -u postgres psql -c "CREATE USER \$DB_USER WITH PASSWORD '\$DB_PASSWORD'; && sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;" && sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;"
It's highly reccommended to create unique db/user pairs
specific for each new project. Try running the below:
$(echoCYAN "Remote access"): "postgresql://root:$DB_PASSWORD_ROOT/$MYHOST:5432/DB_NAME"
$(echoG "*****************************************************************")
$(echoG "******* CADDY ***************************************************")
$(caddy --version) | $(echoBOLD "https://caddyserver.com/docs/getting-started")
Check $(echoBOLD "status") with $(echoY "systemctl status caddy")
$(echoBOLD "Caddyfile") /etc/caddy/Caddyfile
$(echoBOLD "DefaultWebRoot") /usr/share/caddy
$(echoG "*****************************************************************")
$(echoG "******* VCS *****************************************************")
EOF

Server config

1. Download readme & make file executible

GIST_BASE="https://gist.githubusercontent.com/andriilive/558ef3fea3157fae80562272f219cbdd"
OUT_DIR="/etc/update-motd.d" OUT_FILE="90-system" OUT="$OUT_DIR/raw/$OUT_FILE"
wget -O "$OUT" "$URL" --quiet && chmod u+x "$OUT" && chmod 755 "$OUT"
chown root:root "$OUT"
# FOR DIR:
chown -R root:root "$OUT_DIR"

ln -s "$OUT" "/root/readme.sh"
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 DB_NAME DB_USER DB_PASSWORD"
exit 1
fi
DB_NAME=$1
=$2
DB_PASSWORD=$3
sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';"
sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;"
echo "DATABASE_URL=\"postgresql://$DB_USER:$DB_PASSWORD@$MYHOST:5432/$DB_NAME\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment