bash functions and aliases
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

926 lines
28 KiB

  1. #!/bin/bash
  2. # FUNCTIONS
  3. if [ "$(id -u)" = "0" ]; then
  4. 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\]'
  5. else
  6. 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\]'
  7. fi
  8. if [ "$(id -u)" != "0" ]; then
  9. IS_SUDOERS=$(groups | grep -q sudo)
  10. if [ -n "$(command -v sudo)" ] && [ -n "$IS_SUDOERS" ]; then
  11. NEED_SUDO="$(command -v sudo)"
  12. else
  13. NEED_SUDO=""
  14. fi
  15. fi
  16. command_exists() {
  17. command -v "$@" >/dev/null 2>&1
  18. }
  19. _run() {
  20. if [ -n "$2" ]; then
  21. echo -ne "${TPUT_ECHO}${2}${TPUT_RESET}\t"
  22. fi
  23. if ! { "$1"; }; then
  24. if [ -n "$2" ]; then
  25. echo -e "${TPUT_FAIL}[KO]${TPUT_RESET}"
  26. fi
  27. else
  28. if [ -n "$2" ]; then
  29. echo -e "[${TPUT_OK}OK${TPUT_RESET}]"
  30. fi
  31. fi
  32. }
  33. apt_install() {
  34. DEBIAN_FRONTEND=noninteractive apt-get install --option=Dpkg::options::=--force-confmiss --option=Dpkg::options::=--force-confold "$@" --assume-yes >/dev/null 2>&1
  35. }
  36. transfer_vtbox_net() {
  37. { curl --progress-bar --upload-file "$1" "https://transfer.vtbox.net/$(basename $1)" && echo ""; } | tee -a $HOME/.transfer.log && echo ""
  38. }
  39. compress_pigz() {
  40. tar -I pigz -cvf "$1.tar.gz" "$1"
  41. }
  42. compress_zstd() {
  43. tar -I zstd -cvf "$1.tar.zst" "$1"
  44. }
  45. decompress_pigz() {
  46. tar -I pigz -xvf "$1"
  47. }
  48. decompress_ztsd() {
  49. tar -I zstd -xvf "$1"
  50. }
  51. update_git_mybashrc() {
  52. if [ -d "$HOME/.mybashrc/.git" ]; then
  53. git -C "$HOME/.mybashrc" pull -q
  54. $HOME/.mybashrc/setup.sh >/dev/null 2>&1
  55. fi
  56. }
  57. EE_MYSQL_OPTIMIZATION() {
  58. if [ -f "$HOME/.my.cnf" ]; then
  59. /usr/bin/mysqlcheck -Aos --auto-repair
  60. elif [ -f /etc/psa/.psa.shadow ]; then
  61. MYSQL_PWD="$(cat /etc/psa/.psa.shadow)" /usr/bin/mysqlcheck -Aos -uadmin --auto-repair
  62. else
  63. echo "$HOME/.my.cnf or /etc/psa/.psa.shadow doesn't exist"
  64. fi
  65. }
  66. encrypt_gpg() {
  67. gpg -c "$1"
  68. }
  69. decrypt_gpg() {
  70. gpg --output "${1%.gpg}" -d "$1"
  71. }
  72. LIST_BY_SIZE() {
  73. du -sh ./* | sort -h
  74. }
  75. EE_DOCKER_SETUP() {
  76. if [ ! -d "$HOME/.ee" ]; then
  77. mkdir $HOME/.ee
  78. fi
  79. curl -fsSL get.docker.com -o $HOME/.ee/get-docker.sh
  80. chmod +x $HOME/.ee/get-docker.sh
  81. $HOME/.ee/get-docker.sh
  82. }
  83. SET_TINC_UP() {
  84. sudo tincd -n "$1"
  85. }
  86. SET_TINC_DOWN() {
  87. sudo tincd -n "$1" -k
  88. }
  89. DD_BENCHMARK_DISK() {
  90. dd if=/dev/zero bs=1024 count=1000000 of=file_1GB
  91. dd if=file_1GB of=/dev/null bs=1024
  92. rm file_1GB
  93. }
  94. RANDOM_GIT_COMMIT() {
  95. [ -z "$(command -v w3m)" ] && {
  96. apt-get install -y w3m
  97. }
  98. git add .
  99. git commit -m "$(w3m whatthecommit.com | head -1)"
  100. }
  101. GIT_COMMIT() {
  102. git add .
  103. git commit -am "$1"
  104. }
  105. _find_duplicates() {
  106. if [ "$#" = "0" ]; then
  107. echo "duplicate-find <path> [--force]"
  108. echo "use --force to delete files"
  109. else
  110. if ! command_exists rdfind; then
  111. apt-get install rdfind -y
  112. fi
  113. if [ "$2" = "--force" ]; then
  114. rdfind -ignoreempty false -deleteduplicates true "$1"
  115. else
  116. rdfind -dryrun true -ignoreempty false "$1"
  117. fi
  118. fi
  119. }
  120. MAINTENANCE_APT() {
  121. # Colors
  122. # Colors
  123. CSI='\033['
  124. CEND="${CSI}0m"
  125. CGREEN="${CSI}1;32m"
  126. if [ "$(id -u)" = "0" ] || [ -n "$IS_SUDOERS" ]; then
  127. export DEBIAN_FRONTEND=noninteractive
  128. echo -e "${CGREEN}#############################################${CEND}"
  129. echo -e ' APT UPDATE '
  130. echo -e "${CGREEN}#############################################${CEND}"
  131. if ! {
  132. $NEED_SUDO apt-get update --allow-releaseinfo-change
  133. }; then
  134. $NEED_SUDO apt-get update
  135. fi
  136. echo -e "${CGREEN}#############################################${CEND}"
  137. echo -e ' APT FULL-UPGRADE '
  138. echo -e "${CGREEN}#############################################${CEND}"
  139. $NEED_SUDO apt-get --option=Dpkg::options::=--force-confmiss --option=Dpkg::options::=--force-confold --option=Dpkg::options::=--force-unsafe-io -y dist-upgrade
  140. echo -e "${CGREEN}#############################################${CEND}"
  141. echo -e ' APT-GET AUTOREMOVE '
  142. echo -e "${CGREEN}#############################################${CEND}"
  143. $NEED_SUDO apt-get -o Dpkg::Options::="--force-confmiss" -o Dpkg::Options::="--force-confold" -y --purge autoremove
  144. echo -e "${CGREEN}#############################################${CEND}"
  145. echo -e ' APT AUTOCLEAN '
  146. echo -e "${CGREEN}#############################################${CEND}"
  147. $NEED_SUDO apt-get -o Dpkg::Options::="--force-confmiss" -o Dpkg::Options::="--force-confold" -y autoclean
  148. $NEED_SUDO apt-get -y clean
  149. ## clean packages in deinstall state
  150. DEINSTALLED=$($NEED_SUDO dpkg --get-selections | grep deinstall | cut -f1)
  151. if [ -n "$DEINSTALLED" ]; then
  152. echo -e "${CGREEN}#############################################${CEND}"
  153. echo -e ' CLEAN DEINSTALLED PACKAGES '
  154. echo -e "${CGREEN}#############################################${CEND}"
  155. $NEED_SUDO dpkg --get-selections | grep deinstall | cut -f1 | xargs dpkg --purge
  156. fi
  157. if [ "$1" = "--docker" ]; then
  158. if command_exists docker; then
  159. list_images=$(docker images --filter "dangling=true" -q --no-trunc)
  160. list_volumes=$(docker volume ls -qf dangling=true)
  161. if [ -n "$list_images" ]; then
  162. echo -e "${CGREEN}#############################################${CEND}"
  163. echo -e ' DOCKER IMAGES CLEANUP '
  164. echo -e "${CGREEN}#############################################${CEND}"
  165. docker rmi "$list_images"
  166. fi
  167. if [ -n "$list_volumes" ]; then
  168. echo -e "${CGREEN}#############################################${CEND}"
  169. echo -e ' DOCKER VOLUMES CLEANUP '
  170. echo -e "${CGREEN}#############################################${CEND}"
  171. docker volume rm "$list_volumes"
  172. fi
  173. fi
  174. fi
  175. OLD_LOGS=$($NEED_SUDO find /var/log/ -type f -mtime +30 -iname "*.gz")
  176. if [ -n "$OLD_LOGS" ]; then
  177. echo -e "${CGREEN}#############################################${CEND}"
  178. echo -e ' CLEANUP OLD LOGS '
  179. echo -e "${CGREEN}#############################################${CEND}"
  180. $NEED_SUDO find /var/log/ -type f -mtime +30 -iname "*.gz" -exec rm '{}' \;
  181. fi
  182. echo -e "${CGREEN}#############################################${CEND}"
  183. echo -e ' EE-BASHRC UPDATE '
  184. echo -e "${CGREEN}#############################################${CEND}"
  185. update_git_mybashrc
  186. else
  187. echo "you need to be root or sudoers to launch the maintenance"
  188. fi
  189. }
  190. EE_NGINX_COMPILE() {
  191. if [ ! -d $HOME/.scripts ]; then
  192. mkdir $HOME/.scripts
  193. fi
  194. wget -qO $HOME/.scripts/nginx-build.sh https://raw.githubusercontent.com/VirtuBox/nginx-ee/master/nginx-build.sh
  195. chmod +x $HOME/.scripts/nginx-build.sh
  196. $HOME/.scripts/nginx-build.sh "$@"
  197. }
  198. EE_SHOW_LOG() {
  199. if ! command_exists ccze; then
  200. apt install ccze -y
  201. fi
  202. if echo "$1" | grep -q ".gz"; then
  203. zcat "$1" | ccze -A -p syslog -C
  204. else
  205. tail -n 500 "$1" | ccze -A -p syslog -C
  206. fi
  207. }
  208. _PYTHON_VIRTUALENV() {
  209. python3 -m venv "$1"
  210. source "$1/bin/activate"
  211. }
  212. CHEAT_CHECK() {
  213. {
  214. CHECK_CHT=$(command -v cht.sh)
  215. [ -z "$CHECK_CHT" ] && {
  216. wget -O /usr/local/bin/cht.sh https://cht.sh/:cht.sh
  217. chmod +x /usr/local/bin/cht.sh
  218. }
  219. ND_CHECK_CHT="$(command -v cht.sh)"
  220. $ND_CHECK_CHT "$@"
  221. }
  222. }
  223. EE_SHOW_FPM() {
  224. top -bn1 | grep -c "php-fpm"
  225. }
  226. MAGENTO_UPGRADE() {
  227. if [ -x bin/magento ]; then
  228. bin/magento maintenance:enable
  229. bin/magento cache:flush
  230. bin/magento setup:upgrade
  231. rm -Rf "./generated/metadata/*" "./generated/code/*" "./var/cache/*" "./var/page_cache/*"
  232. bin/magento setup:di:compile
  233. /usr/bin/composer dump-autoload -o --apcu
  234. rm -Rf "./pub/static/frontend/*" "./pub/static/adminhtml/*" "./var/view_preprocessed/*" "./pub/static/_cache/*" "./pub/static/_requirejs/*"
  235. bin/magento setup:static-content:deploy fr_FR
  236. bin/magento setup:static-content:deploy en_US --theme="Magento/backend"
  237. bin/magento cache:enable
  238. bin/magento maintenance:disable
  239. else
  240. echo "you are not in a magento root folder"
  241. fi
  242. }
  243. _WP_PERMISSIONS() {
  244. if [ -f ./wp-config.php ] || [ -f ../wp-config.php ]; then
  245. find . -type d -exec chmod 750 {} \;
  246. find . -type f -exec chmod 640 {} \;
  247. else
  248. echo "not a wordpress directory"
  249. fi
  250. }
  251. _PERM_FILES() {
  252. find . -type f -exec chmod "$1" {} \;
  253. }
  254. _PERM_FOLDER() {
  255. find . -type d -exec chmod "$1" {} \;
  256. }
  257. _NGINX_EE() {
  258. wget -qO /tmp/nginx-ee vtb.cx/nginx-ee
  259. chmod +x /tmp/nginx-ee
  260. /tmp/nginx-ee "$@"
  261. }
  262. _INSTALL_NODEJS() {
  263. if [ "$#" -eq 0 ]; then
  264. echo "Usage : setup-nodejs <version>"
  265. echo "Example : setup-nodejs 12"
  266. else
  267. wget -O nodejs.sh https://deb.nodesource.com/setup_"$1".x
  268. chmod +x nodejs.sh
  269. ./nodejs.sh
  270. rm -f nodejs.sh
  271. $NEED_SUDO apt-get install -y nodejs
  272. fi
  273. }
  274. _UPDATE_NPM() {
  275. $NEED_SUDO npm install -g npm
  276. }
  277. _PPA_INSTALL() {
  278. if [ "$#" -eq 0 ]; then
  279. echo "Usage : IPPA <ppa-name>"
  280. echo " PPA : "
  281. echo " - ubuntu backports : --jonathonf"
  282. echo " - virtubox backports : --backports"
  283. echo " - ondrej php : --php"
  284. echo " - ondrej nginx : --nginx"
  285. echo " - WordOps nginx : --nginx-wo"
  286. echo " - ondrej apache : --apache"
  287. echo " - redis-server : --redis"
  288. echo " - ffmpeg4 : --ffmpeg"
  289. echo " - gcc8 : --gcc"
  290. echo " - tinc : --tinc"
  291. echo " - goaccess : --goaccess"
  292. echo " - handbrake : --handbrake"
  293. else
  294. PPA=""
  295. while [ "$#" -gt 0 ]; do
  296. case "$1" in
  297. --jonathonf)
  298. PPA="$PPA ppa:jonathonf/backports"
  299. ;;
  300. --backports)
  301. PPA="$PPA ppa:virtubox/backports"
  302. ;;
  303. --php)
  304. PPA="$PPA ppa:ondrej/php"
  305. ;;
  306. --nginx)
  307. PPA="$PPA ppa:ondrej/nginx-mainline"
  308. ;;
  309. --nginx-wo)
  310. PPA="$PPA ppa:virtubox/nginx-wo"
  311. ;;
  312. --apache)
  313. PPA="$PPA ppa:ondrej/apache2"
  314. ;;
  315. --redis)
  316. PPA="$PPA ppa:chris-lea/redis-server"
  317. ;;
  318. --ffmpeg)
  319. PPA="$PPA ppa:jonathonf/ffmpeg-4"
  320. ;;
  321. --gcc)
  322. PPA="$PPA ppa:jonathonf/gcc"
  323. ;;
  324. --tinc)
  325. PPA="$PPA ppa:virtubox/tinc"
  326. ;;
  327. --goaccess)
  328. PPA="$PPA ppa:alex-p/goaccess"
  329. ;;
  330. --handbrake)
  331. PPA="$PPA ppa:stebbins/handbrake-git-snapshots"
  332. ;;
  333. *) ;;
  334. esac
  335. shift
  336. done
  337. if [ -n "$PPA" ]; then
  338. for UPPA in $PPA; do
  339. $NEED_SUDO add-apt-repository "$UPPA" -y
  340. done
  341. fi
  342. fi
  343. }
  344. _INSTALL_NANORC() {
  345. if [ ! -d /usr/share/nano-syntax-highlighting ]; then
  346. git clone --depth 1 https://github.com/scopatz/nanorc.git /usr/share/nano-syntax-highlighting -q
  347. fi
  348. if ! grep -q "/usr/share/nano-syntax-highlighting" /etc/nanorc; then
  349. echo "include /usr/share/nano-syntax-highlighting/*.nanorc" >>/etc/nanorc
  350. fi
  351. }
  352. _SYSINFO() {
  353. # Reading out system information...
  354. # Reading CPU model
  355. cname=$(awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')
  356. # Reading amount of CPU cores
  357. cores=$(awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo)
  358. # Reading CPU frequency in MHz
  359. freq=$(awk -F: ' /cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')
  360. # Reading total memory in MB
  361. tram=$(free -m | awk 'NR==2 {print $2}')
  362. # Reading Swap in MB
  363. vram=$(free -m | awk 'NR==4 {print $2}')
  364. # Reading system uptime
  365. up=$(uptime | awk '{ $1=$2=$(NF-6)=$(NF-5)=$(NF-4)=$(NF-3)=$(NF-2)=$(NF-1)=$NF=""; print }' | sed 's/^[ \t]*//;s/[ \t]*$//')
  366. # Reading operating system and version (simple, didn't filter the strings at the end...)
  367. opsy=$(cat /etc/issue.net | awk 'NR==1 {print}') # Operating System & Version
  368. arch=$(uname -m) # Architecture
  369. lbit=$(getconf LONG_BIT) # Architecture in Bit
  370. hn=$(hostname -f) # Hostname
  371. kern=$(uname -r)
  372. echo ""
  373. # Output of results
  374. echo "System Info"
  375. echo "Server : $hn"
  376. echo "-----------"
  377. echo "Processor : $cname"
  378. echo "CPU Cores : $cores"
  379. echo "Frequency : $freq MHz"
  380. echo "Memory : $tram MB"
  381. echo "Swap : $vram MB"
  382. echo "Uptime : $up"
  383. echo "-----------"
  384. echo "OS : $opsy"
  385. echo "Arch : $arch ($lbit Bit)"
  386. echo "Kernel : $kern"
  387. echo ""
  388. }
  389. _SITESPEED() {
  390. if [ "$#" = "0" ]; then
  391. echo "Usage : sitespeed <url>"
  392. else
  393. curl -s -w \
  394. '\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 "$@"
  395. fi
  396. }
  397. _SETUP_TOOLS() {
  398. 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
  399. }
  400. _RSYSLOG_UFW() {
  401. [ -f /etc/rsyslog.d/20-ufw.conf ] && {
  402. sed -i 's/\#\& stop/\& stop/' /etc/rsyslog.d/20-ufw.conf
  403. service rsyslog restart
  404. }
  405. }
  406. _START_SSH_AGENT() {
  407. rm -f /root/.ssh/ssh_auth_sock
  408. ssh-agent -a /root/.ssh/ssh_auth_sock
  409. export SSH_AUTH_SOCK="/root/.ssh/ssh_auth_sock"
  410. ssh-add -l >/dev/null || ssh-add
  411. }
  412. _APT_REPO_UBUNTU() {
  413. cp /etc/apt/sources.list /etc/apt/sources.list.bak -f
  414. wget -O /etc/apt/sources.list "https://vtb.cx/$(lsb_release -sc)-list"
  415. }
  416. _APT_BIONIC_KERNEL() {
  417. if [ "$(lsb_release -sc)" = "bionic" ]; then
  418. apt update && apt install --install-recommends linux-generic-hwe-18.04 --assume-yes
  419. fi
  420. }
  421. _FTP_ADD() {
  422. if [ "$#" = "0" ] || [ "$#" = "1" ]; then
  423. echo "Usage : ftpadd <user> <domain>"
  424. else
  425. if ! command_exists pwgen; then
  426. $NEED_SUDO apt-get install pwgen -y >/dev/null 2>&1
  427. fi
  428. if [ -d /var/www/"$2"/htdocs ]; then
  429. ftpaccountpass=$(_PWGEN)
  430. $NEED_SUDO useradd -d "/var/www/$2/htdocs" -M -s /bin/false -G www-data "$1"
  431. echo "$1:$ftpaccountpass" | $NEED_SUDO chpasswd -m
  432. echo "Use : chmod -R g+rw /var/www/$2/htdocs"
  433. echo -e "\n\n[/var/www/$2/htdocs]" | tee -a "$HOME/.ftpaccounts"
  434. echo -e "user : $1" | tee -a "$HOME/.ftpaccounts"
  435. echo "password : $ftpaccountpass" | tee -a "$HOME/.ftpaccounts"
  436. else
  437. echo "site directory doesn't exist"
  438. fi
  439. fi
  440. }
  441. _UFW_MINIMAL() {
  442. ufw logging low
  443. ufw default allow outgoing
  444. ufw default deny incoming
  445. ufw limit 22
  446. ufw limit 10022
  447. ufw allow 53
  448. ufw allow http
  449. ufw allow https
  450. ufw allow 68
  451. ufw allow 655
  452. ufw allow 873
  453. ufw allow 123
  454. ufw allow 22222
  455. ufw allow from 144.76.159.118 to any port 35621
  456. ufw allow from 144.76.159.118 to any port 35622
  457. ufw allow from 144.76.159.118 to any port 35623
  458. ufw allow from 159.69.0.216 to any port 10050
  459. }
  460. _SSH_SECURE() {
  461. CURRENT_SSH_PORT=$(grep "Port" /etc/ssh/sshd_config | awk -F " " '{print $2}')
  462. wget https://raw.githubusercontent.com/VirtuBox/ubuntu-nginx-web-server/master/etc/ssh/sshd_config -O /etc/ssh/sshd_config
  463. if [ -n "$1" ]; then
  464. sed -i "s/Port 22/Port $1/" /etc/ssh/sshd_config
  465. else
  466. sed -i "s/Port 22/Port $CURRENT_SSH_PORT/" /etc/ssh/sshd_config
  467. fi
  468. }
  469. _PWGEN() {
  470. if ! command_exists pwgen; then
  471. apt_install pwgen
  472. fi
  473. pwgen -s 24 1
  474. }
  475. _FD() {
  476. if [ -z "$(command -v fd)" ]; then
  477. echo "downloading fd ..."
  478. 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
  479. echo "installing fd ..."
  480. dpkg -i /tmp/fd.deb && rm /tmp/db.deb >/dev/null
  481. fi
  482. fd "$@"
  483. }
  484. _LXC_LOGIN() {
  485. lxc exec "$@" /usr/bin/env bash
  486. }
  487. _LXC_LAUNCH() {
  488. local RANDOM
  489. RANDOM=$(date +%s | sha256sum | base64 | head -c 4)
  490. if [ "$#" = "0" ]; then
  491. lxc launch ubuntu-daily:18.04 "ctn-$RANDOM"
  492. else
  493. lxc launch ubuntu-daily:18.04 "$1"
  494. fi
  495. }
  496. _BACKPORT_PACKAGE() {
  497. if [ "$#" = "0" ]; then
  498. echo "Usage :"
  499. echo "bppackage <ppa (optional)> <.dsc url> "
  500. echo "exemple : bppackage ppa:virtubox/backports http://url.dsc"
  501. fi
  502. local RANDOM
  503. RANDOM=$(date +%s | sha256sum | base64 | head -c 4)
  504. if [ "$PWD" = "/root" ]; then
  505. mkdir -p "backport-$RANDOM"
  506. cd "backport-$RANDOM" || exit 1
  507. fi
  508. if [ "$#" = "1" ]; then
  509. backportpackage -r -w . -d bionic -u ppa:virtubox/backports -kE3CC41E7F354756B94A7DF4322EB296C97BAD476 "$1"
  510. elif [ "$#" = "2" ]; then
  511. backportpackage -r -w . -d bionic -u "$1" -kE3CC41E7F354756B94A7DF4322EB296C97BAD476 "$2"
  512. fi
  513. }
  514. _WO_FULL_UPGRADE() {
  515. [ ! -f "$HOME/.gitconfig" ] && {
  516. sudo bash -c 'echo -e "[user]\n\tname = $USER\n\temail = root@wops.cc" > $HOME/.gitconfig'
  517. }
  518. rm -f wo
  519. wget -qO wo https://raw.githubusercontent.com/WordOps/WordOps/updating-configuration/install && sudo bash wo -b updating-configuration
  520. }
  521. _SPEEDTEST() {
  522. if ! command_exists speedtest; then
  523. DEB_DISTRO=$(lsb_release -sc)
  524. apt-get install gnupg1 apt-transport-https dirmngr -y
  525. apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 379CE192D401AB61
  526. echo "deb https://ookla.bintray.com/debian $DEB_DISTRO main" | sudo tee /etc/apt/sources.list.d/speedtest.list
  527. apt-get update
  528. apt-get install speedtest -y
  529. fi
  530. speedtest
  531. }
  532. _INSTALL_WPCLI() {
  533. curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  534. chmod +x wp-cli.phar
  535. $NEED_SUDO mv wp-cli.phar /usr/local/bin/wp
  536. wget -O /etc/bash_completion.d/wp-completion.bash https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash
  537. }
  538. _SETUP_CONTROLLER() {
  539. if [ "$#" -eq 0 ]; then
  540. echo "Usage : SSETUP <ppa-name>"
  541. echo " PPA : "
  542. echo " - nanorc : --nanorc"
  543. echo " - wpcli : --wpcli"
  544. else
  545. INSTALL=""
  546. while [ "$#" -gt 0 ]; do
  547. case "$1" in
  548. --nanorc)
  549. INSTALL="$INSTALL _INSTALL_NANORC"
  550. ;;
  551. --wpcli)
  552. INSTALL="$INSTALL _INSTALL_WPCLI"
  553. ;;
  554. *) ;;
  555. esac
  556. shift
  557. done
  558. if [ -n "$INSTALL" ]; then
  559. for APP in $INSTALL; do
  560. $NEED_SUDO $APP
  561. done
  562. fi
  563. fi
  564. }
  565. _ZABBIX_UPDATE() {
  566. local distro
  567. if ! command_exists lsb_release; then
  568. apt install lsb-release -y >/dev/null
  569. fi
  570. distro="$(lsb_release -si)"
  571. 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
  572. dpkg -i /tmp/zabbix.deb
  573. rm -f /tmp/zabbix.deb
  574. }
  575. _BACK_FILE() {
  576. if [ ! -f "$1.bak" ]; then
  577. cp -f "$1" "$1.bak"
  578. else
  579. cp -f "$1" "$1_1.bak"
  580. fi
  581. }
  582. _URBACKUP_CLIENT_INSTALL() {
  583. TF=$(mktemp) && wget "https://hndl.urbackup.org/Client/2.4.9/UrBackup%20Client%20Linux%202.4.9.sh" -O "$TF" && sudo sh "$TF"
  584. rm -f "$TF"
  585. }
  586. _URBACKUP_CLIENT_CONF() {
  587. sed -i 's/RESTORE=disabled/RESTORE="server-confirms"/' /etc/default/urbackupclient
  588. wget -O "$HOME/mysqldump.sh" vtb.cx/mysqldump
  589. chmod +x "$HOME/mysqldump.sh"
  590. if command_exists ufw; then
  591. ufw allow from 144.76.159.118 to any port 35621
  592. ufw allow from 144.76.159.118 to any port 35622
  593. ufw allow from 144.76.159.118 to any port 35623
  594. ufw reload
  595. fi
  596. }
  597. _DELETE_WO_SITES() {
  598. sites=$(wo site list 2>&1)
  599. for site in $sites; do
  600. echo -ne " deleting $site [..]\r"
  601. if {
  602. wo site delete $site --force
  603. }; then
  604. echo -e '\t[OK]'
  605. fi
  606. done
  607. }
  608. _GOT_INSTALL() {
  609. cd /tmp || exit 0
  610. wget https://raw.githubusercontent.com/melbahja/got/master/install.sh
  611. sh install.sh && rm install.sh -f
  612. rm -f /usr/local/bin/got /usr/bin/got
  613. mv bin/got /usr/local/bin/
  614. chmod +x /usr/local/bin/got
  615. cd || exit 0
  616. }
  617. ##################################
  618. # ffmpeg
  619. ##################################
  620. ffmpeg_start_time() {
  621. if [ "$#" = "0" ]; then
  622. echo "ffmpeg-cut-start <start-time> <input> <duration(optional)>"
  623. echo ""
  624. echo "Example : ffmpeg-cut-start 00:34 file.mp4 300"
  625. echo ""
  626. echo "----------------------------------------------------------"
  627. else
  628. if [ -n "$3" ]; then
  629. bash -c "ffmpeg -ss '$1' -t '$3' -y -i '$2' -c copy -movflags +faststart '${2%.mp4}-output.mp4';"
  630. else
  631. bash -c "ffmpeg -ss '$1' -y -i '$2' -c copy -movflags +faststart '${2%.mp4}-output.mp4';"
  632. fi
  633. fi
  634. }
  635. _STABILISE_VIDEO() {
  636. for i in "$PWD"/*; do
  637. echo "Processing vid $i ...${i%.*}"
  638. ffmpeg -i "$i" -vf vidstabdetect=stepsize=6:shakiness=5:accuracy=15:result=/tmp/transform_vectors.trf -f null -
  639. 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"
  640. rm /tmp/transform_vectors.trf
  641. rm -f "$i"
  642. done
  643. }
  644. _MP4_CONVERT() {
  645. for i in "$PWD"/*; do
  646. echo "Processing vid $i ...${i%.*}"
  647. ffmpeg -i "$i" -c:v libx264 -preset slow -crf 18 -c:a aac -b:a 192k -pix_fmt yuv420p -movflags +faststart "${i%.*}-converted.mp4"
  648. rm -f "$i"
  649. done
  650. }
  651. _YOUTUBE_ENCODE() {
  652. for i in "$PWD"/*; do
  653. ffmpeg -i "$i" -c:v libx264 -preset slow -crf 18 -c:a aac -b:a 192k -pix_fmt yuv420p -movflags +faststart "${i%.*}-yt.mp4"
  654. done
  655. }
  656. _MERGE_VIDEO() {
  657. rm -f mylist
  658. if [ "$#" -eq 0 ]; then
  659. for i in "$PWD"/*; do
  660. echo "file '$i'" >>mylist.txt
  661. done
  662. else
  663. videos="$*"
  664. for video in $videos; do
  665. echo "file '$video'" >>mylist.txt
  666. done
  667. fi
  668. ffmpeg -f concat -safe 0 -i mylist.txt -c copy "output-merged.mp4"
  669. rm -f mylist.txt
  670. }
  671. _TRANSFER_AUDIO() {
  672. if [ "$#" -eq 0 ]; then
  673. echo "Usage : ffmpeg-audio <file-with-audio> <file-without-audio>"
  674. else
  675. rm -f "${1%.*}.aac"
  676. ffmpeg -i "$1" -vn -acodec copy "${1%.*}.aac"
  677. if [ -f "${1%.*}.aac" ]; then
  678. rm -f "${2%.*}-sound.mp4"
  679. ffmpeg -i "$2" -i "${1%.*}.aac" -map 0:v -map 1:a -c copy -y "${2%.*}-sound.mp4"
  680. rm -f "${1%.*}.aac"
  681. fi
  682. fi
  683. }
  684. _x265_encode() {
  685. if [ "$#" -eq 0 ]; then
  686. echo "Usage : ffmpeg-x265 <file> <options>"
  687. echo "Options :"
  688. echo " -d : process all files in the current directory"
  689. elif [ "$#" -eq 1 ]; then
  690. if [ "$1" = "-d" ]; then
  691. for i in "$PWD"/*; do
  692. ffmpeg -i "$i" -c:v libx265 -crf 28 -c:a aac -b:a 160k "${i%.*}-x265.mp4"
  693. done
  694. else
  695. if [ -f "$1" ]; then
  696. ffmpeg -i "$1" -c:v libx265 -crf 28 -c:a aac -b:a 160k "${1%.*}-x265.mp4"
  697. fi
  698. fi
  699. fi
  700. }
  701. _extract_audio() {
  702. ffmpeg -i "$1" -vn -acodec copy "${1%.*}.aac"
  703. }
  704. _strip_audio() {
  705. ffmpeg -i "$1" -codec copy -an "${1%.*}-muted.mp4"
  706. }
  707. _identify_resolution() {
  708. for i in "$PWD"/*.mp4; do
  709. if ! echo "$i" | grep -qE "240|480|720|1080|1440|2160"; then
  710. height=$(ffprobe -v error -print_format json -show_format -show_streams -show_chapters "$i" | jq .streams[0] | jq -r .height)
  711. mv "$i" "${i%.*}-$height.mp4"
  712. fi
  713. done
  714. }
  715. # enable color support of ls and also add handy aliases
  716. # some more ls aliases
  717. #alias wp='/usr/bin/wp --allow-root'
  718. alias .....="cd ../../../.."
  719. alias ....="cd ../../.."
  720. alias ...="cd ../.."
  721. alias ..="cd .."
  722. alias allservices='service --status-all'
  723. alias apt-bionic-kernel=_APT_BIONIC_KERNEL
  724. alias apt-repo-ubuntu=_APT_REPO_UBUNTU
  725. alias aptremove='apt-get autoremove -y --purge'
  726. alias arsync_hard='rsync -rLptgoD --human-readable --progress'
  727. alias arsync='rsync -avz -h --progress'
  728. alias bppackage=_BACKPORT_PACKAGE
  729. alias cheat=CHEAT_CHECK
  730. alias commit=GIT_COMMIT
  731. alias dd-benchmark=DD_BENCHMARK_DISK
  732. alias dir='dir --color=auto'
  733. alias docker-setup=EE_DOCKER_SETUP
  734. alias duplicate-finder=_find_duplicates
  735. alias ee-bashrc-update=update_git_mybashrc
  736. alias ee-ls=LIST_BY_SIZE
  737. alias ee-mysql-optimize=EE_MYSQL_OPTIMIZATION
  738. alias ee-syslog='tail -n 250 /var/log/syslog | ccze -A'
  739. alias egrep='egrep --color=auto'
  740. alias ffmpeg-cut-start=ffmpeg_start_time
  741. alias ffmpeg-merge=_MERGE_VIDEO
  742. alias ffmpeg-audio=_TRANSFER_AUDIO
  743. alias ffmpeg-x265=_x265_encode
  744. alias ffmpeg-resolution=_identify_resolution
  745. alias ffmpeg-aac=_extract_audio
  746. alias ffmpeg-strip=_strip_audio
  747. alias ftpadd=_FTP_ADD
  748. alias fgrep='fgrep --color=auto'
  749. alias gg="ping google.fr"
  750. alias gpg-crypt=encrypt_gpg
  751. alias gpg-decrypt=decrypt_gpg
  752. alias gpigz=compress_pigz
  753. alias grep='grep --color=auto'
  754. alias gunpigz=decompress_pigz
  755. alias setup-got=_GOT_INSTALL
  756. alias install-nanorc=_INSTALL_NANORC
  757. alias IPPA=_PPA_INSTALL
  758. alias l='ls -CF'
  759. alias la='ls -A'
  760. alias ld='du -sh ./* | sort -h'
  761. alias lh="stat -c '%A %a %n' ./*"
  762. alias ll='ls -alhF'
  763. alias ls='ls --color=auto'
  764. alias lxclogin=_LXC_LOGIN
  765. alias lxclaunch=_LXC_LAUNCH
  766. alias magento-upgrade=MAGENTO_UPGRADE
  767. alias maintenance=MAINTENANCE_APT
  768. alias nano='nano -E'
  769. alias netdata-fix=_FIX_NETDATA
  770. alias nginx-ee=_NGINX_EE
  771. alias npm-update=_UPDATE_NPM
  772. alias passwdgen=_PWGEN
  773. alias pip='python3 -m pip'
  774. alias random-commit=RANDOM_GIT_COMMIT
  775. alias rsyslog-ufw=_RSYSLOG_UFW
  776. alias setdirperm=_PERM_FOLDER
  777. alias setfileperm=_PERM_FILES
  778. alias setup-nodejs=_INSTALL_NODEJS
  779. alias setup-tools=_SETUP_TOOLS
  780. alias show-fpm-process=EE_SHOW_FPM
  781. alias showlog=EE_SHOW_LOG
  782. alias sitespeed=_SITESPEED
  783. alias ssh-secure=_SSH_SECURE
  784. alias start-ssh=_START_SSH_AGENT
  785. alias sysinfo=_SYSINFO
  786. alias tinc-down=SET_TINC_DOWN
  787. alias tinc-up=SET_TINC_UP
  788. alias transfer=transfer_vtbox_net
  789. alias ufw-minimal=_UFW_MINIMAL
  790. alias vb-virtualenv=_PYTHON_VIRTUALENV
  791. alias vdir='vdir --color=auto'
  792. alias wo-virtualenv=_PYTHON_VIRTUALENV
  793. alias wp-fix-perms=_WP_PERMISSIONS
  794. alias upgrade-wo-full=_WO_FULL_UPGRADE
  795. alias speedtestnet="_run _SPEEDTEST 'Installing speedtest'"
  796. alias SSETUP=_SETUP_CONTROLLER
  797. alias journalctfail='journalctl -b -p err -S "yesterday"'
  798. alias zabbix-update=_ZABBIX_UPDATE
  799. alias bak=_BACK_FILE
  800. alias update-wo-fast='python3 -m pip install -I "git+git://github.com/WordOps/WordOps.git@updating-configuration#egg=wordops"'
  801. alias clone-wordops='git clone https://github.com/WordOps/WordOps.git -b updating-configuration'
  802. alias urbackup-client-install=_URBACKUP_CLIENT_INSTALL
  803. alias urbackup-client-conf=_URBACKUP_CLIENT_CONF
  804. alias delete-all-site=_DELETE_WO_SITES
  805. alias stabilise-video=_STABILISE_VIDEO
  806. alias yt-encode=_YOUTUBE_ENCODE
  807. alias mp4convert=_MP4_CONVERT
  808. alias suroot='sudo su - root'
  809. alias apt-playbook='ansible-playbook /etc/ansible/playbooks/include/update-apt-servers.yml'
  810. alias full-upgrade-playbook='ansible-playbook /etc/ansible/playbooks/update-all-servers.yml'
  811. alias ssh-key-convert='ssh-keygen -i -f'
  812. alias gzstd=compress_zstd
  813. alias gunzstd=decompress_ztsd
  814. alias plesk-upgrade='plesk installer --select-release-latest --upgrade-installed-components'