Browse Source

add sh scripts

Dalibor Votruba 1 năm trước cách đây
mục cha
commit
6694611954

+ 58 - 0
@Scripts/Bash/check_dir_size.sh

@@ -0,0 +1,58 @@
+#!/bin/bash
+
+# v.0.0.0.1
+# 21.4.2024
+
+# scripts check directory size and if exceeds maximal size, notify via email to recipient
+# syntax:
+# check_dir_size <path_to_dir> <maximum_size> <recipient> [<custom_subject>]
+# <path_to_dir>		- path to check direcory. Directory must exists and be acessible
+# <maximum_size>	- whole number size in bytes
+# <recipient>		- email address to notify
+# <custom_subject>	- optional. Custom subject text. If not defined uses default.
+. ./qbashlib.sh
+
+
+default_subject="Warning notification - directory exceed maximal size."
+default_body="Directory '$1' exceed maximal size $2 bytes"
+
+body=$default_body
+subject=$default_subject
+
+# Zkontroluj, zda je zadán první argument
+argument_exists "$1" "<path_to_dir>"
+
+# Zkontroluj, zda existuje adresář s instalací Wordpressu
+if [ ! -d "$1" ]; then
+  echo "<path_to_dir> directory '$1' not found or not accssible."
+  exit 1
+fi
+
+# Zkontroluj, zda je zadán druhý argument
+argument_exists "$2" "<maximum_size>"
+
+# Zkontroluj, zda je zadán druhý argument jako číslo
+is_number $2
+if [ $? -eq 0 ]; then
+  echo "<maximum_size> argument must whole number value."
+  exit 1
+fi
+
+# Zkontroluj, zda je zadán třetí argument
+is_email $3
+if [ $? -eq 0 ]; then
+  echo "<recipient> argument '$3' is not valid email address."
+  exit 1
+fi
+
+# Zkontroluj, zda je zadán třetí argument
+if [ ! -z "$4" ]; then
+  subject=$4
+fi
+
+# porovnání velikosti adresáře proti maximální hodnotě
+dir_size=$(dir_get_size $1)
+if [ $dir_size -gt $2 ]; then
+	# velikost přesáhls maximum -> notifikuj
+	mail_send "$3" "$subject" "$body"
+fi

+ 26 - 0
@Scripts/Bash/check_dirs.sh

@@ -0,0 +1,26 @@
+#!/bin/bash
+
+# v.0.0.0.1
+# 22.4.2024
+
+# scripts check directory collection size limits and if exceeds maximal size, notify via email to recipient
+# syntax:
+# check_dirs
+# setup following constants directories, recipient
+
+# setup following constants - directories
+directories=(
+    [0,0]="/var/www/quadarax"    # directory name
+    [0,1]=10000000               # maximal size in bytes
+    [1,0]="/var/www/hosting/geodet-vorlicek.cz"
+    [1,1]=10000000
+)
+
+# setup following constants - recipient
+recipient="dalibor.votruba@gmail.com"
+
+dir_length=${#directories[@]}
+for ((i=0; i<$dir_length; i++)); do
+    . ./check_dir_size.sh $(directories[$i, 0]) $(directories[$i, 1]) $recipient "MORDOR WARN: Directory exceed size"
+done
+

+ 105 - 0
@Scripts/Bash/qbashlib-tests.sh

@@ -0,0 +1,105 @@
+#!/bin/bash
+
+# v.0.0.0.1
+# 24.4.2024
+
+# tests for qbashlib.sh v.0.0.0.2
+. ./qbashlib.sh
+
+test_ok=0
+test_fail=0
+
+echo "*** 1. is_number ***"
+echo "1.1 is_number a"
+value="a"
+is_number $value
+if [ $? -eq 0 ]; then
+    echo "OK"
+    ((test_ok++))
+else
+    echo "FAIL"
+    ((test_fail++))
+fi
+
+
+echo "1.2 is_number a"
+value="1"
+is_number $value
+if [ $? -eq 1 ]; then
+    echo "OK"
+    ((test_ok++))
+else
+    echo "FAIL"
+    ((test_fail++))
+fi
+
+
+echo "*** 2. is_email ***"
+echo "2.1 is_email a"
+value="a"
+is_email $value
+if [ $? -eq 0 ]; then
+    echo "OK"
+    ((test_ok++))
+else
+    echo "FAIL"
+    ((test_fail++))
+fi
+
+
+echo "2.2 is_email a@b"
+value="a@b"
+is_email $value
+if [ $? -eq 0 ]; then
+    echo "OK"
+    ((test_ok++))
+else
+    echo "FAIL"
+    ((test_fail++))
+fi
+
+echo "2.3 is_email a@b.com"
+value="a@b.com"
+is_email $value
+if [ $? -eq 1 ]; then
+    echo "OK"
+    ((test_ok++))
+else
+    echo "FAIL"
+    ((test_fail++))
+fi
+
+
+echo "*** 3. argument_exists ***"
+echo "3.1 argument_exists a"
+value="a"
+argument_exists $value "test"
+if [ $? -eq 1 ]; then
+    echo "OK"
+    ((test_ok++))
+else
+    echo "FAIL"
+    ((test_fail++))
+fi
+
+echo "3.2 argument_exists <empty>"
+value=""
+argument_exists $value "test"
+if [ $? -eq 0 ]; then
+    echo "OK"
+    ((test_ok++))
+else
+    echo "FAIL"
+    ((test_fail++))
+fi
+
+
+# total evaluation
+echo "TOTAL $test_ok SUCC / $test_fail FAILED"
+if [ $test_fail -eq 0 ]; then
+    echo "TEST RESULT: SUCCEED"
+    exit 0
+else
+    echo "TEST RESULT: FAILED"
+    exit 1
+fi

+ 126 - 0
@Scripts/Bash/qbashlib.sh

@@ -0,0 +1,126 @@
+#!/bin/bash
+
+# v.0.0.0.2
+# 23.4.2024
+
+
+# is_number(value)
+# checks if value is whole number numeric string
+# returns: 	0 - not a number
+#			1 - is a number
+is_number() {
+  local value="$1"
+  re='^[0-9]+$'
+  if ! [[ $value =~ $re ]] ; then
+    return 0
+  else
+    return 1
+  fi
+}
+
+# is_email(value)
+# checks if value is valid email string
+# returns: 	0 - not a email
+#			1 - is a email
+is_email() {
+  local email="$1"
+  local regex="^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
+
+  if [[ "$email" =~ $regex ]]; then
+    return 1
+  else
+    return 0
+  fi
+}
+
+# argument_exists(argument_value argument_name)
+# checks if argument is empty, then print message and exit with code 1
+# returns:  0 - not exists
+#     1 - exists
+argument_exists() {
+  # Zkontroluj, zda je zadán první argument
+  if [ -z "$1" ]; then
+    echo "$2 argument must be filled."
+    return 0
+  fi
+  return 1
+}
+
+# dir_get_size(path_to_dir)
+# returns directory size if exists, otherwise exit 1
+# returns:	size - size in bytes
+#			exit 1 - path_to_dir not exists
+dir_get_size(){
+	# Zkontroluj, zda existuje adresář path_to_dir
+	local dir="$1"
+	if [ ! -d $dir ]; then
+		exit 1
+	fi
+	result=$(du -sb $dir | cut -f1)
+	echo $result
+}
+
+# dir_ensure_trailing_slash(path_to_dir)
+# returns full path with trailing slash at the end, therefore full path not exists.
+# returns:	full path wirh trailing slash
+dir_ensure_trailing_slash() {
+    local path="$1"
+    if [[ ! "$path" == */ ]]; then
+        path="${path}/"
+    fi
+    echo "$path"
+}
+
+# dir_get_name(path_to_dir)
+# returns directory name from full path, therefore full path not exists.
+# returns:	name of directory
+dir_get_name() {
+    local full_path=$(dir_ensure_trailing_slash "$1")
+    local directory_name="${full_path##*/}"
+    echo "$directory_name"
+}
+
+# filename_get_ymwd(prefix extension)
+# creates filename that contains <prefix>_<year>_<month>_<week>_<day-of-week>.<extension>
+# returns:  filename
+filename_get_ymwd(){
+  # Získání aktuálního data
+  local current_date=$(date +'%Y-%m-%d')  # Například: 2024-04-22
+
+  # Získání čísla roku
+  local year=$(date +'%Y')  # Například: 2024
+
+  # Získání čísla měsíce
+  local month=$(date +'%m')  # Například: 04
+
+  # Získání čísla týdne v roce
+  local week_number=$(date +'%U')  # Například: 16
+
+  # Získání čísla dne v týdnu (0 = neděle, 1 = pondělí, ..., 6 = sobota)
+  local day_of_week=$(date +'%w')  # Například: 6 (sobota)
+
+  # Sestavení jména souboru
+  local file_name="$1_${year}_${month}_${week_number}_${day_of_week}.$2"
+
+  echo $file_name
+}
+
+# mail_send(recipient subject body is-test)
+# sends text email with subject and body to recipient via sendmail
+# if is-test="true" then just print message if set to "false" or argument is missing send via sendmail
+mail_send(){
+  local recipient=$1
+  local subject="$2"
+  local body="$3"
+  local is-test=$4
+
+  is_email $recipient
+  if [ $? -eq 0 ]; then
+    echo "<recipient> argument '$recipient' is not valid email address."
+    exit 1
+  fi
+
+  argument_exists "$subject" "<subject>" 
+
+  echo "$body" | mail -s "$subject" $recipient
+}

+ 2 - 0
@Scripts/Bash/readme.txt

@@ -0,0 +1,2 @@
+qbashlib.sh
+Core library with essential functions and patterns

+ 63 - 0
@Scripts/Bash/wp_update_site.sh

@@ -0,0 +1,63 @@
+#!/bin/bash
+
+# v.0.0.0.1
+# 23.4.2024
+
+# scripts check wordpress site for updates and if updates avalable, updates site. Finally send notification.
+# syntax:
+# wp_update_site
+# <path_to_dir>		- path to site direcory. Directory must exists and be acessible
+# <recipient>		- email address to notify
+# <custom_subject>	- optional. Custom subject text. If not defined uses default.
+
+. ./qbashlib.sh
+
+# Zkontroluj, zda je zadán první argument
+argument_exists "$1" "<path_to_dir>"
+
+# Zkontroluj, zda existuje adresář s instalací Wordpressu
+if [ ! -d "$1" ]; then
+  echo "<path_to_dir> directory '$1' not found or not accssible."
+  exit 1
+fi
+
+# Zkontroluj, zda je zadán třetí argument
+is_email $2
+if [ $? -eq 0 ]; then
+  echo "<recipient> argument '$3' is not valid email address."
+  exit 1
+fi
+
+site_name=$(dir_get_name "$1")
+default_subject="Update notification - $site_name"
+subject=$default_subject
+
+# Zkontroluj, zda je zadán třetí argument
+if [ ! -z "$3" ]; then
+  subject=$4
+fi
+
+cd $1
+
+# Zkontrolujte aktualizace pro WordPress, pluginy a šablony
+wp_core_updates=$(wp core check-update)
+wp_plugin_updates=$(wp plugin list --update=available)
+wp_theme_updates=$(wp theme list --update=available)
+
+# Pokud jsou dostupné aktualizace, proveďte zálohu
+if [[ -n "$wp_core_updates" ]] || [[ -n "$wp_plugin_updates" ]] || [[ -n "$wp_theme_updates" ]]; then
+  
+  # Proveďte všechny aktualizace
+  wp_result=$(wp core update)
+  wp_result="$wp_result\n$(wp plugin update --all)"
+  wp_result="$wp_result\n$(wp theme update --all)"
+
+  body="Available updates:\n$wp_core_updates\n$wp_plugin_updates\n$wp_theme_updates\n"
+  body="$body Processed updates:\n$wp_result"
+  #echo "$body" | mail -s "$subject" $recipient
+  echo "$subject -> $2\n$body"
+
+  echo "Updates complete."
+else
+  echo "No updates available."
+fi