forked from freddenis/dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurge_fs.sh
133 lines (120 loc) · 4.22 KB
/
purge_fs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/home/oracle : MOX103> cat /opt/oranfs/cleanup_nfs/purge_fs/purge_fs.sh
#!/bin/bash
# Fred Denis -- purge exports FS
# Purge directories according to the config file $CONF
# Ignore the "oneoff" subdirectories (asked by Covidien)
#
# Variables definition
#
THE_DATE=`date +%d%m%Y_%H%M` # a timestamp
PURGE="NO" # With no option, do not purge, just show a report of the FS
DEBUG="YES" # comment to remove debug
DIR=`dirname $0`
CONF=${DIR}/purge_fs.conf # Config file
# Usage function
#
function usage {
cat <<!
Usage: $0 -p
-p: purge the files
!
exit 0 ;
}
#
# Get the options of the script
#
while getopts :p name
do
case ${name} in
p) PURGE="YES" ;;
?) usage ;;
esac
done
#
# Check that the config file exists
#
if [ ! -f $CONF ]
then
cat << !
Config file ${CONF} not found !
!
fi
#
# A little debug if needed
#
if [[ -n $DEBUG ]]
then
echo "Config file :" $CONF
fi
IFS=$'\n'
for X in `cat $CONF | grep "^\/" | grep -v DIRECTORY_TO_IGNORE`
do
FS=`echo $X | awk '{print $1}'`
RET=`echo $X | awk '{gsub(" ", "", $2); print $2}'`
if [ ! -d ${FS} ]
then
echo "${FS} does not exists !"
else
if [ "$PURGE" = "YES" ]
then
#
# Directories to ignore
#
IFS=","
IGNORE_LIST=""
for DIR in $(cat ${CONF} | grep "^DIRECTORY_TO_IGNORE" | sed s'/^.*=//g' | sed s'/ //g')
do
if [[ -n $DEBUG ]]
then
echo "To ignore : ${FS}/${DIR}"
fi
IGNORE_LIST=`echo $IGNORE_LIST " ! \( -path \"${FS}/${DIR}\" -prune \)"`
done
#
# Build the find command
#
FIND_COMMAND=`echo "find ${FS} ${IGNORE_LIST} -type f -name \"*.dp\" -mtime +${RET} | xargs rm -f"`
if [[ -n $DEBUG ]]
then
echo "purge ${FS} !"
echo "Ignore list : " ${IGNORE_LIST}
echo $FIND_COMMAND
fi
#
# Execute the purge
#
eval ${FIND_COMMAND}
#
# Check the results of the find
#
if [[ $? -eq 0 ]]
then
echo "${FS} has been purged successfully removing files older than ${RET} days. "
else
echo "Error $? occured during ${FS} purged."
fi
else
cat <<!
Config ${FS} is to purge files older than ${RET} days
**********************************************************************************
!
for i in 3 7 10 14 21 28 45 60 90 120
do
NB_FILES_TO_PURGE=`find ${FS} -type f -name "*.dp" -mtime +${i} | wc -l`
if [ "$NB_FILES_TO_PURGE" = "0" ]
then
SIZE="0"
else
SIZE=`find ${FS} -type f -name "*.dp" -mtime +${i} | xargs ls -l | awk '{size+=$5}END{print size/1024/1024/1024}' | sed s'/\..*$//'`
fi
echo files older than ${i} days : ${NB_FILES_TO_PURGE} \(${SIZE} GB\)
done
if [[ -n $DEBUG ]]
then
echo "x :" $X
echo "FS to purge :" $FS
echo "Retention days :" $RET
fi
fi
fi
done