Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/sharedra...
File: backup_scan.sh
#!/bin/bash
[0] Fix | Delete
# Shared RADS Tools, written by Erik Soroka (eriks@imhadmin.net, ext 834)
[1] Fix | Delete
# This tool is used to generate reports of and purge old cPanel backups on shared servers.
[2] Fix | Delete
[3] Fix | Delete
############################
[4] Fix | Delete
## load functions + banners
[5] Fix | Delete
############################
[6] Fix | Delete
source /opt/sharedrads/radsfunctions.sh
[7] Fix | Delete
[8] Fix | Delete
if [ "$1" != "--api" ]; then
[9] Fix | Delete
clear
[10] Fix | Delete
radsbanner
[11] Fix | Delete
fi
[12] Fix | Delete
[13] Fix | Delete
###########################
[14] Fix | Delete
## SCRIPT FUNCTIONS
[15] Fix | Delete
###########################
[16] Fix | Delete
[17] Fix | Delete
function help() {
[18] Fix | Delete
echo
[19] Fix | Delete
echo "This tool can be used to scan/report or automatically remove backups."
[20] Fix | Delete
echo
[21] Fix | Delete
echo "Usage:"
[22] Fix | Delete
echo
[23] Fix | Delete
echo "REPORT: backup_scan --list : per account, lists # of backups and size"
[24] Fix | Delete
echo " backup_scan --full : full output, lists actual file locations"
[25] Fix | Delete
echo " backup_scan --total : total # of backups in # accounts @ `hostname -s`"
[26] Fix | Delete
echo " backup_scan --api : Application programming interface (T3 use only)"
[27] Fix | Delete
echo
[28] Fix | Delete
echo "REMOVE: backup_scan --dryrun : shows users w/ >500MB of backups to be removed"
[29] Fix | Delete
echo " backup_scan --purge : automatically removes >500MB of backups"
[30] Fix | Delete
echo
[31] Fix | Delete
exit 0
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
function getage() {
[35] Fix | Delete
BACKUP_DATE=`/usr/bin/stat $backup --format=%X`
[36] Fix | Delete
CURRENT_DATE=$(date +%s)
[37] Fix | Delete
DAYSOLD=`echo $BACKUP_DATE $CURRENT_DATE | awk '{ print ($2 - $1)/86400 }'`
[38] Fix | Delete
DAYSOLDINT="`echo $DAYSOLD | cut -d. -f1`"
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
[42] Fix | Delete
function pleasewait {
[43] Fix | Delete
echo
[44] Fix | Delete
echo -n 'Scanning is in progress...'
[45] Fix | Delete
[46] Fix | Delete
while kill -0 "$myPID" 2>/dev/null; do
[47] Fix | Delete
sleep 0.3
[48] Fix | Delete
echo -n "."
[49] Fix | Delete
done
[50] Fix | Delete
echo ; echo
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
function overload {
[54] Fix | Delete
echo "Pausing due to server conditions... (Load Average: ${lavg1%%.*} I/O Wait: $iowait)"
[55] Fix | Delete
sleep 10;
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
function loadcheck {
[59] Fix | Delete
read lavg1 junk < /proc/loadavg
[60] Fix | Delete
iowait=$(/usr/bin/vmstat -S M 2 2 | tail -1 | awk '{print $16}')
[61] Fix | Delete
[62] Fix | Delete
while [[ ${lavg1%%.*} -gt 20 ]] || [[ $iowait -gt 40 ]] ;
[63] Fix | Delete
do
[64] Fix | Delete
overload
[65] Fix | Delete
read lavg1 junk < /proc/loadavg
[66] Fix | Delete
iowait=$(/usr/bin/vmstat -S M 2 2 | tail -1 | awk '{print $16}')
[67] Fix | Delete
done
[68] Fix | Delete
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
#############################
[72] Fix | Delete
## CHECK ARGUMENTS RECEIVED
[73] Fix | Delete
#############################
[74] Fix | Delete
[75] Fix | Delete
if [ "$1" == "--help" ]; then
[76] Fix | Delete
help
[77] Fix | Delete
fi
[78] Fix | Delete
[79] Fix | Delete
if [ -z "$1" ]; then
[80] Fix | Delete
help
[81] Fix | Delete
fi
[82] Fix | Delete
[83] Fix | Delete
[84] Fix | Delete
if [ "$1" == "--full" ]; then
[85] Fix | Delete
echo "Please wait while I search the /home/ partition for any archived cPanel backups."
[86] Fix | Delete
fi
[87] Fix | Delete
[88] Fix | Delete
if [ "$1" == "--list" ]; then
[89] Fix | Delete
echo "Please wait while I search the /home/ partition for any archived cPanel backups."
[90] Fix | Delete
echo " tip: to display results w/full path & directory listings, add the --full flag"
[91] Fix | Delete
fi
[92] Fix | Delete
[93] Fix | Delete
[94] Fix | Delete
##########################
[95] Fix | Delete
##SET A UNIQUE SESSION ID
[96] Fix | Delete
##########################
[97] Fix | Delete
[98] Fix | Delete
SESSIONID=`date +%s`
[99] Fix | Delete
RAWLIST="/tmp/.bkpscan.$SESSIONID.raw"
[100] Fix | Delete
USERLIST="/tmp/.bkpscan.$SESSIONID.user"
[101] Fix | Delete
UNIQUSERLIST="/tmp/.bkpscan.$SESSIONID.uniquser"
[102] Fix | Delete
[103] Fix | Delete
#########################################################
[104] Fix | Delete
## lets list out the directories and wait for completion
[105] Fix | Delete
##########################################################
[106] Fix | Delete
[107] Fix | Delete
if [ "$1" != "--api" ]; then
[108] Fix | Delete
ls /home/*/backup-*.tar.gz -la | sort -nk5 > $RAWLIST &
[109] Fix | Delete
myPID=$!
[110] Fix | Delete
pleasewait
[111] Fix | Delete
else
[112] Fix | Delete
ls /home/*/backup-*.tar.gz -la | sort -nk5 > $RAWLIST
[113] Fix | Delete
fi
[114] Fix | Delete
[115] Fix | Delete
######################################
[116] Fix | Delete
## GET AND SORT UNIQUE USERS
[117] Fix | Delete
######################################
[118] Fix | Delete
[119] Fix | Delete
for user in `cat $RAWLIST | awk '{print $9}' | awk -F_ '{print $3}' | cut -d. -f1`
[120] Fix | Delete
do
[121] Fix | Delete
NOTHERE=FALSE
[122] Fix | Delete
id $user >> /dev/null || NOTHERE=TRUE
[123] Fix | Delete
[124] Fix | Delete
if [ "$NOTHERE" == "FALSE" ]; then
[125] Fix | Delete
echo $user
[126] Fix | Delete
fi
[127] Fix | Delete
[128] Fix | Delete
done > $USERLIST
[129] Fix | Delete
[130] Fix | Delete
#cleanup user list, make unique
[131] Fix | Delete
cat $USERLIST | sort | uniq > $UNIQUSERLIST
[132] Fix | Delete
[133] Fix | Delete
for uniquser in `cat $UNIQUSERLIST`
[134] Fix | Delete
do
[135] Fix | Delete
[136] Fix | Delete
if [ "$1" == "--api" ]; then
[137] Fix | Delete
BACKUPCOUNT=`grep "^$uniquser$" $USERLIST | wc -l`
[138] Fix | Delete
TOTALDISKMB=`grep \/$uniquser\/ $RAWLIST | awk '{ TOTAL += $5 } END { print TOTAL/1024/1024 }' | cut -d. -f1`
[139] Fix | Delete
echo "$uniquser,$BACKUPCOUNT,$TOTALDISKMB"
[140] Fix | Delete
continue
[141] Fix | Delete
fi
[142] Fix | Delete
[143] Fix | Delete
if [ "$1" == "--total" ]; then
[144] Fix | Delete
BACKUPCOUNT=`cat $USERLIST | wc -l`
[145] Fix | Delete
TOTALDISKMB=`cat $RAWLIST | awk '{ TOTAL += $5 } END { print TOTAL/1024/1024 }'`
[146] Fix | Delete
UNIQUSERS=`cat $UNIQUSERLIST | wc -l`
[147] Fix | Delete
echo "[*] Found $BACKUPCOUNT cPanel backups in $UNIQUSERS accounts occupying a total of $TOTALDISKMB MB."
[148] Fix | Delete
echo
[149] Fix | Delete
exit 0
[150] Fix | Delete
fi
[151] Fix | Delete
[152] Fix | Delete
[153] Fix | Delete
#############################################
[154] Fix | Delete
## SHOW WHAT WE WOULD PURGE, NO ACTION TAKEN
[155] Fix | Delete
#############################################
[156] Fix | Delete
[157] Fix | Delete
if [ "$1" == "--dryrun" ]; then
[158] Fix | Delete
BACKUPCOUNT=`grep "^$uniquser$" $USERLIST | wc -l`
[159] Fix | Delete
TOTALDISKMB=`grep \/$uniquser\/ $RAWLIST | awk '{ TOTAL += $5 } END { print TOTAL/1024/1024 }'`
[160] Fix | Delete
TOTALDISKINT=`echo $TOTALDISKMB | cut -d. -f1`
[161] Fix | Delete
[162] Fix | Delete
for backup in `grep \/$uniquser\/ $RAWLIST | awk '{print $9}'`
[163] Fix | Delete
do
[164] Fix | Delete
if [ "$TOTALDISKINT" -gt "500" ]; then
[165] Fix | Delete
echo "[*] $uniquser has more than 500MB of backups."
[166] Fix | Delete
[167] Fix | Delete
if [ -f "$backup" ]; then
[168] Fix | Delete
getage
[169] Fix | Delete
[170] Fix | Delete
if [ "$DAYSOLDINT" -gt "7" ]; then
[171] Fix | Delete
echo "[*] Large backup found >7 days old. This would be removed."
[172] Fix | Delete
echo "nice -n 19 ionice -c2 -n7 rm -rfv $backup"
[173] Fix | Delete
continue
[174] Fix | Delete
else
[175] Fix | Delete
echo "[*] Large backup found, but is not yet 7 days old - skipping."
[176] Fix | Delete
continue
[177] Fix | Delete
fi
[178] Fix | Delete
else
[179] Fix | Delete
echo "ERROR: could not find $backup"
[180] Fix | Delete
continue
[181] Fix | Delete
fi
[182] Fix | Delete
else
[183] Fix | Delete
continue
[184] Fix | Delete
fi
[185] Fix | Delete
done
[186] Fix | Delete
[187] Fix | Delete
fi
[188] Fix | Delete
[189] Fix | Delete
#########################################
[190] Fix | Delete
## LETS ACTUALLY PURGE OLD BACKUPS >500M
[191] Fix | Delete
#########################################
[192] Fix | Delete
[193] Fix | Delete
if [ "$1" == "--purge" ]; then
[194] Fix | Delete
BACKUPCOUNT=`grep "^$uniquser$" $USERLIST | wc -l`
[195] Fix | Delete
TOTALDISKMB=`grep \/$uniquser\/ $RAWLIST | awk '{ TOTAL += $5 } END { print TOTAL/1024/1024 }'`
[196] Fix | Delete
TOTALDISKINT=`echo $TOTALDISKMB | cut -d. -f1`
[197] Fix | Delete
[198] Fix | Delete
for backup in `grep \/$uniquser\/ $RAWLIST | awk '{print $9}'`
[199] Fix | Delete
do
[200] Fix | Delete
if [ "$TOTALDISKINT" -gt "500" ]; then
[201] Fix | Delete
echo "[*] $uniquser has more than 500MB of backups."
[202] Fix | Delete
[203] Fix | Delete
if [ -f "$backup" ]; then
[204] Fix | Delete
getage
[205] Fix | Delete
[206] Fix | Delete
if [ "$DAYSOLDINT" -gt "7" ]; then
[207] Fix | Delete
loadcheck #check to make sure the server is stable before removal
[208] Fix | Delete
echo "[*] Large backup found, automatically removing >7 day old backup."
[209] Fix | Delete
logger -t IMH-backups "$TOTALDISKMB MB of backup(s) found and removed: $backup "
[210] Fix | Delete
nice -n 19 ionice -c2 -n7 rm -rfv $backup
[211] Fix | Delete
continue
[212] Fix | Delete
else
[213] Fix | Delete
echo "[*] Large backup found, but is not yet 7 days old - skipping."
[214] Fix | Delete
continue
[215] Fix | Delete
fi
[216] Fix | Delete
else
[217] Fix | Delete
echo "ERROR: could not find $backup"
[218] Fix | Delete
continue
[219] Fix | Delete
fi
[220] Fix | Delete
else
[221] Fix | Delete
continue
[222] Fix | Delete
fi
[223] Fix | Delete
done
[224] Fix | Delete
[225] Fix | Delete
fi
[226] Fix | Delete
[227] Fix | Delete
[228] Fix | Delete
###########################
[229] Fix | Delete
## FULL REPORT, NO ACTION
[230] Fix | Delete
###########################
[231] Fix | Delete
[232] Fix | Delete
if [ "$1" == "--full" ]; then
[233] Fix | Delete
[234] Fix | Delete
BACKUPCOUNT=`grep "^$uniquser$" $USERLIST | wc -l`
[235] Fix | Delete
TOTALDISKMB=`grep \/$uniquser\/ $RAWLIST | awk '{ TOTAL += $5 } END { print TOTAL/1024/1024 }'`
[236] Fix | Delete
echo "[*] Found the following $BACKUPCOUNT archived cPanel backups in /home/$uniquser occupying a total of $TOTALDISKMB MB:"
[237] Fix | Delete
grep \/$uniquser\/ $RAWLIST
[238] Fix | Delete
echo
[239] Fix | Delete
echo "-----------------------------------------------------------------------------------------------------------"
[240] Fix | Delete
echo
[241] Fix | Delete
fi
[242] Fix | Delete
[243] Fix | Delete
if [ "$1" == "--list" ]; then
[244] Fix | Delete
BACKUPCOUNT=`grep "^$uniquser$" $USERLIST | wc -l`
[245] Fix | Delete
TOTALDISKMB=`grep \/$uniquser\/ $RAWLIST | awk '{ TOTAL += $5 } END { print TOTAL/1024/1024 }' | cut -d. -f1`
[246] Fix | Delete
echo "[*] Found $BACKUPCOUNT cPanel backups in /home/$uniquser occupying a total of $TOTALDISKMB MB."
[247] Fix | Delete
fi
[248] Fix | Delete
[249] Fix | Delete
done
[250] Fix | Delete
[251] Fix | Delete
[252] Fix | Delete
#cleanup our mess
[253] Fix | Delete
rm -f $RAWLIST
[254] Fix | Delete
rm -f $USERLIST
[255] Fix | Delete
rm -f $UNIQUSERLIST
[256] Fix | Delete
[257] Fix | Delete
[258] Fix | Delete
if [ "$1" != "--api" ]; then
[259] Fix | Delete
echo
[260] Fix | Delete
echo "Scanning complete."
[261] Fix | Delete
echo
[262] Fix | Delete
fi
[263] Fix | Delete
[264] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function