1
0
Fork 0
bashrc/mybashrc

867 lines
26 KiB
Plaintext
Raw Normal View History

2019-01-31 17:55:51 +01:00
#!/bin/bash
2018-10-30 01:32:44 +01:00
# FUNCTIONS
2018-09-21 15:46:34 +02:00
2019-11-19 18:08:28 +01:00
if [ "$(id -u)" = "0" ]; then
2019-11-19 18:11:34 +01:00
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;36m\]\h \[\033[01;33m\]\w \[\033[01;35m\]\$ \[\033[00m\]'
2019-11-19 18:08:28 +01:00
else
2019-11-19 18:11:34 +01:00
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[01;33m\]@\[\033[01;36m\]\h \[\033[01;33m\]\w \[\033[01;35m\]\$ \[\033[00m\]'
2019-11-19 18:08:28 +01:00
fi
2019-01-29 15:11:15 +01:00
if [ "$(id -u)" != "0" ]; then
2020-04-24 15:57:51 +02:00
IS_SUDOERS=$(groups | grep -q sudo)
2019-06-08 10:36:39 +02:00
if [ -n "$(command -v sudo)" ] && [ -n "$IS_SUDOERS" ]; then
NEED_SUDO="$(command -v sudo)"
2019-01-29 15:11:15 +01:00
else
NEED_SUDO=""
fi
2019-01-29 15:11:15 +01:00
fi
command_exists() {
2019-10-28 17:44:14 +01:00
command -v "$@" >/dev/null 2>&1
}
2019-11-13 13:53:47 +01:00
_run() {
if [ -n "$2" ]; then
echo -ne "${TPUT_ECHO}${2}${TPUT_RESET}\t"
fi
2019-11-13 13:57:46 +01:00
if ! { "$1"; }; then
2019-11-13 13:53:47 +01:00
if [ -n "$2" ]; then
echo -e "${TPUT_FAIL}[KO]${TPUT_RESET}"
fi
else
if [ -n "$2" ]; then
echo -e "[${TPUT_OK}OK${TPUT_RESET}]"
fi
fi
}
2019-10-18 17:24:15 +02:00
apt_install() {
2020-04-24 15:57:51 +02:00
DEBIAN_FRONTEND=noninteractive apt-get install --option=Dpkg::options::=--force-confmiss --option=Dpkg::options::=--force-confold "$@" --assume-yes >/dev/null 2>&1
2019-10-18 17:24:15 +02:00
}
2018-08-27 19:24:30 +02:00
transfer_vtbox_net() {
2019-11-13 13:53:47 +01:00
{ curl --progress-bar --upload-file "$1" "https://transfer.vtbox.net/$(basename $1)" && echo ""; } | tee -a $HOME/.transfer.log && echo ""
2018-08-23 16:26:41 +02:00
}
2018-08-27 19:08:06 +02:00
2018-09-01 15:12:05 +02:00
compress_pigz() {
2019-01-31 17:55:51 +01:00
tar -I pigz -cvf "$1.tar.gz" "$1"
2018-09-01 15:12:05 +02:00
}
decompress_pigz() {
2019-01-09 01:02:50 +01:00
tar -I pigz -xvf "$1"
2018-09-01 15:12:05 +02:00
}
2018-08-27 19:08:06 +02:00
2018-09-13 17:12:54 +02:00
update_git_mybashrc() {
2019-05-02 16:33:46 +02:00
if [ -d "$HOME/.mybashrc/.git" ]; then
git -C "$HOME/.mybashrc" pull -q
2019-10-28 17:44:14 +01:00
$HOME/.mybashrc/setup.sh >/dev/null 2>&1
2019-04-10 14:30:19 +02:00
fi
2018-09-13 17:12:54 +02:00
}
2018-10-09 11:30:55 +02:00
EE_MYSQL_OPTIMIZATION() {
2019-04-10 14:30:19 +02:00
2019-05-02 16:33:46 +02:00
if [ -f "$HOME/.my.cnf" ]; then
2019-01-29 15:11:15 +01:00
/usr/bin/mysqlcheck -Aos --auto-repair
2019-04-10 14:30:19 +02:00
elif [ -f /etc/psa/.psa.shadow ]; then
MYSQL_PWD="$(cat /etc/psa/.psa.shadow)" /usr/bin/mysqlcheck -Aos -uadmin --auto-repair
2019-01-29 15:11:15 +01:00
else
echo "$HOME/.my.cnf or /etc/psa/.psa.shadow doesn't exist"
2018-10-30 01:32:44 +01:00
fi
2018-10-09 11:30:55 +02:00
}
2018-10-16 16:26:57 +02:00
encrypt_gpg() {
2019-01-09 01:02:50 +01:00
gpg -c "$1"
2018-10-16 16:26:57 +02:00
}
decrypt_gpg() {
2019-01-09 01:02:50 +01:00
gpg --output "${1%.gpg}" -d "$1"
2018-10-16 16:26:57 +02:00
}
2018-11-05 10:17:26 +01:00
LIST_BY_SIZE() {
2019-01-31 17:55:51 +01:00
du -sh ./* | sort -h
2018-11-05 10:17:26 +01:00
}
2018-11-04 14:44:06 +01:00
EE_DOCKER_SETUP() {
2019-05-02 16:33:46 +02:00
if [ ! -d "$HOME/.ee" ]; then
2019-01-09 01:02:50 +01:00
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
2019-04-10 14:30:19 +02:00
2018-11-04 14:44:06 +01:00
}
2018-11-13 12:32:28 +01:00
SET_TINC_UP() {
2019-04-10 14:30:19 +02:00
2019-01-09 01:02:50 +01:00
sudo tincd -n "$1"
2019-04-10 14:30:19 +02:00
2018-11-13 12:32:28 +01:00
}
2018-11-13 12:33:20 +01:00
SET_TINC_DOWN() {
2019-04-10 14:30:19 +02:00
2019-01-09 01:02:50 +01:00
sudo tincd -n "$1" -k
2019-04-10 14:30:19 +02:00
2018-11-13 12:32:28 +01:00
}
2018-12-21 16:54:02 +01:00
DD_BENCHMARK_DISK() {
2019-04-10 14:30:19 +02:00
2019-01-09 01:02:50 +01:00
dd if=/dev/zero bs=1024 count=1000000 of=file_1GB
dd if=file_1GB of=/dev/null bs=1024
rm file_1GB
2019-04-10 14:30:19 +02:00
2018-12-21 16:54:02 +01:00
}
2019-04-03 15:13:57 +02:00
RANDOM_GIT_COMMIT() {
2019-08-15 00:07:59 +02:00
[ -z "$(command -v w3m)" ] && {
2019-04-10 14:30:19 +02:00
apt-get install -y w3m
}
2019-04-03 15:13:57 +02:00
2019-04-10 14:30:19 +02:00
git add .
git commit -m "$(w3m whatthecommit.com | head -1)"
2019-04-03 15:13:57 +02:00
}
GIT_COMMIT() {
2019-04-10 14:30:19 +02:00
git add .
git commit -am "$1"
2019-04-03 15:13:57 +02:00
}
2019-05-02 16:21:30 +02:00
_find_duplicates() {
2019-09-05 14:41:05 +02:00
if [ "$#" = "0" ]; then
2019-05-02 16:21:30 +02:00
echo "duplicate-find <path> [--force]"
echo "use --force to delete files"
2019-03-11 18:22:05 +01:00
else
if ! command_exists rdfind; then
2019-05-02 16:21:30 +02:00
apt-get install rdfind -y
fi
if [ "$2" = "--force" ]; then
rdfind -ignoreempty false -deleteduplicates true "$1"
2019-05-02 16:21:30 +02:00
else
rdfind -dryrun true -ignoreempty false "$1"
fi
2019-03-11 18:22:05 +01:00
fi
}
2019-01-31 15:34:17 +01:00
2018-08-27 19:24:30 +02:00
MAINTENANCE_APT() {
2019-04-10 14:30:19 +02:00
2018-09-13 17:26:38 +02:00
# Colors
2018-09-13 17:10:05 +02:00
# Colors
CSI='\033['
CEND="${CSI}0m"
2018-09-13 17:26:38 +02:00
CGREEN="${CSI}1;32m"
2019-04-10 14:30:19 +02:00
2019-01-30 04:54:45 +01:00
if [ "$(id -u)" = "0" ] || [ -n "$IS_SUDOERS" ]; then
2019-08-26 03:54:48 +02:00
export DEBIAN_FRONTEND=noninteractive
2018-09-13 17:28:06 +02:00
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT UPDATE '
2018-09-13 17:28:06 +02:00
echo -e "${CGREEN}#############################################${CEND}"
2019-10-18 17:24:15 +02:00
if ! {
$NEED_SUDO apt-get update --allow-releaseinfo-change
2019-10-11 12:22:56 +02:00
}; then
2019-10-18 17:24:15 +02:00
$NEED_SUDO apt-get update
2019-10-11 12:22:56 +02:00
fi
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT FULL-UPGRADE '
echo -e "${CGREEN}#############################################${CEND}"
2019-10-11 12:22:56 +02:00
$NEED_SUDO apt-get --option=Dpkg::options::=--force-confmiss --option=Dpkg::options::=--force-confold --option=Dpkg::options::=--force-unsafe-io -y dist-upgrade
2018-09-13 17:28:06 +02:00
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT-GET AUTOREMOVE '
2018-09-13 17:28:06 +02:00
echo -e "${CGREEN}#############################################${CEND}"
2019-08-26 03:54:48 +02:00
$NEED_SUDO apt-get -o Dpkg::Options::="--force-confmiss" -o Dpkg::Options::="--force-confold" -y --purge autoremove
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' APT AUTOCLEAN '
echo -e "${CGREEN}#############################################${CEND}"
2019-08-26 03:54:48 +02:00
$NEED_SUDO apt-get -o Dpkg::Options::="--force-confmiss" -o Dpkg::Options::="--force-confold" -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}"
2019-03-14 16:02:41 +01:00
echo -e ' CLEAN DEINSTALLED PACKAGES '
echo -e "${CGREEN}#############################################${CEND}"
$NEED_SUDO dpkg --get-selections | grep deinstall | cut -f1 | xargs dpkg --purge
2018-08-27 19:24:30 +02:00
fi
if [ "$1" = "--docker" ]; then
if command_exists 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
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 '{}' \;
2018-08-27 19:24:30 +02:00
fi
2019-01-09 01:02:50 +01:00
echo -e "${CGREEN}#############################################${CEND}"
echo -e ' EE-BASHRC UPDATE '
2019-01-09 01:02:50 +01:00
echo -e "${CGREEN}#############################################${CEND}"
update_git_mybashrc
2019-01-29 15:11:15 +01:00
else
echo "you need to be root or sudoers to launch the maintenance"
fi
2019-04-10 14:30:19 +02:00
2018-08-27 19:24:30 +02:00
}
2018-10-07 16:56:41 +02:00
EE_NGINX_COMPILE() {
2018-12-27 15:48:11 +01:00
if [ ! -d $HOME/.scripts ]; then
mkdir $HOME/.scripts
2018-10-30 01:32:44 +01:00
fi
2018-12-27 15:48:11 +01:00
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
2019-01-09 01:02:50 +01:00
$HOME/.scripts/nginx-build.sh "$@"
2018-10-07 16:56:41 +02:00
}
2018-10-30 01:25:10 +01:00
EE_SHOW_LOG() {
2019-04-10 14:30:19 +02:00
2019-11-13 13:53:47 +01:00
if ! command_exists ccze; then
2019-04-10 14:30:19 +02:00
apt install ccze -y
2019-11-13 13:53:47 +01:00
fi
2019-11-13 14:03:21 +01:00
if echo "$1" | grep -q ".gz"; then
2019-11-13 13:53:47 +01:00
zcat "$1" | ccze -A -p syslog -C
else
tail -n 500 "$1" | ccze -A -p syslog -C
2019-11-13 13:53:47 +01:00
fi
2019-04-10 14:30:19 +02:00
2019-01-29 15:11:15 +01:00
}
2019-02-20 17:55:29 +01:00
_PYTHON_VIRTUALENV() {
2019-04-10 14:30:19 +02:00
python3 -m venv "$1"
2019-02-20 17:55:29 +01:00
source "$1/bin/activate"
2019-01-09 01:02:50 +01:00
}
2018-10-30 01:25:10 +01:00
2019-01-09 01:02:50 +01:00
CHEAT_CHECK() {
2019-01-09 01:07:34 +01:00
{
2019-04-10 14:30:19 +02:00
CHECK_CHT=$(command -v cht.sh)
[ -z "$CHECK_CHT" ] && {
wget -O /usr/local/bin/cht.sh https://cht.sh/:cht.sh
chmod +x /usr/local/bin/cht.sh
}
2019-04-10 15:31:14 +02:00
ND_CHECK_CHT="$(command -v cht.sh)"
$ND_CHECK_CHT "$@"
2019-01-09 01:07:34 +01:00
}
2019-04-10 14:30:19 +02:00
2018-10-30 01:25:10 +01:00
}
2018-10-30 18:12:41 +01:00
EE_SHOW_FPM() {
2019-04-10 14:30:19 +02:00
2019-02-02 02:30:31 +01:00
top -bn1 | grep -c "php-fpm"
2019-04-10 14:30:19 +02:00
2018-10-30 18:12:41 +01:00
}
MAGENTO_UPGRADE() {
2019-04-10 14:30:19 +02:00
if [ -x bin/magento ]; then
bin/magento maintenance:enable
bin/magento cache:flush
bin/magento setup:upgrade
2019-04-10 14:30:19 +02:00
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
2019-04-10 14:30:19 +02:00
2018-10-30 18:12:41 +01:00
}
2019-03-11 18:22:05 +01:00
_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
}
2018-09-21 15:46:34 +02:00
2019-04-12 02:59:01 +02:00
_PERM_FILES() {
2019-05-02 16:33:46 +02:00
find . -type f -exec chmod "$1" {} \;
2019-04-12 02:59:01 +02:00
}
_PERM_FOLDER() {
2019-05-02 16:33:46 +02:00
find . -type d -exec chmod "$1" {} \;
2019-04-12 02:59:01 +02:00
}
2019-04-25 01:49:14 +02:00
_NGINX_EE() {
wget -qO /tmp/nginx-ee vtb.cx/nginx-ee
chmod +x /tmp/nginx-ee
/tmp/nginx-ee "$@"
}
2019-05-02 16:21:30 +02:00
_INSTALL_NODEJS() {
if [ "$#" -eq 0 ]; then
echo "Usage : setup-nodejs <version>"
echo "Example : setup-nodejs 12"
2019-05-02 16:21:30 +02:00
else
wget -O nodejs.sh https://deb.nodesource.com/setup_"$1".x
2020-08-10 16:24:59 +02:00
chmod +x nodejs.sh
./nodejs.sh
rm -f nodejs.sh
$NEED_SUDO apt-get install -y nodejs
2019-05-02 16:21:30 +02:00
fi
}
_UPDATE_NPM() {
$NEED_SUDO npm install -g npm
}
2019-06-08 10:36:39 +02:00
_PPA_INSTALL() {
if [ "$#" -eq 0 ]; then
echo "Usage : IPPA <ppa-name>"
echo " PPA : "
2019-07-11 18:01:40 +02:00
echo " - ubuntu backports : --jonathonf"
echo " - virtubox backports : --backports"
2019-06-08 10:36:39 +02:00
echo " - ondrej php : --php"
echo " - ondrej nginx : --nginx"
2019-07-11 18:01:40 +02:00
echo " - WordOps nginx : --nginx-wo"
2019-06-08 10:36:39 +02:00
echo " - ondrej apache : --apache"
echo " - redis-server : --redis"
echo " - ffmpeg4 : --ffmpeg"
echo " - gcc8 : --gcc"
2019-07-11 18:01:40 +02:00
echo " - tinc : --tinc"
2019-09-22 01:41:51 +02:00
echo " - goaccess : --goaccess"
2019-11-18 13:37:31 +01:00
echo " - handbrake : --handbrake"
2019-06-08 10:36:39 +02:00
else
PPA=""
while [ "$#" -gt 0 ]; do
case "$1" in
2019-07-11 18:01:40 +02:00
--jonathonf)
2019-08-15 12:49:59 +02:00
PPA="$PPA ppa:jonathonf/backports"
2019-06-08 10:36:39 +02:00
;;
2019-07-11 18:01:40 +02:00
--backports)
2019-08-15 12:49:59 +02:00
PPA="$PPA ppa:virtubox/backports"
2019-07-11 18:01:40 +02:00
;;
2019-06-08 10:36:39 +02:00
--php)
2019-08-15 12:49:59 +02:00
PPA="$PPA ppa:ondrej/php"
2019-06-08 10:36:39 +02:00
;;
--nginx)
2019-08-15 12:49:59 +02:00
PPA="$PPA ppa:ondrej/nginx-mainline"
2019-06-08 10:36:39 +02:00
;;
2019-07-11 18:01:40 +02:00
--nginx-wo)
2019-08-15 12:49:59 +02:00
PPA="$PPA ppa:virtubox/nginx-wo"
2019-07-11 18:01:40 +02:00
;;
2019-06-08 10:36:39 +02:00
--apache)
2019-08-15 12:49:59 +02:00
PPA="$PPA ppa:ondrej/apache2"
2019-06-08 10:36:39 +02:00
;;
--redis)
2019-08-15 12:49:59 +02:00
PPA="$PPA ppa:chris-lea/redis-server"
2019-06-08 10:36:39 +02:00
;;
--ffmpeg)
2019-08-15 12:49:59 +02:00
PPA="$PPA ppa:jonathonf/ffmpeg-4"
2019-06-08 10:36:39 +02:00
;;
--gcc)
2019-08-15 12:49:59 +02:00
PPA="$PPA ppa:jonathonf/gcc"
2019-06-08 10:36:39 +02:00
;;
2019-07-11 18:01:40 +02:00
--tinc)
2019-08-15 12:49:59 +02:00
PPA="$PPA ppa:virtubox/tinc"
2019-07-11 18:01:40 +02:00
;;
2019-09-22 01:41:51 +02:00
--goaccess)
PPA="$PPA ppa:alex-p/goaccess"
;;
2019-11-18 13:37:31 +01:00
--handbrake)
PPA="$PPA ppa:stebbins/handbrake-git-snapshots"
;;
2019-06-08 10:36:39 +02:00
*) ;;
esac
shift
done
if [ -n "$PPA" ]; then
for UPPA in $PPA; do
$NEED_SUDO add-apt-repository "$UPPA" -y
done
2019-06-08 10:36:39 +02:00
fi
fi
}
2018-10-25 12:45:36 +02:00
2019-07-04 13:13:54 +02:00
_INSTALL_NANORC() {
2019-09-23 16:06:28 +02:00
if [ ! -d /usr/share/nano-syntax-highlighting ]; then
git clone --depth 1 https://github.com/scopatz/nanorc.git /usr/share/nano-syntax-highlighting -q
fi
if ! grep -q "/usr/share/nano-syntax-highlighting" /etc/nanorc; then
2019-10-28 17:44:14 +01:00
echo "include /usr/share/nano-syntax-highlighting/*.nanorc" >>/etc/nanorc
2019-09-23 16:06:28 +02:00
fi
2019-07-04 13:13:54 +02:00
}
2019-07-19 16:12:55 +02:00
_SYSINFO() {
# Reading out system information...
# Reading CPU model
cname=$(awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')
# Reading amount of CPU cores
cores=$(awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo)
# Reading CPU frequency in MHz
freq=$(awk -F: ' /cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')
# Reading total memory in MB
tram=$(free -m | awk 'NR==2 {print $2}')
# Reading Swap in MB
vram=$(free -m | awk 'NR==4 {print $2}')
# Reading system uptime
up=$(uptime | awk '{ $1=$2=$(NF-6)=$(NF-5)=$(NF-4)=$(NF-3)=$(NF-2)=$(NF-1)=$NF=""; print }' | sed 's/^[ \t]*//;s/[ \t]*$//')
# Reading operating system and version (simple, didn't filter the strings at the end...)
opsy=$(cat /etc/issue.net | awk 'NR==1 {print}') # Operating System & Version
arch=$(uname -m) # Architecture
lbit=$(getconf LONG_BIT) # Architecture in Bit
hn=$(hostname -f) # Hostname
kern=$(uname -r)
echo ""
# Output of results
echo "System Info"
echo "Server : $hn"
echo "-----------"
echo "Processor : $cname"
echo "CPU Cores : $cores"
echo "Frequency : $freq MHz"
echo "Memory : $tram MB"
echo "Swap : $vram MB"
echo "Uptime : $up"
echo "-----------"
echo "OS : $opsy"
echo "Arch : $arch ($lbit Bit)"
echo "Kernel : $kern"
echo ""
}
2019-07-21 17:49:56 +02:00
_SITESPEED() {
if [ "$#" = "0" ]; then
2019-07-30 11:51:49 +02:00
echo "Usage : sitespeed <url>"
2019-07-21 17:49:56 +02:00
else
curl -s -w \
2019-11-08 03:45:49 +01:00
'\nLookup time:\t\t%{time_namelookup}\nConnect time:\t\t%{time_connect}\nSSL handshake time:\t%{time_appconnect}\nPre-Transfer time:\t%{time_pretransfer}\nRedirect time:\t\t%{time_redirect}\nStart transfer time:\t%{time_starttransfer}\n\nTotal time:\t\t%{time_total}\n' -o /dev/null "$@"
2019-07-21 17:49:56 +02:00
fi
}
2019-07-19 16:12:55 +02:00
2019-07-23 19:59:54 +02:00
_SETUP_TOOLS() {
2019-07-30 11:51:49 +02:00
DEBIAN_FRONTEND=noninteractive $NEED_SUDO apt-get -o Dpkg::Options::="--force-confmiss" -o Dpkg::Options::="--force-confold" -y install haveged curl git unzip zip htop nload nmon ntp gnupg gnupg2 wget pigz tree ccze
2019-07-23 19:59:54 +02:00
}
2019-07-29 09:19:28 +02:00
_RSYSLOG_UFW() {
2019-07-30 11:51:49 +02:00
[ -f /etc/rsyslog.d/20-ufw.conf ] && {
sed -i 's/\#\& stop/\& stop/' /etc/rsyslog.d/20-ufw.conf
service rsyslog restart
}
}
_START_SSH_AGENT() {
2019-11-18 13:37:31 +01:00
rm -f /root/.ssh/ssh_auth_sock
ssh-agent -a /root/.ssh/ssh_auth_sock
export SSH_AUTH_SOCK="/root/.ssh/ssh_auth_sock"
ssh-add -l >/dev/null || ssh-add
2019-07-29 09:19:28 +02:00
}
2019-07-30 18:10:15 +02:00
_APT_REPO_UBUNTU() {
wget -O /etc/apt/sources.list https://vtb.cx/$(lsb_release -sc)-list
}
2019-08-04 11:37:31 +02:00
_APT_BIONIC_KERNEL() {
if [ "$(lsb_release -sc)" = "bionic" ]; then
apt update && apt install --install-recommends linux-generic-hwe-18.04 --assume-yes
fi
}
2019-08-06 17:23:42 +02:00
_FTP_ADD() {
2019-08-15 00:07:59 +02:00
if [ "$#" = "0" ] || [ "$#" = "1" ]; then
echo "Usage : ftpadd <user> <domain>"
else
2019-10-18 17:32:44 +02:00
if ! command_exists pwgen; then
2020-04-24 15:57:51 +02:00
$NEED_SUDO apt-get install pwgen -y >/dev/null 2>&1
2019-10-18 17:32:44 +02:00
fi
2019-10-18 17:24:15 +02:00
if [ -d /var/www/"$2"/htdocs ]; then
2020-01-02 12:31:59 +01:00
ftpaccountpass=$(_PWGEN)
2019-11-14 16:04:09 +01:00
$NEED_SUDO useradd -d "/var/www/$2/htdocs" -M -s /bin/false -G www-data "$1"
2019-10-18 17:24:15 +02:00
echo "$1:$ftpaccountpass" | $NEED_SUDO chpasswd -m
2019-11-14 16:04:09 +01:00
chmod -R g+rw "/var/www/$2/htdocs"
2020-01-02 12:31:59 +01:00
echo -e "\n\n[/var/www/$2/htdocs]" | tee -a "$HOME/.ftpaccounts"
2019-10-18 17:32:44 +02:00
echo -e "user : $1" | tee -a "$HOME/.ftpaccounts"
echo "password : $ftpaccountpass" | tee -a "$HOME/.ftpaccounts"
2019-10-18 17:24:15 +02:00
else
echo "site directory doesn't exist"
fi
2019-08-15 00:07:59 +02:00
fi
2019-08-06 17:23:42 +02:00
}
_UFW_MINIMAL() {
ufw logging low
ufw default allow outgoing
ufw default deny incoming
ufw limit 22
ufw limit 10022
ufw allow 53
ufw allow http
ufw allow https
ufw allow 68
ufw allow 655
ufw allow 873
ufw allow 123
ufw allow 22222
ufw allow from 144.76.159.118 to any port 35621
ufw allow from 144.76.159.118 to any port 35622
ufw allow from 144.76.159.118 to any port 35623
ufw allow from 159.69.0.216 to any port 10050
}
_SSH_SECURE() {
2019-08-06 17:26:56 +02:00
CURRENT_SSH_PORT=$(grep "Port" /etc/ssh/sshd_config | awk -F " " '{print $2}')
2019-08-06 17:23:42 +02:00
wget https://raw.githubusercontent.com/VirtuBox/ubuntu-nginx-web-server/master/etc/ssh/sshd_config -O /etc/ssh/sshd_config
if [ -n "$1" ]; then
sed -i "s/Port 22/Port $1/" /etc/ssh/sshd_config
else
2019-08-06 17:26:56 +02:00
sed -i "s/Port 22/Port $CURRENT_SSH_PORT/" /etc/ssh/sshd_config
2019-08-06 17:23:42 +02:00
fi
}
2019-08-15 00:07:59 +02:00
_PWGEN() {
2019-10-18 17:24:15 +02:00
if ! command_exists pwgen; then
apt_install pwgen
2019-08-15 00:07:59 +02:00
fi
pwgen -s 24 1
}
2019-08-26 03:54:48 +02:00
_FD() {
if [ -z "$(command -v fd)" ]; then
echo "downloading fd ..."
2019-10-28 17:44:14 +01:00
wget -qO https://github.com/sharkdp/fd/releases/download/v7.3.0/fd-musl_7.3.0_amd64.deb -O /tmp/fd.deb >/dev/null
2019-08-26 03:54:48 +02:00
echo "installing fd ..."
2019-10-28 17:44:14 +01:00
dpkg -i /tmp/fd.deb && rm /tmp/db.deb >/dev/null
2019-08-26 03:54:48 +02:00
fi
fd "$@"
}
2019-09-23 16:06:28 +02:00
_LXC_LOGIN() {
2019-09-23 16:09:51 +02:00
lxc exec "$@" /usr/bin/env bash
2019-09-23 16:06:28 +02:00
}
2019-09-23 17:25:09 +02:00
_LXC_LAUNCH() {
local RANDOM
RANDOM=$(date +%s | sha256sum | base64 | head -c 4)
if [ "$#" = "0" ]; then
lxc launch ubuntu-daily:18.04 "ctn-$RANDOM"
else
lxc launch ubuntu-daily:18.04 "$1"
fi
}
2019-09-24 12:43:02 +02:00
_BACKPORT_PACKAGE() {
if [ "$#" = "0" ]; then
echo "Usage :"
echo "bppackage <ppa (optional)> <.dsc url> "
echo "exemple : bppackage ppa:virtubox/backports http://url.dsc"
fi
local RANDOM
RANDOM=$(date +%s | sha256sum | base64 | head -c 4)
if [ "$PWD" = "/root" ]; then
mkdir -p "backport-$RANDOM"
cd "backport-$RANDOM" || exit 1
fi
if [ "$#" = "1" ]; then
backportpackage -r -w . -d bionic -u ppa:virtubox/backports -kE3CC41E7F354756B94A7DF4322EB296C97BAD476 "$1"
elif [ "$#" = "2" ]; then
backportpackage -r -w . -d bionic -u "$1" -kE3CC41E7F354756B94A7DF4322EB296C97BAD476 "$2"
fi
}
2019-10-23 18:30:39 +02:00
_WO_FULL_UPGRADE() {
[ ! -f "$HOME/.gitconfig" ] && {
sudo bash -c 'echo -e "[user]\n\tname = $USER\n\temail = root@wops.cc" > $HOME/.gitconfig'
}
2019-10-28 17:44:14 +01:00
rm -f wo
2019-10-28 17:55:55 +01:00
wget -qO wo https://raw.githubusercontent.com/WordOps/WordOps/updating-configuration/install && sudo bash wo -b updating-configuration
2019-10-23 18:30:39 +02:00
}
2019-11-05 14:02:38 +01:00
_SPEEDTEST() {
if ! command_exists speedtest; then
DEB_DISTRO=$(lsb_release -sc)
2019-11-13 13:53:47 +01:00
apt-get install gnupg1 apt-transport-https dirmngr -y
2019-11-05 14:02:38 +01:00
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 379CE192D401AB61
2019-11-13 13:53:47 +01:00
echo "deb https://ookla.bintray.com/debian $DEB_DISTRO main" | sudo tee /etc/apt/sources.list.d/speedtest.list
2019-11-05 14:02:38 +01:00
apt-get update
2019-11-13 13:53:47 +01:00
apt-get install speedtest -y
2019-11-05 14:02:38 +01:00
fi
speedtest
}
2019-11-07 14:26:37 +01:00
_INSTALL_WPCLI() {
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
$NEED_SUDO mv wp-cli.phar /usr/local/bin/wp
wget -O /etc/bash_completion.d/wp-completion.bash https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash
}
_SETUP_CONTROLLER() {
if [ "$#" -eq 0 ]; then
echo "Usage : SSETUP <ppa-name>"
echo " PPA : "
echo " - nanorc : --nanorc"
echo " - wpcli : --wpcli"
else
INSTALL=""
while [ "$#" -gt 0 ]; do
case "$1" in
--nanorc)
INSTALL="$INSTALL _INSTALL_NANORC"
;;
--wpcli)
INSTALL="$INSTALL _INSTALL_WPCLI"
;;
*) ;;
esac
shift
done
if [ -n "$INSTALL" ]; then
for APP in $INSTALL; do
$NEED_SUDO $APP
done
fi
fi
}
2019-11-14 16:04:09 +01:00
_ZABBIX_UPDATE() {
local distro
2019-12-27 20:48:33 +01:00
if ! command_exists lsb_release; then
apt install lsb-release -y >/dev/null
fi
2019-11-14 16:04:09 +01:00
distro="$(lsb_release -si)"
2019-11-14 16:06:14 +01:00
wget -O /tmp/zabbix.deb https://repo.zabbix.com/zabbix/4.4/"${distro,,}"/pool/main/z/zabbix-release/zabbix-release_4.4-1+"$(lsb_release -sc)"_all.deb
2019-11-14 16:04:09 +01:00
dpkg -i /tmp/zabbix.deb
rm -f /tmp/zabbix.deb
}
2019-11-19 18:04:54 +01:00
_BACK_FILE() {
if [ ! -f "$1.bak" ]; then
cp -f "$1" "$1.bak"
else
cp -f "$1" "$1_1.bak"
fi
}
2019-11-26 12:50:03 +01:00
_URBACKUP_CLIENT() {
TF=$(mktemp) && wget "https://hndl.urbackup.org/Client/2.4.9/UrBackup%20Client%20Linux%202.4.9.sh" -O "$TF" && sudo sh "$TF"
rm -f "$TF"
sed -i 's/RESTORE=disabled/RESTORE="server-confirms"/' /etc/default/urbackupclient
wget -O "$HOME/mysqldump.sh" vtb.cx/mysqldump
chmod +x "$HOME/mysqldump.sh"
if command_exists ufw; then
ufw allow from 144.76.159.118 to any port 35621
ufw allow from 144.76.159.118 to any port 35622
ufw allow from 144.76.159.118 to any port 35623
fi
}
2019-11-29 13:36:09 +01:00
_DELETE_WO_SITES() {
sites=$(wo site list 2>&1)
for site in $sites; do
echo -ne " deleting $site [..]\r"
if {
wo site delete $site --force
2019-11-29 13:38:24 +01:00
}; then
echo -e '\t[OK]'
2019-11-29 13:36:09 +01:00
fi
done
}
ffmpeg_start_time() {
if [ "$#" = "0" ]; then
echo "ffmpeg-cut-start <start-time> <input> <duration(optional)>"
echo ""
echo "example : ffmpeg-cut-start 00:34 file.mp4 300"
else
if [ -n "$3" ]; then
bash -c "ffmpeg -ss '$1' -t '$3' -y -i '$2' -c copy -movflags +faststart '${2%.mp4}-output.mp4';"
else
bash -c "ffmpeg -ss '$1' -y -i '$2' -c copy -movflags +faststart '${2%.mp4}-output.mp4';"
fi
fi
}
2019-12-16 12:48:51 +01:00
_STABILISE_VIDEO() {
2019-12-16 14:14:15 +01:00
for i in "$PWD"/*; do
echo "Processing vid $i ...${i%.*}"
2020-06-12 20:33:40 +02:00
ffmpeg -i "$i" -vf vidstabdetect=stepsize=6:shakiness=5:accuracy=15:result=/tmp/transform_vectors.trf -f null -
2019-12-27 20:48:33 +01:00
ffmpeg -i "$i" -vf vidstabtransform=input=/tmp/transform_vectors.trf:zoom=2:smoothing=10,unsharp=5:5:0.8:3:3:0.4 -c:v libx264 -preset slow -crf 18 -c:a copy -pix_fmt yuv420p -movflags +faststart "${i%.*}-stable.mp4"
2019-12-16 14:14:15 +01:00
rm /tmp/transform_vectors.trf
2019-12-20 13:39:07 +01:00
rm -f "$i"
2019-12-16 14:14:15 +01:00
done
2019-12-16 12:51:56 +01:00
2019-12-16 12:48:51 +01:00
}
2020-05-08 19:31:52 +02:00
_MP4_CONVERT() {
for i in "$PWD"/*; do
echo "Processing vid $i ...${i%.*}"
2020-05-10 13:18:29 +02:00
ffmpeg -i "$i" -c:v libx264 -preset slow -crf 18 -c:a aac -b:a 192k -pix_fmt yuv420p -movflags +faststart "${i%.*}-converted.mp4"
2020-05-08 19:31:52 +02:00
rm -f "$i"
done
}
2019-12-16 14:09:49 +01:00
_YOUTUBE_ENCODE() {
2019-12-16 14:14:15 +01:00
for i in "$PWD"/*; do
ffmpeg -i "$i" -c:v libx264 -preset slow -crf 18 -c:a aac -b:a 192k -pix_fmt yuv420p -movflags +faststart "${i%.*}-yt.mp4"
2019-12-16 14:09:49 +01:00
done
}
2020-07-24 14:29:54 +02:00
_MERGE_VIDEO() {
2020-07-24 14:46:28 +02:00
rm -f mylist
2020-07-24 14:29:54 +02:00
if [ "$#" -eq 0 ]; then
2020-07-24 14:46:28 +02:00
for i in "$PWD"/*; do
echo "file '$i'" >>mylist.txt
done
2020-07-24 14:29:54 +02:00
else
videos="$*"
2020-07-24 14:46:28 +02:00
for video in $videos; do
echo "file '$video'" >>mylist.txt
done
2020-07-24 14:29:54 +02:00
fi
ffmpeg -f concat -safe 0 -i mylist.txt -c copy "output-merged.mp4"
}
2020-07-29 13:40:31 +02:00
_TRANSFER_AUDIO() {
if [ "$#" -eq 0 ]; then
echo "Usage : ffmpeg-audio <file-with-audio> <file-without-audio>"
else
rm -f "${1%.*}.aac"
ffmpeg -i "$1" -vn -acodec copy "${1%.*}.aac"
if [ -f "${1%.*}.aac" ]; then
rm -f "${2%.*}-sound.mp4"
ffmpeg -i "$2" -i "${1%.*}.aac" -map 0:v -map 1:a -c copy -y "${2%.*}-sound.mp4"
fi
fi
}
2020-07-30 15:08:03 +02:00
_x265_encode() {
2020-08-10 18:10:06 +02:00
if [ "$#" -eq 0 ]; then
echo "Usage : ffmpeg-x265 <file> <options>"
echo "Options :"
echo " -d : process all files in the current directory"
elif [ "$#" -eq 1 ]; then
if [ "$1" = "-d" ]; then
for i in "$PWD"/*; do
ffmpeg -i "$i" -c:v libx265 -crf 28 -c:a aac -b:a 160k "${i%.*}-x265.mp4"
done
else
if [ -f "$1" ]; then
ffmpeg -i "$1" -c:v libx265 -crf 28 -c:a aac -b:a 160k "${1%.*}-x265.mp4"
fi
fi
2020-07-30 15:08:03 +02:00
fi
}
2018-10-30 01:32:44 +01:00
# enable color support of ls and also add handy aliases
2018-09-21 15:46:34 +02:00
# some more ls aliases
2019-06-08 10:36:39 +02:00
#alias wp='/usr/bin/wp --allow-root'
2019-03-11 18:22:05 +01:00
alias .....="cd ../../../.."
2019-06-08 10:36:39 +02:00
alias ....="cd ../../.."
alias ...="cd ../.."
alias ..="cd .."
alias allservices='service --status-all'
2019-09-16 16:51:35 +02:00
alias apt-bionic-kernel=_APT_BIONIC_KERNEL
alias apt-repo-ubuntu=_APT_REPO_UBUNTU
2019-06-08 10:36:39 +02:00
alias aptremove='apt-get autoremove -y --purge'
alias arsync_hard='rsync -rLptgoD --human-readable --progress'
alias arsync='rsync -avz -h --progress'
2019-09-24 12:43:02 +02:00
alias bppackage=_BACKPORT_PACKAGE
2019-06-08 10:36:39 +02:00
alias cheat=CHEAT_CHECK
alias commit=GIT_COMMIT
alias dd-benchmark=DD_BENCHMARK_DISK
alias dir='dir --color=auto'
2019-03-15 16:13:46 +01:00
alias docker-setup=EE_DOCKER_SETUP
2019-06-08 10:36:39 +02:00
alias duplicate-finder=_find_duplicates
alias ee-bashrc-update=update_git_mybashrc
2018-11-05 10:17:26 +01:00
alias ee-ls=LIST_BY_SIZE
2019-06-08 10:36:39 +02:00
alias ee-mysql-optimize=EE_MYSQL_OPTIMIZATION
alias ee-syslog='tail -n 250 /var/log/syslog | ccze -A'
alias egrep='egrep --color=auto'
2019-01-31 16:46:01 +01:00
alias ffmpeg-cut-start=ffmpeg_start_time
2020-07-24 14:29:54 +02:00
alias ffmpeg-merge=_MERGE_VIDEO
2020-07-29 13:40:31 +02:00
alias ffmpeg-audio=_TRANSFER_AUDIO
2020-07-30 15:08:03 +02:00
alias ffmpeg-x265=_x265_encode
2019-10-18 17:24:15 +02:00
alias ftpadd=_FTP_ADD
2019-06-08 10:36:39 +02:00
alias fgrep='fgrep --color=auto'
2019-03-23 18:54:43 +01:00
alias gg="ping google.fr"
2019-06-08 10:36:39 +02:00
alias gpg-crypt=encrypt_gpg
alias gpg-decrypt=decrypt_gpg
alias gpigz=compress_pigz
alias grep='grep --color=auto'
alias gunpigz=decompress_pigz
2019-09-16 16:51:35 +02:00
alias install-nanorc=_INSTALL_NANORC
alias IPPA=_PPA_INSTALL
2019-06-08 10:36:39 +02:00
alias l='ls -CF'
alias la='ls -A'
alias ld='du -sh ./* | sort -h'
alias lh="stat -c '%A %a %n' ./*"
alias ll='ls -alhF'
alias ls='ls --color=auto'
2019-09-23 16:06:28 +02:00
alias lxclogin=_LXC_LOGIN
2019-09-23 17:25:09 +02:00
alias lxclaunch=_LXC_LAUNCH
2019-06-08 10:36:39 +02:00
alias magento-upgrade=MAGENTO_UPGRADE
alias maintenance=MAINTENANCE_APT
2019-09-16 16:51:35 +02:00
alias nano='nano -E'
2019-04-12 03:00:11 +02:00
alias netdata-fix=_FIX_NETDATA
2019-04-25 01:49:14 +02:00
alias nginx-ee=_NGINX_EE
2019-06-08 10:36:39 +02:00
alias npm-update=_UPDATE_NPM
2019-09-16 16:51:35 +02:00
alias passwdgen=_PWGEN
alias pip='python3 -m pip'
2019-06-08 10:36:39 +02:00
alias random-commit=RANDOM_GIT_COMMIT
2019-09-16 16:51:35 +02:00
alias rsyslog-ufw=_RSYSLOG_UFW
2019-06-08 10:36:39 +02:00
alias setdirperm=_PERM_FOLDER
alias setfileperm=_PERM_FILES
2019-05-02 16:21:30 +02:00
alias setup-nodejs=_INSTALL_NODEJS
2019-09-16 16:51:35 +02:00
alias setup-tools=_SETUP_TOOLS
2019-06-08 10:36:39 +02:00
alias show-fpm-process=EE_SHOW_FPM
alias showlog=EE_SHOW_LOG
2019-09-16 16:51:35 +02:00
alias sitespeed=_SITESPEED
alias ssh-secure=_SSH_SECURE
alias start-ssh=_START_SSH_AGENT
alias sysinfo=_SYSINFO
2019-06-08 10:36:39 +02:00
alias tinc-down=SET_TINC_DOWN
alias tinc-up=SET_TINC_UP
alias transfer=transfer_vtbox_net
2019-09-16 16:51:35 +02:00
alias ufw-minimal=_UFW_MINIMAL
2019-06-08 10:36:39 +02:00
alias vb-virtualenv=_PYTHON_VIRTUALENV
alias vdir='vdir --color=auto'
alias wo-virtualenv=_PYTHON_VIRTUALENV
alias wp-fix-perms=_WP_PERMISSIONS
2019-10-23 18:30:39 +02:00
alias upgrade-wo-full=_WO_FULL_UPGRADE
2019-11-13 14:01:18 +01:00
alias speedtestnet="_run _SPEEDTEST 'Installing speedtest'"
2019-11-07 14:26:37 +01:00
alias SSETUP=_SETUP_CONTROLLER
2019-11-08 03:45:49 +01:00
alias journalctfail='journalctl -b -p err -S "yesterday"'
2019-11-14 16:04:09 +01:00
alias zabbix-update=_ZABBIX_UPDATE
2019-11-24 23:53:36 +01:00
alias bak=_BACK_FILE
2019-11-26 12:50:03 +01:00
alias update-wo-fast='python3 -m pip install -I "git+git://github.com/WordOps/WordOps.git@updating-configuration#egg=wordops"'
alias clone-wordops='git clone https://github.com/WordOps/WordOps.git -b updating-configuration'
2019-11-29 13:36:09 +01:00
alias urbackup-client-setup=_URBACKUP_CLIENT
2019-11-29 13:38:24 +01:00
alias delete-all-site=_DELETE_WO_SITES
2019-12-16 14:09:49 +01:00
alias stabilise-video=_STABILISE_VIDEO
2019-12-16 14:14:15 +01:00
alias yt-encode=_YOUTUBE_ENCODE
2020-05-08 19:43:45 +02:00
alias mp4convert=_MP4_CONVERT
2020-06-12 20:33:40 +02:00
alias suroot='sudo su - root'
2020-08-08 15:16:21 +02:00
alias apt-playbook='ansible-playbook /etc/ansible/playbooks/include/update-apt-servers.yml'
alias full-upgrade-playbook='ansible-playbook /etc/ansible/playbooks/update-all-servers.yml'