1
0
Fork 0
bashrc/mybashrc

305 lines
9.0 KiB
Bash

#!/bin/bash
# FUNCTIONS
if [ "$(id -u)" != "0" ]; then
IS_SUDOERS=$(groups | grep sudo)
if [ -x /usr/bin/sudo ] && [ -n "$IS_SUDOERS" ]; then
NEED_SUDO="/usr/bin/sudo"
else
NEED_SUDO=""
fi
fi
transfer_vtbox_net() {
{ curl --progress-bar --upload-file "$1" https://transfer.vtbox.net/$(basename $1) && echo ""; } | tee -a $HOME/.transfer.log && echo ""
}
compress_pigz() {
tar -I pigz -cvf "$1.tar.gz" "$1"
}
decompress_pigz() {
tar -I pigz -xvf "$1"
}
update_git_mybashrc() {
git -C $HOME/.mybashrc pull -q
}
EE_MYSQL_OPTIMIZATION() {
if [ -f $HOME/.my.cnf ]; then
/usr/bin/mysqlcheck -Aos --auto-repair
elif [ -f /etc/psa/.psa.shadow ]; then
MYSQL_PWD="$(cat /etc/psa/.psa.shadow)" /usr/bin/mysqlcheck -Aos -uadmin --auto-repair
else
echo "$HOME/.my.cnf or /etc/psa/.psa.shadow doesn't exist"
fi
}
encrypt_gpg() {
gpg -c "$1"
}
decrypt_gpg() {
gpg --output "${1%.gpg}" -d "$1"
}
LIST_BY_SIZE() {
du -sh ./* | sort -h
}
EE_DOCKER_SETUP() {
if [ ! -d $HOME/.ee ]; then
mkdir $HOME/.ee
fi
curl -fsSL get.docker.com -o $HOME/.ee/get-docker.sh
chmod +x $HOME/.ee/get-docker.sh
$HOME/.ee/get-docker.sh
}
SET_TINC_UP() {
sudo tincd -n "$1"
}
SET_TINC_DOWN() {
sudo tincd -n "$1" -k
}
DD_BENCHMARK_DISK() {
dd if=/dev/zero bs=1024 count=1000000 of=file_1GB
dd if=file_1GB of=/dev/null bs=1024
rm file_1GB
}
ffmpeg_start_time() {
if [ "${#}" = "0" ]; then
echo "ffmpeg-cut-start <start-time> <input> <duration(optional)>"
echo ""
echo "example : ffmpeg-cut-start 00:34:24.85 file.mp4 300"
else
FFMPEG_ARGS="-ss "$2" -i "$1""
if [ "$3" ]; then
FFMPEG_ARGS="$FFMPEG_ARGS -time $3 -c copy -movflags +faststart ${1%.mp4}-output.mp4"
else
FFMPEG_ARGS="$FFMPEG_ARGS -c copy -movflags +faststart ${1%.mp4}-output.mp4"
fi
/usr/bin/ffmpeg "$FFMPEG_ARGS"
fi
}
find_duplicates() {
if [ -z "$(command -v rdfind)" ]; then
apt-get install rdfind -y
else
rdfind -dryrun true -ignoreempty false "$1"
fi
}
MAINTENANCE_APT() {
# Colors
# Colors
CSI='\033['
CEND="${CSI}0m"
CRED="${CSI}1;31m"
CGREEN="${CSI}1;32m"
if [ "$(id -u)" = "0" ] || [ -n "$IS_SUDOERS" ]; then
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT UPDATE '
echo -e "${CGREEN}#############################################${CEND}"
$NEED_SUDO apt-get update
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT FULL-UPGRADE '
echo -e "${CGREEN}#############################################${CEND}"
DEBIAN_FRONTEND=noninteractive $NEED_SUDO apt-get -o Dpkg::Options::="--force-confmiss" -o Dpkg::Options::="--force-confold" -y dist-upgrade
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT-GET AUTOREMOVE '
echo -e "${CGREEN}#############################################${CEND}"
$NEED_SUDO apt-get -y --purge autoremove
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT AUTOCLEAN '
echo -e "${CGREEN}#############################################${CEND}"
$NEED_SUDO apt-get -y autoclean
$NEED_SUDO apt-get -y clean
## clean packages in deinstall state
DEINSTALLED=$($NEED_SUDO dpkg --get-selections | grep deinstall | cut -f1)
if [ -n "$DEINSTALLED" ]; then
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' CLEAN DEINSTALLED PACKAGES '
echo -e "${CGREEN}#############################################${CEND}"
$NEED_SUDO dpkg --get-selections | grep deinstall | cut -f1 | xargs dpkg --purge
fi
if [ -x /usr/bin/docker ]; then
list_images=$(docker images --filter "dangling=true" -q --no-trunc)
list_volumes=$(docker volume ls -qf dangling=true)
if [ -n "$list_images" ]; then
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' DOCKER IMAGES CLEANUP '
echo -e "${CGREEN}#############################################${CEND}"
docker rmi "$list_images"
fi
if [ -n "$list_volumes" ]; then
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' DOCKER VOLUMES CLEANUP '
echo -e "${CGREEN}#############################################${CEND}"
docker volume rm "$list_volumes"
fi
fi
OLD_LOGS=$($NEED_SUDO find /var/log/ -type f -mtime +30 -iname "*.gz")
if [ -n "$OLD_LOGS" ]; then
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' CLEANUP OLD LOGS '
echo -e "${CGREEN}#############################################${CEND}"
$NEED_SUDO find /var/log/ -type f -mtime +30 -iname "*.gz" -exec rm '{}' \;
fi
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' EE-BASHRC UPDATE '
echo -e "${CGREEN}#############################################${CEND}"
update_git_mybashrc
else
echo "you need to be root or sudoers to launch the maintenance"
fi
}
EE_NGINX_COMPILE() {
if [ ! -d $HOME/.scripts ]; then
mkdir $HOME/.scripts
fi
wget -qO $HOME/.scripts/nginx-build.sh https://raw.githubusercontent.com/VirtuBox/nginx-ee/master/nginx-build.sh
chmod +x $HOME/.scripts/nginx-build.sh
$HOME/.scripts/nginx-build.sh "$@"
}
EE_SHOW_LOG() {
{ [ -x /usr/bin/ccze ] && {
tail -n 500 "$1" | ccze -A
} } || { apt install ccze -y; tail -n 500 "$1" | ccze -A
}
}
_PYTHON_VIRTUALENV() {
virtualenv "$1"
source "$1/bin/activate"
}
CHEAT_CHECK() {
{
[ -x /usr/local/bin/cht.sh ] && { /usr/local/bin/cht.sh "$@"; }
[ -x /usr/bin/cht.sh ] && { /usr/bin/cht.sh "$@"; }
} || {
curl -s https://cht.sh/:cht.sh >/usr/bin/cht.sh || wget -O /usr/bin/cht.sh https://cht.sh/:cht.sh
chmod +x /usr/local/bin/cht.sh
/usr/local/bin/cht.sh "$@"
}
}
EE_SHOW_FPM() {
top -bn1 | grep -c "php-fpm"
}
MAGENTO_UPGRADE() {
if [ -x bin/magento ]; then
bin/magento maintenance:enable
bin/magento cache:flush
bin/magento setup:upgrade
rm -Rf "./generated/metadata/*" "./generated/code/*" "./var/cache/*" "./var/page_cache/*"
bin/magento setup:di:compile
/usr/bin/composer dump-autoload -o --apcu
rm -Rf "./pub/static/frontend/*" "./pub/static/adminhtml/*" "./var/view_preprocessed/*" "./pub/static/_cache/*" "./pub/static/_requirejs/*"
bin/magento setup:static-content:deploy fr_FR
bin/magento setup:static-content:deploy en_US --theme="Magento/backend"
bin/magento cache:enable
bin/magento maintenance:disable
else
echo "you are not in a magento root folder"
fi
}
_WP_PERMISSIONS() {
if [ -f ./wp-config.php ] || [ -f ../wp-config.php ]; then
find . -type d -exec chmod 750 {} \;
find . -type f -exec chmod 640 {} \;
else
echo "not a wordpress directory"
fi
}
alias transfer=transfer_vtbox_net
alias magento-upgrade=MAGENTO_UPGRADE
alias cheat=CHEAT_CHECK
#alias wp='/usr/bin/wp --allow-root'
alias gpigz=compress_pigz
alias gunpigz=decompress_pigz
alias ee-bashrc-update=update_git_mybashrc
alias rsync='rsync -avz --human-readable --progress'
alias rsync_hard='rsync -rLptgoD --human-readable --progress'
alias ee-syslog='tail -n 250 /var/log/syslog | ccze -A'
alias compile-nginx-ee=EE_NGINX_COMPILE
alias ee-mysql-optimize=EE_MYSQL_OPTIMIZATION
alias showlog=EE_SHOW_LOG
alias dd-benchmark=DD_BENCHMARK_DISK
alias vb-virtualenv=_PYTHON_VIRTUALENV
# enable color support of ls and also add handy aliases
alias ls='ls --color=auto'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
# some more ls aliases
alias ll='ls -alhF'
alias la='ls -A'
alias l='ls -CF'
alias lh="stat -c '%A %a %n' ./*"
alias ld='du -sh ./* | sort -h'
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias maintenance=MAINTENANCE_APT
alias gpg-crypt=encrypt_gpg
alias gpg-decrypt=decrypt_gpg
alias show-fpm-process=EE_SHOW_FPM
alias docker-setup=EE_DOCKER_SETUP
alias ee-ls=LIST_BY_SIZE
alias tinc-up=SET_TINC_UP
alias tinc-down=SET_TINC_DOWN
alias ffmpeg-cut-start=ffmpeg_start_time
alias wp-fix-perms=_WP_PERMISSIONS
alias allservices='service --status-all'
alias gg="ping google.fr"