Skip to content
Snippets Groups Projects
Commit 78260392 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
#!/bin/bash
# ======================================================================
#
# check backup status on storage
#
# ----------------------------------------------------------------------
# 2016-12-02 created hahn
# 2018-02-14 v1.1 hahn configure time limit
# 2019-10-20 v1.3 hahn source inc_config.sh
# 2020-12-07 v1.4 hahn deactivate backup
# ----------------------------------------------------------------------
. `dirname $0`/inc_config.sh
cd ${sBackupBasedir} || exit 1
sVersion=1.4
typeset -i iErrors=0
typeset -i iHours=36
typeset -i iMinutes=$iHours*60
typeset -i iKeepBackups=180
sInactiveFile=inactive.txt
# ----------------------------------------------------------------------
#FUNCTIONS
# ----------------------------------------------------------------------
# ------------------------------------------------------------
# color text
# ------------------------------------------------------------
function color(){
sColorcode=""
case $1 in
"reset") sColorcode="0"
;;
"head") sColorcode="33" # yellow
;;
"cmd") sColorcode="94" # light blue
;;
"input") sColorcode="92" # green
;;
"ok") sColorcode="92" # green
;;
"error") sColorcode="91" # red
;;
esac
if [ ! -z ${sColorcode} ]; then
echo -ne "\e[${sColorcode}m"
fi
}
# get age of a file in sec
# param string filename to test
#
function _getFileAge(){
echo $(($(date +%s) - $(date +%s -r "$1")))
}
function getServers(){
# ls -1 | fgrep "iml.unibe.ch"
ls -1 | grep -Ev "^(_active|SOMETHINGELSE)"
}
function _checkDir(){
typeset -i iCount
mydir=$1
if [ -L $mydir ]; then
mydir="`ls -l $mydir | awk '{print $11}'`"
echo "INFO: Link detected to $mydir"
fi
if [ -d $mydir ]; then
iCount=`find $mydir -type f -cmin -$iMinutes | wc -l `
if [ $iCount -eq 0 ]; then
color error
fi
echo -ne "$iCount\t"
echo -ne "`du -hs $mydir #| awk ' { print $1 } '`\t"
if [ $iCount -eq 0 ]; then
echo -n "<<< WARNING: No changed files detected !!! rc=1"
color reset
iErrors=$iErrors+1
fi
echo
else
echo "SKIP: $mydir won't be scanned."
fi
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
echo
color head
echo "===== BACKUP STATUS $sVersion ====="
color reset
echo
echo "time : `date`"
echo "backup directory : `pwd`"
# echo "used space : `du -hs $mydir | awk ' { print $1 } '`"
echo "free space : `df -h . | tail -1 | awk ' { print $4 } '`"
echo
echo "changed files last $iHours hours and used diskspace:"
echo
# _checkDir .
echo
echo "... and by server and backup sets:"
echo
for myserver in `getServers`
do
color head
echo ----- $myserver
color reset
echo
if [ -f $myserver/$sInactiveFile ]; then
du -hs $myserver
echo
echo "INFO: server is marked as inactive."
ls -l $myserver/$sInactiveFile
cat $myserver/$sInactiveFile
typeset -i iAge=`_getFileAge "$myserver/$sInactiveFile"`
typeset -i iDays=$iKeepBackups-$iAge/60/60/24
# echo Age: $iAge s
if [ $iDays -lt 0 ]; then
color error
echo "ERROR: Outdated backup was found! You can delete data for $myserver."
color reset
iErrors=$iErrors+1
else
echo "INFO: keeping it $iKeepBackups d .. $iDays d left."
fi
else
echo
_checkDir $myserver
for subdir in `ls -1 $myserver`
do
_checkDir $myserver/$subdir
done
fi
echo
done
# --- show result
echo -n "Status: "
if [ $iErrors -eq 0 ]; then
color ok
echo "OK"
else
color error
echo "$iErrors Errors detected."
fi
color reset
echo rc=$iErrors
exit $iErrors
# ======================================================================
#
# IML BACKUP STORAGE HELPER :: CONFIG
#
# !!!DO NOT EDIT ON SERVER!!!
# This file was generated by Puppet.
#
# ----------------------------------------------------------------------
#
#
# ======================================================================
# max count of simultanous connections; 0 is unlimited
typeset -i iMaxConnections=10
typeset -i iConnections
sBackupBasedir="/netshare/one-backup"
sConncectionDir="$sBackupBasedir/_active"
sLogdir="/opt/imlbackup/server/"
sLogfile="$sLogdir/connections.log"
# ----------------------------------------------------------------------
#!/bin/bash
# ======================================================================
#
# IML BACKUP STORAGE HELPER
#
# ----------------------------------------------------------------------
#
# SYNTAX:
# register [hostname]
# init backup for hostname; backup client must interrupt/ repeat
# return code is <> 0
#
# unregister [hostname]
# send info that a backup was done to delete the connection flag
# for the given host
#
# status
# show current connections
#
# usage
# show diskusage infos
#
# ----------------------------------------------------------------------
# 2019-11-01 hahn write more clear log messages
# ======================================================================
# ----------------------------------------------------------------------
# CONFIG
# ----------------------------------------------------------------------
. `dirname $0`/inc_config.sh
# set in setBackupclient
sBackupClient=dummy
# value is set in _showConnectionCount
iConnections=0
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
# add a line into logfile
# param string message (date will be added as prefix)
function _addLog(){
# echo `date` $* >>${sLogDir}${sLogfile}
echo `date "+%Y-%m-%d %H:%M:%S"` $* >>${sLogfile}
}
# ------------------------------------------------------------
# color text
# ------------------------------------------------------------
function color(){
sColorcode=""
case $1 in
"reset") sColorcode="0"
;;
"head") sColorcode="33" # yellow
;;
"cmd") sColorcode="94" # light blue
;;
"input") sColorcode="92" # green
;;
"ok") sColorcode="92" # green
;;
"error") sColorcode="91" # red
;;
esac
if [ ! -z ${sColorcode} ]; then
echo -ne "\e[${sColorcode}m"
fi
}
function _checkDir(){
typeset -i iCount
mydir=$1
if [ -d $mydir ]; then
iCount=`find $mydir -type f -cmin -720 | wc -l `
if [ $iCount -eq 0 ]; then
color error
fi
echo -n "$iCount "
echo -n "`du -hs $mydir #| awk ' { print $1 } '` "
if [ $iCount -eq 0 ]; then
echo -n "<<< WARNING: No changed files detected !!! rc=1"
color reset
iErrors=$iErrors+1
fi
echo
else
echo SKIP: $mydir is not a directory
fi
}
# show current connction count
function _showConnectionCount(){
iConnections=`getConnectionCount`
sMax=$iMaxConnections
if [ $iMaxConnections -eq 0 ]; then
sMax="0 [unlimited]"
fi
echo STATUS: $iConnections existing connection\(s\). Allowed maximum is $sMax.
}
# helper: get filename for backup client connection
function getConnFilename(){
# sBackupClient=$1
echo $sConncectionDir/$sBackupClient
}
# allow a slot for a new connection
# see PUBLIC_register
function addConnection(){
# sBackupClient=$1
sTouchfile=`getConnFilename $sBackupClient`
if [ -f "$sTouchfile" ]; then
echo INFO: server [$sBackupClient] has a slot already
_addLog "IGNORE request for a slot - [$sBackupClient] has a slot already"
else
touch "$sTouchfile"
_addLog "REQUEST a slot for server [$sBackupClient]"
fi
}
# remove slot of a connection
# see PUBLIC_unregister
function freeConnection(){
# sBackupClient=$1
sTouchfile=`getConnFilename $sBackupClient`
if [ -f "$sTouchfile" ]; then
rm -f "$sTouchfile"
else
echo INFO: server [$sBackupClient] had no reserved slot
_addLog "IGNORE freeing the connection - [$sBackupClient] had no reserved slot"
fi
}
# count of conection slots
function getConnectionCount(){
# who | grep "^${sBackupUser}" | wc -l
find $sConncectionDir -type f | wc -l
}
# helper function: set internal var for hostname of backup client
function setBackupclient(){
sBackupClient=`echo $1 | sed s"#[^a-zA-Z\.0-9]#_#g"`
if [ -z $sBackupClient ]; then
echo ERROR: no backup client was given
exit 1
fi
}
# ----------------------------------------------------------------------
# PUBLIC FUNCTIONS
# ----------------------------------------------------------------------
# return backup directory for a client
function PUBLIC_getBackupdir(){
setBackupclient $1
echo $sBackupBasedir/$sBackupClient
exit 0
}
# register a hostname to start backups
function PUBLIC_register(){
setBackupclient $1
echo "REGISTER $1"
echo
addConnection
# iConnections=`getConnectionCount`
_showConnectionCount >/dev/null
if [ $iConnections -le $iMaxConnections -o $iMaxConnections -eq 0 ]; then
sStatus="OK"
iExit=0
_addLog "ACCEPTED [$sBackupClient] got a slot."
else
sStatus="FAILED"
iExit=1
freeConnection
_addLog "REJECTED maximum of $iMaxConnections connections exceeded - request of server [$sBackupClient] failed."
fi
_showConnectionCount
echo $sStatus - rc=$iExit
exit $iExit
}
# unregister a backup slot
function PUBLIC_unregister(){
setBackupclient $1
echo "UNREGISTER $1"
echo
freeConnection
_addLog "REMOVED slot for [$sBackupClient]"
_showConnectionCount
exit 0
}
# show used slots
function PUBLIC_status(){
_showConnectionCount
if [ $iConnections -gt 0 ]; then
find $sConncectionDir -type f -exec ls -l {} \;
fi
exit 0
}
function PUBLIC_usage(){
typeset -i iErrors=0
# can be "full"
sDetail=$1
echo "DISK USAGE:"
echo
echo "time : `date`"
echo "Backups are located in : $sBackupBasedir"
echo "dirs : `find ${sBackupBasedir} -type d | wc -l`"
echo "files : `find ${sBackupBasedir} -type f | wc -l`"
echo "volume size : `df -h ${sBackupBasedir} | tail -1 | awk '{ print $2 }'`"
echo "backup size : `du -hs ${sBackupBasedir} | cut -f 1 `"
echo "free : `df -h ${sBackupBasedir} | tail -1 | awk '{ print $4 }'`"
echo
echo sizes:
# ls -l "${sBackupBasedir}" | grep "^d" | fgrep -v "`basename $sConncectionDir`"
du -hs ${sBackupBasedir}*
echo
echo "changed files last 12 hours and used diskspace:"
echo
_checkDir ${sBackupBasedir}
echo
if [ ! -z ${sDetail} ]; then
sFilter=${sDetail}
if [ "${sDetail}" = "full" ]; then
sFilter=
fi
echo "... and by server and backup sets:"
echo
for myserver in `ls -1 ${sBackupBasedir} | fgrep -v "_active" | grep "$sFilter"`
do
color head
echo ----- $myserver
color reset
echo
_checkDir ${sBackupBasedir}/$myserver
echo
for subdir in `ls -1 ${sBackupBasedir}/$myserver`
do
_checkDir ${sBackupBasedir}/$myserver/$subdir
done
echo
done
fi
# --- show result
echo -n "Status: "
if [ $iErrors -eq 0 ]; then
echo "OK"
else
color error
echo "$iErrors Errors detected."
fi
color reset
echo rc=$iErrors
exit $iErrors
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
if [ ! -d "$sConncectionDir" ]; then
mkdir -p "$sConncectionDir" || exit 1
fi
color head
echo
echo "---------- :: STORAGE :: `hostname` :: ----------"
echo
color reset
sFunction="PUBLIC_$1"
fgrep "${sFunction}()" $0 >/dev/null
if [ $? -ne 0 ]; then
echo SYNTAX:
echo "`basename $0` [function]"
echo
echo " register | unregister [hostname]"
echo " add/ remove slot for a backup client"
echo
echo " status"
echo " show current reserved slots"
echo
echo " usage [full|[filter to hostname]]"
echo " show used diskspace"
echo " you can set a string to show additionally details if these hosts"
echo " if you add \"full\" then details of all hosts will be shown"
echo
#echo SCAN... all functions are:
#grep "function\ PUBLIC_" $0 | cut -f 2 -d "_" | cut -f 1 -d "("
else
shift 1
$sFunction $*
fi
# ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment