Repository navigation
Expand file tree
/
Copy pathmakeimmutable.sh
More file actions
228 lines (206 loc) · 8.19 KB
/
Copy pathmakeimmutable.sh
File metadata and controls
228 lines (206 loc) · 8.19 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#! /bin/ksh
################################################################################
# The MIT License (MIT) #
# #
# Copyright (c) 2020 Nils Haustein #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal#
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,#
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE#
# SOFTWARE. #
################################################################################
# Program: makeimmutable.sh
#
# Description:
#-------------
# Interface script for LIST policy invoked by mmapplypolicy
# Sets selected files to immutable with retention time given in policy.
#
# Prerequisite:
#-------------
# EXTERNAL list policy that identifies files that are not immutable.
#
# Input:
#-------
# invoked by mmapplypolicy with the following parameters:
# $1 operation (list, test)
# $2 file system name or name of filelist
# $3 optional parameter defined in LIST policy under OPTS, defines retention period in days relative to current date
#
# Output:
#--------
# Sets files identified to immutable with retention period define in policy (default is defined as $DEFRETTIME
# Write runtime information and debugging messages to log file $LOGFILE
#
# Example Policy:
#-----------------
#/* define exclude list */
#define( exclude_list,
# (PATH_NAME LIKE '%/.SpaceMan/%' OR
# PATH_NAME LIKE '%/.ltfsee/%' OR
# PATH_NAME LIKE '%/.mmSharedTmpDir/%' OR
# PATH_NAME LIKE '%/.snapshots/%' OR
# NAME LIKE '.mmbackupShadow%' OR
# NAME LIKE 'mmbackup%')
#/* define immutable attribute */
#define( immutable, MISC_ATTRIBUTES LIKE '%X%')
#/* define external list with makeimmutable.sh script to set mp3 files in fileset 'worm' to immutable with 1 day retention */
#RULE EXTERNAL LIST 'setmp3' EXEC '/root/silo/makeimmutable.sh' OPTS '1'
#RULE 'mp3' LIST 'setmp3' FOR FILESET ('worm') WHERE NOT (exclude_list) and NOT (immutable) and (NAME LIKE '%.mp3')
#
# Invokation:
#-------------
# mmapplypolicy fsname -P policyfile [-N nodenames -m threads -B bucketsize]
# Policy file must include a policy as shown above.
#
# Change History
#----------------
# 10/09/12 first implementation based on startbackup
# 12/20/15 implementation for immutability, some streamlining of existing code
# 06/22/20 minor adjustments
#----------------------------------
#These parameters can be adjusted
#-----------------------------------
# define the path for the log file
MYPATH="./makeimmutable/"
# logfile used for system_log function, logs are appended
LOGFILE=$MYPATH"makeimmutable.log"
# sets the log level for the system log, everything below that number is logged
LOGLEVEL=1
# default retention time
DEFRETTIME=0
#----------------------------------
# Constants
#----------------------------------
# GPFS path name to be used with all GPFS commands
gpfsPath="/usr/lpp/mmfs/bin"
## Append to the system log
## Usage: system_log <log_level> <log_message>
system_log () {
SEV=$1
case $SEV in
[0-9]) ;;
*) SEV=1;;
esac
LINE=$2
if [ $LOGLEVEL -ge $SEV ] ; then
if [[ -z "$LINE" ]]; then
echo -e "INTERNAL WARNING: Improper value given to system_log function ($@)" >> $LOGFILE
else
echo -e "$LINE" >> $LOGFILE
fi
fi
}
## Print a message to the stdout
## Usage: user_log <log_message>
user_log () {
echo -e "Makeimmutable $@"
}
## Get current date and time
## Usage: get_cur_date_time
get_cur_date_time(){
echo "$(date +"%Y-%b-%d %H:%M:%S")"
}
#++++++++++++++++++++++++++ MAIN ++++++++++++++++++++++++++++++++++++++
user_log "$(get_cur_date_time) makeimmutable invoked by policy engine"
# check the path for logging
if [[ ! -d $MYPATH ]] then
mkdir -p $MYPATH
rc=$?
if (( rc > 0 )) then
user_log "ERROR: failed to create directory $MYPATH, check permissions"
exit 1
fi
fi
system_log 1 "========================================================================="
system_log 1 "$(get_cur_date_time) makeimmutable invoked with arguments: $*"
## Parse Arguments & execute
#$1 is the policy operation (list, migrate, etc)
op=$1
#$2 is the policy file name
polFile=$2
#$3 is the option given in the EXTERNAL LIST rule with OPTS '..' should be retention time here
retTime=$3
## this is required, as the script may be called multiple times during
## the same backup (if there are too many files to process).
case $op in
TEST )
user_log "INFO: TEST option received for directory $polFile."
system_log 1 "INFO: TEST option received for $polFile"
if [[ ! -z "$polFile" ]] then
if [[ -d "$polFile" ]] then
user_log "INFO: TEST directory $polFile exists."
system_log 1 "INFO: Directory $polFile exists."
else
user_log "WARNING: TEST directory $polFile does not exists."
system_log 1 "WARNING: Directory $polFile does not exist."
fi
fi
;;
LIST )
user_log "INFO: LIST option received, starting makeimmutable task"
system_log 1 "INFO: LIST option received with file name $polFile and options $retTime"
#set retention time to default if not set
if [[ -z $retTime ]] then
retTime=$DEFRETTIME
fi
itemNum=0
numEntries=$(wc -l $polFile | awk '{print $1}')
system_log 1 "INFO: setting retention time $retTime day(s) for $numEntries files"
#consider a function for this
cat $polFile | while read line
do
# use set to get file name, does tolerate blanks
set $line
shift 4
fName="$*"
rc=0
# perhaps check if file exists
if [[ ! -a $fName ]] then
system_log 1 "WARNING: file $fName does not exist."
user_log "DEBUG: file $fName does not exist."
else
$gpfsPath/mmchattr -E $(date +%Y-%m-%d@%H:%M:%S -d "$DATE + $retTime day") "$fName"
rc=$?
if (( rc == 0 )) then
$gpfsPath/mmchattr -i yes "$fName"
(( rc=rc+$? ))
fi
if (( rc == 0 )) then
system_log 1 "INFO: Retention time $retTime for file $fName set successful."
user_log "INFO: Retention time $retTime for file $fName set successful."
else
system_log 1 "WARNING: Setting retention time $retTime for file $fName failed (rc=$rc)"
user_log "WARNING: Setting retention time $retTime for file $fName failed (rc=$rc)"
fi
fi
(( itemNum=itemNum+1 ))
done
system_log 1 "INFO: retention time set for $itemNum out of $numEntries files"
user_log "INFO: retention time set for $itemNum out of $numEntries files"
;;
REDO )
user_log "INFO: REDO option received, doing nothing"
system_log 1 "INFO: REDO option received with file name $polFile and options $retTime"
;;
* )
user_log "WARNING: Unknown option $op received, doing nothing"
system_log 1 "WARNUNG: UNKNOWN option ($op) received with file name $polFile and options $retTime"
;;
esac
user_log "$(get_cur_date_time) makeimmutable ended"
system_log 1 "$(get_cur_date_time) makeimmutable ended"
exit 0