Last active
November 21, 2023 11:10
-
-
Save dhsathiya/f40757e9c64cfe11ed432c0fcb76965c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function date_to_seconds() { | |
if [[ $# -lt 1 ]]; then | |
echo "Date argument missing" | |
echo "Usage: date_to_seconds date" | |
exit 1 | |
fi | |
remove_slash_date=$(echo $1 | sed 's/\//\-/g') | |
echo $(date +%s -d $(echo $remove_slash_date)) | |
} | |
function get_ee_path() { | |
echo "$(command -v ee)" | |
} | |
function get_all_sites() { | |
EE_PATH="$(get_ee_path)" | |
echo "$($EE_PATH site list --format=text)" | |
} | |
function get_access_dates() { | |
if [[ $# -lt 2 ]]; then | |
echo "Argument/s missing" | |
echo "Usage: get_access_dates lines_to_tail log_path" | |
exit 1 | |
fi | |
if [[ $3 == "access" ]]; then | |
if [[ $(tail -n $1 /opt/easyengine/sites/$4/logs/$2/access.log | grep "wp-login.php" | awk '{ if (substr($4,1,1) ~ /\+/ ) {print $3} else {print $4}}' | tr -d '[]' | cut -f1 -d":") ]]; then | |
check=$? | |
echo $check | |
return | |
elif [[ ! $(tail -n $1 /opt/easyengine/sites/$4/logs/$2/access.log.1 | grep "wp-login.php" | awk '{ if (substr($4,1,1) ~ /\+/ ) {print $3} else {print $4}}' | tr -d '[]' | cut -f1 -d":") ]]; then | |
echo "EMPTY1" | |
else | |
for gzipped_file_number in {2..20}; do | |
if [[ ! $(tail -n $1 /opt/easyengine/sites/$4/logs/$2/access.log.$gzipped_file_number.gz | grep "wp-login.php" | awk '{ if (substr($4,1,1) ~ /\+/ ) {print $3} else {print $4}}' | tr -d '[]' | cut -f1 -d":") ]]; then | |
echo "EMPTY3" | |
fi | |
done | |
fi | |
fi | |
#tail -n $1 $2 | grep "wp-login.php" | awk '{ if (substr($4,1,1) ~ /\+/ ) {print $3} else {print $4}}' | tr -d '[]' | cut -f1 -d":" | |
} | |
function return_last_access_day() { | |
if [[ $# -lt 1 ]]; then | |
echo "Sitename argument missing" | |
echo "Usage: return_last_access_day sitename" | |
exit 1 | |
fi | |
current_day_in_seconds=$(date +%s) | |
#dates_of_php_access=($(get_access_dates 1000 /opt/easyengine/sites/$1/logs/php/access.log.1)) | |
#dates_of_nginx_access=($(get_access_dates 1000 /opt/easyengine/sites/$1/logs/nginx/access.log.1)) | |
echo $(get_access_dates 1000 php access $1) | |
exit | |
# dates_of_php_access=($(get_access_dates 1000 php access $1)) | |
# dates_of_nginx_access=($(get_access_dates 1000 nginx access $1)) | |
common_dates=() | |
# Stores common date from NGINX and PHP logs into common_dates array. | |
for datep in "${dates_of_php_access[@]}"; do | |
for daten in "${dates_of_nginx_access[@]}"; do | |
if [[ "$datep" == "$daten" ]]; then | |
common_dates+=("$daten") | |
fi | |
done | |
done | |
# Store only unique dates from the common_dates into unique_common_dates. | |
unique_common_dates=($(echo "${common_dates[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) | |
unique_common_dates_in_seconds=() | |
# Converts dates from unique_common_dates in seconds. | |
for unique_common_date in ${unique_common_dates[@]}; do | |
unique_common_dates_in_seconds+=($(date_to_seconds $unique_common_date)) | |
done | |
# Find last day it was used from the array. | |
least_day=$(( current_day_in_seconds-unique_common_dates_in_seconds[0] )) | |
for date_in_second in ${unique_common_dates_in_seconds[@]}; do | |
date_in_second=$(( $current_day_in_seconds - $date_in_second )) | |
if [[ $date_in_second -lt $least_day ]]; then | |
least_day=$date_in_second | |
fi | |
done | |
echo "$(($least_day/(60*60*24)))" | |
} | |
#------------------------------------------------------------------------------ | |
# Disable if site accessed more than 7 days ago. | |
# Create a backup and delete site if accessed more than 25 days ago. | |
# Script runs weekly | |
#------------------------------------------------------------------------------ | |
function site_disable() { | |
for site in $(get_all_sites);do | |
if [[ ! $($(get_ee_path) site info $site | grep 'WordPress' ) ]]; then | |
continue | |
fi | |
last_access_day=$(return_last_access_day $site) | |
echo $site $last_access_day | |
done | |
} | |
site_disable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Track IP and command from .zshrc | |
preexec() { | |
echo "[$(date +%d/%m/%Y-%H:%M:%S)] $(echo $SSH_CLIENT | awk '{ print $1}') - $1" >> /var/log/command.log | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment