Created
November 24, 2020 13:32
-
-
Save dhsathiya/c37f9f206147cc97399d0c98668c3706 to your computer and use it in GitHub Desktop.
Delete unused sites
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 | |
tail -n $1 $2 | 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 100 /opt/easyengine/sites/$1/logs/php/access.log)) | |
dates_of_nginx_access=($(get_access_dates 100 /opt/easyengine/sites/$1/logs/nginx/access.log)) | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment